2d Array Help Please!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 31
Reputation: timb89 is an unknown quantity at this point 
Solved Threads: 0
timb89 timb89 is offline Offline
Light Poster

2d Array Help Please!!!

 
0
  #1
Aug 22nd, 2008
How do you read and display a 2d array?
the input text is below
i just really have no idea of tried everything
any sort of help would be appreciated

* . . . . . . . . . . . . . . . . . . *
. * . . . . . . . . . . . . . . . . * .
. . * . . . . . . . . . . . . . . * . .
. . . * . . . . . . . . . . . . * . . .
. . . . * . . . . . . . . . . * . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . * . . . . . . . . . . * . . . .
. . . * . . . . . . . . . . . . * . . .
. . * . . . . . . . . . . . . . . * . .
. * . . . . . . . . . . . . . . . . * .
* . . . . . . . . . . . . . . . . . . *
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 31
Reputation: timb89 is an unknown quantity at this point 
Solved Threads: 0
timb89 timb89 is offline Offline
Light Poster

Re: 2d Array Help Please!!!

 
0
  #2
Aug 23rd, 2008
heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works

wat im not sure about tho is storing information in a text file into a 2d array so lost!

  1. int main()
  2. {
  3. // VARIABLES
  4. string fileName; // the name of the grid file
  5. char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
  6. string str;
  7. int rows = 0;
  8. int columns= 0;
  9. int i = 0;
  10. int j = 0;
  11.  
  12. ifstream inFile;
  13. ofstream outFile;
  14.  
  15. // FILENAME INPUT
  16. cout << "Enter the filename of the grid: " << flush;
  17. cin >> fileName;
  18. cout << endl;
  19.  
  20. // STORE USER INPUT
  21. for (int i = 0; i < rows; i++)
  22. {
  23. for (int j = 0; j < columns; j++)
  24. {
  25. cin >> grid[i][j];
  26. }
  27. }
  28.  
  29. // PRINT USER INPUT
  30. inFile.open(fileName.c_str());
  31.  
  32. for (int i = 0; i < SIZE; i++)
  33. {
  34. getline(inFile, str);
  35. cout << str << endl;
  36. }
  37.  
  38.  
  39. cout << endl;
  40. system("pause");
  41. return 0;
  42. }
Last edited by timb89; Aug 23rd, 2008 at 12:42 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: 2d Array Help Please!!!

 
0
  #3
Aug 23rd, 2008
Do you want to keep a poor user typing this grid char by char, line by line under your program control? It is hardly worth it. Let him/her prepare the grid in his/her favorite text editor then load the result text file in your 2D array. If you don't want to follow this way, why you ask him/her about inFile name?
That's code sceleton:
  1. int i;
  2. for (i = 0; i < SIZE && getline(inFile,str) && str.length() == SIZE; ++i)
  3. {
  4. for (int j = 0; j < SIZE; ++j)
  5. grid[i][j] = str[j];
  6. }
  7. inFile.close();
  8. if (i != SIZE)
  9. {
  10. cout << "*** Invalid grid line #" << i << endl;
  11. return 1; // or what else...
  12. }
  13. // Now you have a filled grid
  14. ...
  15. // Print it
  16. for (int i = 0; i < SIZE; ++i)
  17. {
  18. for (int j = 0; j < SIZE; ++j)
  19. cout << grid[i][j];
  20. cout << '\n';
  21. }
  22. cout.flush();
  23. ...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 31
Reputation: timb89 is an unknown quantity at this point 
Solved Threads: 0
timb89 timb89 is offline Offline
Light Poster

Re: 2d Array Help Please!!!

 
0
  #4
Aug 23rd, 2008
im confused?

i am asking them to input a file?

ive got it to output the txt file
i jsut cant get it store in a 2 d array

ure code helps a bit but im still lost as
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: 2d Array Help Please!!!

 
0
  #5
Aug 23rd, 2008
Do you need to take in the input from the user ? And store those values into a file ?

Or Do you need to read from a file and then again put the same back into the file ?

Please Specify.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2d Array Help Please!!!

 
0
  #6
Aug 23rd, 2008
Originally Posted by timb89 View Post
heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works

wat im not sure about tho is storing information in a text file into a 2d array so lost!

  1. int main()
  2. {
  3. // VARIABLES
  4. string fileName; // the name of the grid file
  5. char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
  6. string str;
  7. int rows = 0;
  8. int columns= 0;
  9. int i = 0;
  10. int j = 0;
  11.  
  12. ifstream inFile;
  13. ofstream outFile;
  14.  
  15. // FILENAME INPUT
  16. cout << "Enter the filename of the grid: " << flush;
  17. cin >> fileName;
  18. cout << endl;
  19.  
  20. // STORE USER INPUT
  21. for (int i = 0; i < rows; i++)
  22. {
  23. for (int j = 0; j < columns; j++)
  24. {
  25. cin >> grid[i][j];
  26. }
  27. }
  28.  
  29. // PRINT USER INPUT
  30. inFile.open(fileName.c_str());
  31.  
  32. for (int i = 0; i < SIZE; i++)
  33. {
  34. getline(inFile, str);
  35. cout << str << endl;
  36. }
  37.  
  38.  
  39. cout << endl;
  40. system("pause");
  41. return 0;
  42. }

You have rows and columns initialized to 0 in lines 7 and 8 and they never change, so your for loop in lines 21 through 27 will never execute. You need to initialize row and columns. ArkM has a good point. For a grid like yours, that's an awful lot of typing. Better to read from a file. If it 's a small matrix, like 4 x 4, you may want to type it in to get it working. Use ArkM's code to display. It'll work, assuming that rows, columns, and SIZE are all the same.

  1. // Print it
  2. for (int i = 0; i < SIZE; ++i)
  3. {
  4. for (int j = 0; j < SIZE; ++j)
  5. cout << grid[i][j];
  6. cout << '\n';
  7. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 31
Reputation: timb89 is an unknown quantity at this point 
Solved Threads: 0
timb89 timb89 is offline Offline
Light Poster

Re: 2d Array Help Please!!!

 
0
  #7
Aug 24th, 2008
Originally Posted by Sky Diploma View Post
Do you need to take in the input from the user ? And store those values into a file ?

Or Do you need to read from a file and then again put the same back into the file ?

Please Specify.
no

their is a text file with a 20x20 grid called in1.txt

the user types in1.txt

the program is meant to store it in an array

then print that array on screen

and it will be the same as the txt input
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: 2d Array Help Please!!!

 
0
  #8
Aug 24th, 2008
Hey Timb, If you are using the same program till now, I see that you have initialised your rows and columns to zero, and therefore your program doesnt even start up getting in data from the file itself. I guess you will need to initialise to 19 both and then try them out.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2d Array Help Please!!!

 
0
  #9
Aug 24th, 2008
Originally Posted by timb89 View Post
no

their is a text file with a 20x20 grid called in1.txt

the user types in1.txt

the program is meant to store it in an array

then print that array on screen

and it will be the same as the txt input
If you are reading from a file, don't use cin in this loop:

    // STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: 2d Array Help Please!!!

 
0
  #10
Aug 24th, 2008
Originally Posted by VernonDozier View Post
If you are reading from a file, don't use cin in this loop:

    // STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }

Yes you should use Infile instead of cin.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC