943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 660
  • C++ RSS
Oct 9th, 2008
0

Multi-dim arrays

Expand Post »
Is it possible to ask the user to input the size of a 2-dim array?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Oct 9th, 2008
0

Re: Multi-dim arrays

Of course, you may ask the user to input size or what else. The problem is what to do with the answer.
Better explain your problem, present source code snippets...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 9th, 2008
0

Re: Multi-dim arrays

can you do something like this but with a 2-dim array?

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int size;
  4. int *pointer;
  5.  
  6. cout<<"Enter size\n";
  7. cin>>size;
  8. pointer=new int[size];
  9.  
  10. for(int i=0;i<size;i++)
  11. {
  12. pointer[i]=i;
  13. }
  14.  
  15. for(int i=0;i<size;i++)
  16. {
  17. cout<<pointer[i]<<endl;
  18. }
  19.  
  20. return 0;
  21. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Oct 9th, 2008
1

Re: Multi-dim arrays

You have to be a little more careful, but this is how you can do it.

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int rows, cols;
  4. int **pointer;
  5.  
  6. cout<<"Enter rows and columns for your 2-D array\n";
  7. cin >> rows >> cols;
  8.  
  9. // allocate a row of pointers.
  10. pointer = new int*[rows];
  11.  
  12. // for each row allocate the columns
  13. for(int i = 0; i < rows; i++){
  14. pointer[i] = new int[cols];
  15. }
  16.  
  17. // add values to the 2-D array
  18. for(int i=0;i < rows;i++){
  19. for(int j = 0; j < cols; j++){
  20. pointer[i][j]=(i + 1) * (j + 1);
  21. }
  22. }
  23.  
  24. // print out the values
  25. for(int i=0;i<rows;i++){
  26. for(int j = 0; j < cols; j++){
  27. cout << pointer[i][j] << '\t';
  28. }
  29. cout << endl;
  30. }
  31.  
  32. //free all the columns for each row
  33. for(int i = 0; i < rows; i++){
  34. delete pointer[i];
  35. }
  36. // free the rows
  37. delete pointer;
  38.  
  39. return 0;
  40. }
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 10th, 2008
1

Re: Multi-dim arrays

or you could do something like ...

C++ Syntax (Toggle Plain Text)
  1. int* arr = new int[rows * cols];

then go on adding elements to it ...
Last edited by Agni; Oct 10th, 2008 at 1:03 am.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: While Loop
Next Thread in C++ Forum Timeline: Not understanding Class Scope





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


Follow us on Twitter


© 2011 DaniWeb® LLC