Output 2D array?

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

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Output 2D array?

 
0
  #1
Jun 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output 2D array?

 
0
  #2
Jun 21st, 2005
You'll need to keep track of each size entered by the user.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Output 2D array?

 
0
  #3
Jun 21st, 2005
Hi Dave,

Well, I tried to use this piece of code
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output 2D array?

 
0
  #4
Jun 21st, 2005
You will need to keep track of the size of each row yourself. As another variable. Or a different data structure.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Output 2D array?

 
0
  #5
Jun 21st, 2005
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> ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output 2D array?

 
0
  #6
Jun 21st, 2005
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!
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Output 2D array?

 
0
  #7
Jun 21st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Output 2D array?

 
0
  #8
Jun 21st, 2005
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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Output 2D array?

 
0
  #9
Jun 23rd, 2005
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
  1. struct row
  2. {
  3. int *col, size;
  4. };
  5.  
  6. int main()
  7. {
  8. row pp;
  9. ...
  10. }
and this:
  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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Output 2D array?

 
0
  #10
Jun 23rd, 2005
> 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:
  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:
  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:
  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:
  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.
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