Multi-dim arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Multi-dim arrays

 
0
  #1
Oct 9th, 2008
Is it possible to ask the user to input the size of a 2-dim array?
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: Multi-dim arrays

 
0
  #2
Oct 9th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Multi-dim arrays

 
0
  #3
Oct 9th, 2008
can you do something like this but with a 2-dim array?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Multi-dim arrays

 
1
  #4
Oct 9th, 2008
You have to be a little more careful, but this is how you can do it.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Multi-dim arrays

 
1
  #5
Oct 10th, 2008
or you could do something like ...

  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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC