943,509 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 5004
  • C++ RSS
0

Dynamic 2D array - user input

by on Oct 19th, 2009
This program asks for integers from the user and stores them into a multidimensional array that adjusts its size.
C++ Code Snippet (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void allocate_2D(int **&List, int x, int y, int ycur, int xmax)
  7. {
  8. //makes a temp for saving data that was in the array
  9. int **TEMP = List;
  10. //creates a new array to the right size
  11. List = new int*[x];
  12. for( int i = 0; i < x; i++ )
  13. {
  14. List[i] = new int[y];
  15. for( int j = 0; j < y; j++ )
  16. {
  17. List[i][j] = 0;
  18. }
  19. }
  20. //copies info into newly allocated array
  21. for( int i = 0; i < xmax; i++ )
  22. {
  23. for( int j = 0; j <= ycur; j++ )
  24. {
  25. List[i][j] = TEMP[i][j];
  26. }
  27. }
  28. //deletes the TEMP array
  29. for( int i = 0; i < ycur; i++ )
  30. {
  31. delete[] TEMP[i];
  32. TEMP[i] = 0;
  33. }
  34. delete[] TEMP;
  35. TEMP = 0;
  36. }
  37.  
  38. int main()
  39. {
  40. string input;
  41. int xsize = 1, ysize = 1;
  42. int xcur = 0, ycur = 0, xmax = 0;
  43. int **List;
  44. cout << "Enter some integers (input 'r' for new row and 's' for stop):" << endl;
  45. while(cin >> input)
  46. {
  47. if( input == "s" ) //done entering values
  48. break;
  49. else if( input == "r" ) //add a row
  50. {
  51. ysize++;
  52. allocate_2D(List, xsize, ysize, ycur, xmax); //makes a new array with another row
  53. xcur = 0;
  54. ycur += 1;
  55. }
  56. else
  57. {
  58. //add a value
  59. xsize++;
  60. allocate_2D(List, xsize, ysize, ycur, xmax); //makes a new array with another column
  61. List[xcur][ycur] = atoi(input.c_str()); //converts input to integer and adds it to the list
  62. xcur++;
  63. if( xcur > xmax ) //keeps track of the maxium width of the array
  64. xmax = xcur;
  65. }
  66. }
  67. //outputs the array
  68. for( int j = 0; j <= ycur; j++ )
  69. {
  70. for( int i = 0; i < xmax; i++ )
  71. {
  72. cout << List[i][j] << " ";
  73. }
  74. cout << endl;
  75. }
  76. system("PAUSE");
  77. return 0;
  78. }
Comments on this Code Snippet
Oct 23rd, 2009
0

Re: Dynamic 2D array - user input

This snippet sets aside far more memory than is required, mainly due to the fact that both xsize and ysize are continuously incremented without any checks. Used this way, xsize represents the total number of integers typed in (off by one), not the total number of rows. Type in the following:

1 r
2 r
3 r
4 r
s

and you end up with a 5 x 5 array rather than a 1 x4 array.
Last edited by VernonDozier; Oct 23rd, 2009 at 10:22 am.
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Message:
Previous Thread in C++ Forum Timeline: which one is faster
Next Thread in C++ Forum Timeline: help needed in here...





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


Follow us on Twitter


© 2011 DaniWeb® LLC