### Trouble with my dynamic array

I've been up all night trying to figure this out and I've gone nowhere. How do I set up and use a dynamic array? I skimmed through a billion and one websites and all the answers varied. Here's my code :

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228`` `````` // ********************************************************************* // // This program uses dynamic arrays to store login information for // four labs. Each of the four labs is referenced by the labs[] array // which is indexed from 0-3. A pointer in the labs[] array then // references a dynamic array that is of size for however many computers // are in that lab. // // ********************************************************************* #include #include using namespace std; // Type definition typedef int* IntPtr; // Constants const int NUMLABS = 4; // Function prototypes void createArrays(IntPtr labs[], int labsizes[]); // ====================== // createArrays: // Creates the dynamic arrays for the labs. // The first array is the array of labs, // The second array contains the size (or number of computers) // we will put in each lab. This dictates the size of the dynamic // array. // ====================== void freeArrays(IntPtr labs[]); // ====================== // freeArrays: // Releases memory we allocated with "new". // ====================== void showLabs(IntPtr labs[], int labsizes[]); // ====================== // showLabs: // Displays the status of all labs (who is logged into which computer). // ====================== void login(IntPtr labs[], int labsizes[]); // ====================== // login: // Simulates a user login by asking for the login info from // the console. // ====================== void logoff(IntPtr labs[], int labsizes[]); // ====================== // logoff: // Searches through the arrays for the input user ID and if found // logs that user out. // ====================== void search(IntPtr labs[], int labsizes[]); // ====================== // search: // Searches through the arrays for the input user ID and if found // outputs the station number. //======================= int main() { IntPtr labs[NUMLABS]; // Array to reference each lab int labsizes[NUMLABS]; // Number of computers in each lab int choice = -1; // Initialize labsizes to those given in the problem labsizes[0] = 5; // 5 computers in lab 1 labsizes[1] = 6; labsizes[2] = 4; labsizes[3] = 3; // 3 computers in lab 4 // Create ragged array structure createArrays(labs, labsizes); // Main Menu while (choice != 0) { cout << endl; showLabs(labs, labsizes); cout << "MAIN MENU" << endl; cout << "0) Quit" << endl; cout << "1) Simulate login" << endl; cout << "2) Simulate logoff" << endl; cout << "3) Search" << endl; cin >> choice; if (choice == 1) { login(labs, labsizes); } else if (choice == 2) { logoff(labs, labsizes); } else if (choice == 3) { search(labs, labsizes); } } freeArrays(labs); // Free memory before exiting return 0; } void createArrays(IntPtr labs[], int labsizes[]) { labs[i][j] = -1; // all I was able to think of here } void freeArrays(IntPtr labs[]) { //I'm assuming this is where I would delete the dynamic array but why make a function for it if I can just do delete [] ...; } void showLabs(IntPtr labs[], int labsizes[]) { using namespace std; int i; int j; cout << "LAB STATUS" << endl; cout << "Lab # Computer Stations" << endl; for (i=0; i < NUMLABS; i++) { cout << i+1 << " "; for (j=0; j < labsizes[i]; j++) { cout << (j+1) << ": "; if (labs[i][j] == -1) { cout << "empty "; } else { cout << labs[i][j] << " "; } } cout << endl; } cout << endl; return; } void login(IntPtr labs[], int labsizes[]) { int id = -1, lab = -1, num = -1; // Get input from the keyboard, validating data ranges while ((id < 0) || (id > 99999)) { cout << "Enter the 5 digit ID number of the user logging in:" << endl; cin >> id; } while ((lab < 0) || (lab > NUMLABS)) { cout << "Enter the lab number the user is logging in from (1-" << NUMLABS << "):" << endl; cin >> lab; } while ((num < 0) || (num > labsizes[lab-1])) { cout << "Enter computer station number the user is logging in to " << "(1-" << labsizes[lab-1] << "):" << endl; cin >> num; } // Check to see if this station is free if (labs[lab-1][num-1]!=-1) { cout << "ERROR, user " << labs[lab-1][num-1] << " is already logged into that station." << endl; return; } // Assign this station to the user labs[lab-1][num-1] = id; return; } void logoff(IntPtr labs[], int labsizes[]) { int id = -1, i,j; // Get input from the keyboard, validating data ranges while ((id < 0) || (id > 99999)) { cout << "Enter the 5 digit ID number of the user to find:" << endl; cin >> id; } for (i=0; i

Sample Run:

LAB STATUS
Lab # Computer Stations
1 1: empty 2: empty 3: empty 4: empty 5: empty
2 1: empty 2: empty 3: empty 4: empty 5: empty 6: empty
3 1: empty 2: empty 3: empty 4: empty
4 1: empty 2: empty 3: empty
0) Quit
2) Simulate logoff
3) Search
1
Enter the 5 digit ID number of the user logging in:
12345
Enter the lab number the user is logging in from (1-4):
2
Enter computer station number the user is logging in to (1-6):
2
LAB STATUS
Lab # Computer Stations
1 1: empty 2: empty 3: empty 4: empty 5: empty
2 1: empty 2: 12345 3: empty 4: empty 5: empty 6: empty
3 1: empty 2: empty 3: empty 4: empty
4 1: empty 2: empty 3: empty

I have absolutely no idea on how to continue. Any help would be greatly appreciated.
A dynamic array would look like this:

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include int main( int argc, char* argv[] ) { int size; int *myDynamicArray; // Get user input. I'm limiting it so it has to // be between 1 and 10 inclusive std::cout << "Please enter size: "; while( !( std::cin >> size ) || size < 1 || size > 10 ) { std::cout << "Invalid size. Enter again: "; std::cin.clear(); std::cin.ignore( 256, '\n' ); } // Set up dynamic array myDynamicArray = new int[size]; // Populate array for( unsigned int i = 0; i < size; i++ ) { myDynamicArray[i] = i; std::cout << "Element " << i << " is " << myDynamicArray[i] << std::endl; } // Delete array delete [] myDynamicArray; return 0; }``````

I'm guessing by your assignment that you're not allowed to use them but, in my opinion, a better dynamic array looks like this:
 `` `` ``#include ``

;-)
thanks,

I came up with this, but haven't tried to execute it yet:

 ``1234567891011121314151617`` `````` void createArrays(IntPtr labs[], int labsizes[]) int *ptr_array=NULL; int sizeLab; int numCPUs; ptr_array=new int [numCPUs]; for (int i=0; i

... ok, im still lost. I know labs[i][j] has to equal -1 so the cout would display 'empty' but thats it.
Well, what you need to do is create a dynamic array for each element of the labs array.

So, when you create your dynamic arrays, so need to do so for each element of that array. The size is stored in the labsizes array.

 ``1234567891011`` ``````void createArrays(IntPtr labs[], int labsizes[]) { for( int i=0; i < NUMLABS; i++ ) { labs[i] = new int[labsizes[i]]; for( int j=0; j < labsizes[i]; j++ ) { labs[i][j] = -1; } } }``````

This code is a pretty bad example of dynamic arrays, to be honest. Rather, it doesn't show *why* you'd use them. The whole point of a dynamic array is that it's size doesn't need to be known at compile time. Having hard coded sizes kind of nullifies that.
Topic archived. No new replies allowed.