| | |
Dynamic 2D array - user input
Please support our C++ advertiser: Intel Parallel Studio Home
This program asks for integers from the user and stores them into a multidimensional array that adjusts its size.
#include <iostream> #include <string> using namespace std; void allocate_2D(int **&List, int x, int y, int ycur, int xmax) { //makes a temp for saving data that was in the array int **TEMP = List; //creates a new array to the right size List = new int*[x]; for( int i = 0; i < x; i++ ) { List[i] = new int[y]; for( int j = 0; j < y; j++ ) { List[i][j] = 0; } } //copies info into newly allocated array for( int i = 0; i < xmax; i++ ) { for( int j = 0; j <= ycur; j++ ) { List[i][j] = TEMP[i][j]; } } //deletes the TEMP array for( int i = 0; i < ycur; i++ ) { delete[] TEMP[i]; TEMP[i] = 0; } delete[] TEMP; TEMP = 0; } int main() { string input; int xsize = 1, ysize = 1; int xcur = 0, ycur = 0, xmax = 0; int **List; cout << "Enter some integers (input 'r' for new row and 's' for stop):" << endl; while(cin >> input) { if( input == "s" ) //done entering values break; else if( input == "r" ) //add a row { ysize++; allocate_2D(List, xsize, ysize, ycur, xmax); //makes a new array with another row xcur = 0; ycur += 1; } else { //add a value xsize++; allocate_2D(List, xsize, ysize, ycur, xmax); //makes a new array with another column List[xcur][ycur] = atoi(input.c_str()); //converts input to integer and adds it to the list xcur++; if( xcur > xmax ) //keeps track of the maxium width of the array xmax = xcur; } } //outputs the array for( int j = 0; j <= ycur; j++ ) { for( int i = 0; i < xmax; i++ ) { cout << List[i][j] << " "; } cout << endl; } system("PAUSE"); return 0; }
0
•
•
•
•
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:
and you end up with a 5 x 5 array rather than a 1 x4 array.
1 r
2 r
3 r
4 r
sand 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.
Similar Threads
- Java program - user input and default argument (Java)
- dynamic arrays and standard deviation + grades (C++)
- Dynamic array => dropdown issues... (PHP)
- Programming in VBA reading file into dynamic array (VB.NET)
- 2-dimensional array using user input of high /low temps (Java)
- How To get Input in 2D Dynamic Array?? (C++)
- 2D Dynamic Array (C++)
- user input and arrays (Visual Basic 4 / 5 / 6)
| Thread Tools | Search this Thread |
account age amd arc array avatar binarysearchtree bluegene c# c++ cache cast chips click cpu cron data database ddr3 delete development disk dos dynamic economy energy enterprise file files firefox flash formatdecimal frequency fstream function garbage gecko graph graphics hardware hp ibm ibm.news image include index industry intelibm java leak linux list listbox medicine memory microsoft monitor mozilla mysql news number openoffice opensource parameter pc perl php physics pointer processor programming progressbar ps3 python ram random recession redhat reference russia search sequential server speed ssd string sun supercomputer supercomputing technology trends txt ubuntu upgrade url variable vb visualbasic.net working x86




