943,986 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8466
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 21st, 2005
0

Output 2D array?

Expand Post »
Hello ladies and gents,

I'm trying to output a 2D array, but can't seem to find the solution, can anyone help me out here.
#include <stdafx.h>
#include <iostream>

using namespace std;

int main()
{
	int **pp;
	int i, j, n, ni;

	cout<<"Geef het aantal regels als volgt: ";

	cin>> n;
	pp = new int *[n];

	for (i = 0; i < n; i++)
	{
		cout<<"Geef ni, de lengte van de eerstvolgende regel: ";
		
		cin>> ni;
		pp[i] = new int[ni];

		cout<<"Tik "<< ni <<" getallen in:\n";

		for(j = 0; j < ni; j++)
		{
			cin>> pp[i][j];
		}
	}

	for(i = 0; i < n; i++)
	{
		ni = sizeof ?????
		for(j = 0; j < ni; j++)
			cout<< pp[i][j] <<" ";
		cout<<endl;
	}

	for(i = 0; i < n; i++)
		delete[] pp[i];

	delete[] pp;

	cout<<"Press any key to quit!\n";cin.get();

	return 0; 
}

I know I have to use the size of each row of the array, but fail to see wich piece of code I have to use to write next to sizeof
for(i = 0; i < n; i++)
	{
		ni = sizeof ?????
		for(j = 0; j < ni; j++)
			cout<< pp[i][j] <<" ";
		cout<<endl;
	}

Any help would be greatly appreciated
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Jun 21st, 2005
0

Re: Output 2D array?

You'll need to keep track of each size entered by the user.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 21st, 2005
0

Re: Output 2D array?

Hi Dave,

Well, I tried to use this piece of code
C++ Syntax (Toggle Plain Text)
  1. pp[i] = new int[ni];
detemines how many there would be entered in each row of the array, I suspect(ed) that it was this what I needed.

But when I write
C++ Syntax (Toggle Plain Text)
  1. ni = sizeof pp[i];
my output four numbers in each row, if I entered more then four, they are not in the row of the array they supposed to be in.

If I enter less, it's filled up with random number (rubbish)

I'm baffled, because, I don't know what else I should use other then that piece of code
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Jun 21st, 2005
0

Re: Output 2D array?

You will need to keep track of the size of each row yourself. As another variable. Or a different data structure.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 21st, 2005
0

Re: Output 2D array?

Got it

Thanks Dave :!: Now, that's what I call, good TIPS

for (i = 0; i < n; i++)
	{		
		cout<<"Geef ni, de lengte van de eerstvolgende regel: ";

		cin>>ni;
		pp[i] = new int[ni];
		teller[i] = ni;

		cout<<"Tik "<< ni <<" getallen in:\n";

		for(j = 0; j < ni; j++)
			cin>> pp[i][j];
	}

	for(i = 0; i < n; i++)
	{
		for(j = 0; j < teller[i] ;j++)
		{
			cout<< pp[i][j] <<" ";
		}
		cout<<endl;
	}

Is there another way I could have solved this, besides using STL <vector> ?
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Jun 21st, 2005
0

Re: Output 2D array?

Quote ...
Is there another way I could have solved this, besides using STL <vector> ?
Sure. Like I said, you could use a different data structure.
//#include <stdafx.h>
#include <iostream>

using namespace std;

int main()
{
   int i, j, n;
   struct row
   {
      int *col, size;
   } *pp;

   cout<<"Geef het aantal regels als volgt: ";

   cin>> n;
   pp = new row[n];

   for ( i = 0; i < n; i++ )
   {
      cout<<"Geef ni, de lengte van de eerstvolgende regel: ";

      cin>> pp[i].size;
      pp[i].col = new int[pp[i].size];

      cout<<"Tik "<< pp[i].size <<" getallen in:\n";

      for ( j = 0; j < pp[i].size; j++ )
      {
         cin>> pp[i].col[j];
      }
   }

   for ( i = 0; i < n; i++ )
   {
      for ( j = 0; j < pp[i].size; j++ )
         cout<< pp[i].col[j] <<" ";
      cout<<endl;
   }

   for ( i = 0; i < n; i++ )
      delete[] pp[i].col;

   delete[] pp;

   cout<<"Press any key to quit!\n";cin.get();

   return 0;
}

/* my output
Geef het aantal regels als volgt: 3
Geef ni, de lengte van de eerstvolgende regel: 4
Tik 4 getallen in:
1 2 3 4
Geef ni, de lengte van de eerstvolgende regel: 2
Tik 2 getallen in:
5 6
Geef ni, de lengte van de eerstvolgende regel: 3
Tik 3 getallen in:
7 8 9
1 2 3 4
5 6
7 8 9
Press any key to quit!
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 21st, 2005
0

Re: Output 2D array?

Wow, thanks for that example.

Few questions Dave if you don't mind.

1) Am I correct that you wrote the structure inside main so you could get to its members directly?

2) One thing I don't understand, because I actually haven't seen it yet is this
struct row
   {
      int *col, size;
   } *pp;
Why is written outside the structure but yet inside the ; ?

3) You allways show your output in the manner as above, how do you do this, is this something you can let your compiler do?

4) When would you prefere your solution towards mine? For more complicated programs, so that, it actually becomes less complicated?
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Jun 21st, 2005
0

Re: Output 2D array?

1) No, a structure's members are public by default, so anyone can access them. Dave put the structure in main to limit its scope to main. Consider this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. struct test {};
  4. test t; // Okay, test visible
  5. }
  6.  
  7. int foo()
  8. {
  9. test t; // Error! test not visible outside main
  10. }
2) That's a declaration of pp, a pointer to struct row. You can split it up by the type and the identifier, and compare it with an integer to see the similarities:
C++ Syntax (Toggle Plain Text)
  1. Type Identifier
  2. ------------------------------ ----------
  3. struct row {int *col, size;} * p;
  4. int x;
The only difference is that int already exists while struct row is being defined.

3) You can cut and paste from the output medium, such as the console, or redirect the output to a file. It really depends on your system and compiler.

4) Dave's solution makes use of simple objects. Each row object has an array and also stores the size of that array. In all but the most trivial of programs, this solution has the benefit of simplicity.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jun 23rd, 2005
0

Re: Output 2D array?

1) Yep, that made it perfectly clear :!:

2) In other words, it means that *pp has those properties wich are mentioned in the structure right

And can only a pointer have that ability to be written in that place

What is the difference then when writing a structure, like this
C++ Syntax (Toggle Plain Text)
  1. struct row
  2. {
  3. int *col, size;
  4. };
  5.  
  6. int main()
  7. {
  8. row pp;
  9. ...
  10. }
and this:
C++ Syntax (Toggle Plain Text)
  1. struct row
  2. {
  3. int *col, size;
  4. }*pp;
  5.  
  6. int main()
  7. {
  8. pp;
  9. ...
  10. }
Because, the first example also gives the properties of the structure to the object pp right?

3) Well, Ive tried cutting and pasting, but, it doesn't work when the exercise is executed? Could you tell me how I can make it redirect towards the output of a file?
System is Windows XP Pro and compiler is VC++ 6.0

4) Well, I guess it depends on what you call simple right :o But, I do think I get the picture with you're explanation and Dave's code example, I think
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Jun 23rd, 2005
0

Re: Output 2D array?

> In other words, it means that *pp has those properties wich are mentioned in the structure right
Yea, basically.

> And can only a pointer have that ability to be written in that place
No, you're just declaring a variable. It's the same thing as this:
C++ Syntax (Toggle Plain Text)
  1. struct row
  2. {
  3. int *col, size;
  4. };
  5.  
  6. row *pp;
> What is the difference then when writing a structure, like this
pp is local to main in the first example, but has file scope and external linkage in the second.

> Could you tell me how I can make it redirect towards the output of a file?
If you run the program from the command line you can do this:
C++ Syntax (Toggle Plain Text)
  1. C:\> prog > file
  2. C:\> type file
Or you can redirect to a file from directly in within the program. First by using the file stream instead of cout:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. std::ofstream out("file");
  6.  
  7. out << "output\n";
  8. }
Or by re-assigning the stream buffer of the file stream to cout:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. std::ofstream out("file");
  6. std::streambuf *saved_buff = std::cout.rdbuf();
  7.  
  8. std::cout.rdbuf(out.rdbuf());
  9.  
  10. std::cout << "output\n";
  11.  
  12. std::cout.rdbuf(saved_buf);
  13. }
> Well, I guess it depends on what you call simple right
Yea. :mrgreen: Simple for me may not be simple for you, and vice versa depending on the problem. Nobody knows everything.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please some one urgent help me in this code
Next Thread in C++ Forum Timeline: check internet connection?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC