Dynamically Allocated Array Parameters

I have changed my const global int NUMLABS to a non constant variable so that the user can decide how many labs to input. I adjusted the parameters of each function to add NUMLABS becuase the variable is no longer constant. But now main() returns 0 right after the user chooses how many stations to put in each lab. Any suggestions? I am having difficulty understanding these dynamically allocated arrays.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332


/*********************************************************************
 Lab4.cpp

   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.

Written by: Luca Del Signore 
Last modified on: October 3rd
Known bugs: N/A
*********************************************************************/

#include <iostream>
#include <cstdlib>

using namespace std;

// Type definition
typedef int* IntPtr;


// Function prototypes
int NUMLABS;
/*
 Creates the dynamic arrays for the labs.
 @param labs: the array of labs,
 @param labsizes: contains the size (or number of computers) of each lab
  This dictates the size of the dynamic array.
 @precondition: labsize[0] is the # of computers in lab1,
                labsize[1] is the # of computers in lab2, ...
 @postcondition: labs[0] points to lab1's array (of size given by labsize[0])
      labs[1] points to lab2's array (of size given by labsize[1])
        ...
*/
void createArrays(IntPtr labs[], int labsizes[], int NUMLABS);


/* 
freeArrays:
Releases memory we allocated with "new"i.
@param labs: the array of labs to be deleted from memory
@precondition: lab contains data saved to it
@postcondition: lab no longer has memory allocated to it
*/
void freeArrays(IntPtr labs[]);


/*
showLabs:
Displays the status of all labs (who is logged into which computer).
@param labs: the array of labs
@param labsizes: the number of computers in each lab
@precondition: memory has been allocated for the labs
@postcondition: the data stored for each cell in the grid is displayed to the user
*/
void showLabs(IntPtr labs[], int labsizes[], int NUMLABS);


// ======================
// login:
// Simulates a user login by asking for the login info from
// the console.
// ======================
void login(IntPtr labs[], int labsizes[], int NUMLABS);


// ======================
// logoff:
// Searches through the arrays for the input user ID and if found
// logs that user out.
// ======================
void logoff(IntPtr labs[], int labsizes[], int NUMLABS);


// ======================
// search:
// Searches through the arrays for the input user ID and if found
// outputs the station number.
// ======================
void search(IntPtr labs[], int labsizes[], int NUMLABS);



// ======================
//     main function
// ======================
int main()
{
	IntPtr labs[NUMLABS]; 	// store the pointers to the dynamic array for each lab
	int labsizes[NUMLABS];	// Number of computers in each lab
	int choice = -1;
	
	cout <<"Welcome to the LabMonitorProgram!\n";

	// Prompt the user to enter NUMLABS
	do
	{
		cout << "Please enter the number of computer labs." << endl;
		cin >> NUMLABS;
	} while (NUMLABS <= 0);
	


	// Prompt the user to enter labsizes	
	cout <<"Please enter the number of computer stations in each lab:\n"; 
	for (int i=0; i< NUMLABS; i++)
	{
		do
		{
			cout <<"How many computers in Lab "<< i+1<<"?";
			cin >> labsizes[i]; 
		} while (labsizes[i]<0); 
	}

	// Create ragged array structure
	createArrays(labs, labsizes, NUMLABS);

	// Main Menu
	while (choice != 0)
	{
		cout << endl;
		showLabs(labs, labsizes, NUMLABS);
		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, NUMLABS);
		}
		else if (choice == 2)
		{
			logoff(labs, labsizes, NUMLABS);
		}
		else if (choice == 3)
		{
			search(labs, labsizes, NUMLABS);
		}
	}

	freeArrays(labs);		// Free memory before exiting
	return 0;
}


/*
This function operates thusly:
(1) For each element of labs[], memory is allocated according to
the number of computer stations in each lab.
(2) Each element of labsizes[] corresponding to each element of labs[]
is initialized to -1.
 */
void createArrays(IntPtr labs[], int labsizes[], int NUMLABS)
{
	for (int i = 0; i < NUMLABS; i++)
	{
		labs[i] = new int[labsizes[i]];
		for (int j = 0; j < labsizes[j]; j++)
		{
			labs[i][j] = -1;		
		}
	}
}

/*
This function deallocates the memory of labs[].
 */
void freeArrays(IntPtr labs[])
{
	for (int i = 0; i < NUMLABS; i++)
	{
		labs[i] = NULL;
	}
}


/* COMMENTS NEEDED HERE:
	Describe how your function does its job, 
	as required by the comments given for the declaration of the function.  
 */
void showLabs(IntPtr labs[], int labsizes[], int NUMLABS)
{

	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;
}

/*
  This function executes thusly:
  (1) Gets an id number from the user between 1 and 1000000
  (2) Gets the number of the lab from the user
  (3) Gets the number of the staion within the lab from the user
  (4) Checks to see if the station is free. If not returns error message
  (5) Inputs the id number into the that corresponds to the station
 */
void login(IntPtr labs[], int labsizes[], int NUMLABS)
{
	int id, lab, num = -1;

	// read user id 
	do
	{
  		cout << "Enter the 5 digit ID number of the user logging in:" << endl;
		cin >> id;
	} while ((id < 0) || (id > 99999));

	// read the lab number 
	do 
	{
		cout << "Enter the lab number the user is logging in from (1-" <<
			NUMLABS << "):" << endl;
		cin >> lab;
	} while ((lab < 0) || (lab > NUMLABS));

	//read computer number 
	do
	{
		cout << "Enter computer station number the user is logging in to " <<
			"(1-" << labsizes[lab-1] << "):" << endl;
		cin >> num;
	} while ((num < 0) || (num > labsizes[lab-1]));

	// 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;
}


/*
  This function operates thusly:
  (1) Reads the id number from the user
  (2) Searches for the id in the two dimensioanl array grid
  (3) If present, the id nuber is deleted
  (4) The value in the cell is reset to -1 
 */
void logoff(IntPtr labs[], int labsizes[], int NUMLABS)
{
	int id, i,j;

	// Get input from the keyboard, validating data ranges
	do
	{
  		cout << "Enter the 5 digit ID number of the user to find:" << endl;
		cin >> id;
	} while ((id < 0) || (id > 99999));

	
	for (i=0; i<NUMLABS; i++) // check for each lab 
	{
		for (j=0; j<labsizes[i]; j++) //if the user is using any computer in the lab
		{
			if (labs[i][j]==id) //if so, log the user off... 
			{
				// Log the user off by setting the entry to -1
				labs[i][j] = -1;
				cout << "User " << id << " is logged off." << endl;
				return;
			}
		}
	}
	cout << "That user is not logged in." << endl;
	return;
}

/*
  This function executes thusly:
  (1) Reads in the id number from the user
  (2) Searches through the two dimensional array
  (3) If present, displays the lab number and station number of the id
 */
void search(IntPtr labs[], int labsizes[], int NUMLABS)
{
	int id;

	do
        {
                cout << "Enter the 5 digit ID number of the user to find:" << endl;
                cin >> id;
        } while ((id < 0) || (id > 99999));


	for (int i = 0; i < NUMLABS; i++)
	{
		for (int j = 0; j < labsizes[i]; j++)
		{
			if (labs[i][j] == id)
			{
				cout << "Lab: " << i + 1 << "  ";
				cout << "Station: " << j + 1 << endl;	
			}	
		}
	}

}


IntPtr labs[NUMLABS]; is illegal. NUMLABS should be a compile time constant. You should allocate your labs and second array dynamically

freeArrays do not deletes any memory. It just sets pointers to 0 creating massive memory leak.
Topic archived. No new replies allowed.