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 :


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

// *********************************************************************
//
//  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 <iostream>
#include <cstdlib>

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<NUMLABS; i++)
	{
		for (j=0; j<labsizes[i]; j++)
		{
			if (labs[i][j]==id)
			{
				// 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;
}


void search(IntPtr labs[], int labsizes[])
{
  //I should be able to do this once I am able to make the array

}




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
MAIN MENU
0) Quit
1) Simulate login
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:

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
#include <iostream>

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 <vector> 


;-)
thanks,

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void createArrays(IntPtr labs[], int labsizes[])

int *ptr_array=NULL;

int sizeLab;
int numCPUs;

ptr_array=new int [numCPUs];

for (int i=0; i<sizeLabs; i++)
{
   for (int j=0; j<numCPUs; j++)
      labs[i][j] = -1;
}



... 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.

1
2
3
4
5
6
7
8
9
10
11
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.