| | |
Output 2D array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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
Any help would be greatly appreciated
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
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
Hi Dave,
Well, I tried to use this piece of code 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 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
Well, I tried to use this piece of code
C++ Syntax (Toggle Plain Text)
pp[i] = new int[ni];
But when I write
C++ Syntax (Toggle Plain Text)
ni = sizeof pp[i];
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
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
Got it
Thanks Dave :!: Now, that's what I call, good TIPS
Is there another way I could have solved this, besides using STL <vector> ?
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> ?
•
•
•
•
Is there another way I could have solved this, besides using STL <vector> ?
//#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
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
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?
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;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?
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:
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:
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.
C++ Syntax (Toggle Plain Text)
int main() { struct test {}; test t; // Okay, test visible } int foo() { test t; // Error! test not visible outside main }
C++ Syntax (Toggle Plain Text)
Type Identifier ------------------------------ ---------- struct row {int *col, size;} * p; int x;
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.
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
and this:
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
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)
struct row { int *col, size; }; int main() { row pp; ... }
C++ Syntax (Toggle Plain Text)
struct row { int *col, size; }*pp; int main() { pp; ... }
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
> 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:
> 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:
Or you can redirect to a file from directly in within the program. First by using the file stream instead of cout:
Or by re-assigning the stream buffer of the file stream to cout:
> 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.
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)
struct row { int *col, size; }; row *pp;
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)
C:\> prog > file C:\> type file
C++ Syntax (Toggle Plain Text)
#include <fstream> int main() { std::ofstream out("file"); out << "output\n"; }
C++ Syntax (Toggle Plain Text)
#include <fstream> int main() { std::ofstream out("file"); std::streambuf *saved_buff = std::cout.rdbuf(); std::cout.rdbuf(out.rdbuf()); std::cout << "output\n"; std::cout.rdbuf(saved_buf); }
Yea. :mrgreen: Simple for me may not be simple for you, and vice versa depending on the problem. Nobody knows everything.
![]() |
Similar Threads
- Return Array from C++ (C++)
- Why am I getting this output (C++)
- How can i call my FileOutput method to output the array ?? really confused... (Java)
- Using a variable as an array's index? (C)
- Array (C++)
- bubble sorting in an array (C)
- writing an array to a file (C++)
Other Threads in the C++ Forum
- Previous Thread: Please some one urgent help me in this code
- Next Thread: check internet connection?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






