Array

Hello,
I was working on this code and I'm having some trouble. Please help.
What I have to do:

Write a program that shows a menu in a loop with these following options to get the elements of an array
of integers:

Item 1: To enter manually: Ask the user for the size of the array and then ask to enter the elements of it.
1-1- Store the created array in a variable
1-2- Print the created array
1-3- Use Bubble Sort to sort this array.
1-4- Print the array after sorting and inform the user that you use Bubble Sort to sort the array.
1-5-Ask the user to enter a value to search (Linear search) in this array. Show if the value is found
in the array or not. If it is found, how many of that value is in the array. (In case of repeated
value)
1-6- Write the sorted arrays in a file. (Ask the user to enter the name of the file)

Item 2: To read from a file: Your program should read a file to get the elements of the array of integers
(Use “Partially-Filled Arrays”). Prepare a text file with some integers.
2-1- Store the created array in a variable
2-2- Print the created array.
2-3- Use Selection Sort to sort this second array.
2-4- Print the array after sorting and inform user that you use Selection Sort to sort the array.
2-5- Ask the user to enter a value to search (Binary search) in the array. Show if the value is
found in the array or not. If it is found, how many of that value is in the array. (In case of repeated
value)
2-6- Write the sorted arrays in a file. (Ask the user to enter the name of the file)

Item 3: To exit of this menu

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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

//Function prototypes

int linearSearch(int [], int, int);

void bubbleSort(int [], int);

void swapBubbleSort(int &, int &);

void PrintOut(int [], int);

void selectionSort(int [], int);

void swapSelectionSort(int &, int &);

//Main Function

int main()

{

int count = 1;

short int choice = 0;



//Start program. Print out platform

cout << "********************************\n";

cout << "* GET THE ELEMENTS OF AN ARRAY *\n";

cout << "********************************\n";



cout << "--------------------------------\n";



//Display menu

while (choice != 3) {

cout << "Menu: Enter '1' for Option 1, '2' for Option 2, '3' for Option 3\n";

cout << "1. Option 1: To enter manually\n";

cout << "2. Option 2: To read from a file\n";

cout << "3. Option 3: Exit\n";

cout << "Please choose your one of the following options below!(Enter 1, 2, or 3)\n";

cin >> choice;



//Program running for each option chosen by users

switch (choice) {

//case 1: User manually input elements for the array

case 1:

{

int size;

//User input the size of the array (1-1)

cout << "Please enter how many elements do you want to input!\n";

cin >> size;

int array1[size];

int i=0;

//User input elements for the array

for (i = 0; i < size; i++)

{

cout << "Please enter an element of the array!\n";

cout << "Element-" << count++ << ": ";

cin >> array1[i];

}

//Program Output for unsorted array (1-2)

cout << "Here is your array: ";

PrintOut(array1, size);



//Bubble Sort (1-3)

bubbleSort(array1, size);

//Program Output for sorter array (bubbleSort) (1-4)

cout << "The array after sorting (bubbleSort) in ascending order: ";

for (auto element : array1)

cout << element << " ";

cout << endl;



//User input value they want to look for (1-5)

int value;

cout << "Enter the value of element that you would like to seach in the array: ";

cin >> value;

//Store result of linear search into a variable

int results = linearSearch(array1, size, value);

if (results != -1)

{

cout << "The number of elements found: ";

cout << linearSearch(array1, size, value) << endl;

}

else

{

//result = -1 mean no results is found

cout << "No results are found in the array!";

}

string name;

//Save the array into a txt file with input name from users (1-6)

cout << "Please enter name for the file you want to store the array: ";

cin >> name;

ofstream myfile (name);

if (myfile.is_open()) {

for (int i = 0; i < size; i++)

{

myfile << array1[i] << " ";

}

myfile.close();

}

else

{

cout << "Unable to open the file!\n" << endl;

}

cout << endl;

}

break; //end case 1



//case 2: Read elements from file name (input by user) for array elements

case 2:

{

ifstream Inputfile;

string filename;

int current_number = 0;

vector<int> array2;



//User input the file name to read (2-1)

cout << "What is the file name?\n";

cin >> filename;



//Open file

Inputfile.open(filename);



if (Inputfile) //Check if file open successfully

{

cout << "Successful Open!\n";

while (Inputfile >> current_number) {

array2.push_back(current_number);

}

Inputfile.close();



// Display the numbers read: (2-2)

cout << "The elements are: ";

for (int count = 0; count < array2.size(); count++)

{

cout << array2[count] << " ";

}

cout << endl;

//Selection Sort (2-3)

/*

selectionSort(array2, array2.size());

cout << "The sorted values:\n";

for (auto element1 : array2)

cout << element1 << " ";

cout << endl;

   */

}

else

{

cout << "Error while openning!\n";

}

}

break;

// end case 2

case 3:

{

cout << "Exit" << endl;

}

break;

default:

{

cout << "Please start the program again and choose one of the option!\n";

}

break;

}

return 0;

}

}//end main function

//Stored array in Print Out (1-1)

void PrintOut(int array1[], int size)

{

for (int index = 0; index < size; index++)

cout << array1[index] << " ";

cout << endl;

}// end void PrintOut

void SecondPrintOut(int array2[], int size)

{

for (int index = 0; index < size; index++)

cout << array2[index] << " ";

cout << endl;

}// end void SecondPrintOut

//bubbleSort the array in ascending order (1-3)

void bubbleSort(int array1[], int size)

{

int maxElement;

int index;

for (maxElement = size -1; maxElement > 0; maxElement--)

{

for (index = 0; index < maxElement; index++)

{

if (array1[index] > array1[index + 1])

{

swap(array1[index], array1[index + 1]);

}

}

}

}//end void bubbleSort.

// The swap function swaps a and b in memory.

void swapBubbleSort(int &a, int &b)

{

int temp = a;

a = b;

b = temp;

}//end void swap

//Linear search (1-5)

int linearSearch(int array1[], int size, int value)

{

int count = 0;



for (int index = 0; index < size; index++) {

if (array1[index] == value)

{

//count how many found, return the count

count++;

}

}

return count;

}//end linear search

//Selection Sort the array (2-4)

void selectionSort(int array2[], int size)

{

int minIndex; int minValue;

for (int start = 0; start < (size -1); start++)

{

minIndex = start;

minValue = array2[start];

for (int index = start + 1; index < size; index++)

{

if (array2[index] < minValue)

{

minValue = array2[index];

minIndex = index;

}

}

swap(array2[minIndex], array2[start]);

}

}

// The swap function swaps a and b in memory.

void swapSelectionSort(int &a, int &b)

{

int temp = a;

a = b;

b = temp;

}
Hello shortyvivian,

Welcome to the forum.

Two initial comments:

There are way to many blank lines in your code. This makes it as hard to read as no blank lines.

Indenting of your code is very helpful. As an example:

Your code:
1
2
3
4
5
6
7
8
9
10
11
void swapSelectionSort(int &a, int &b)

{

int temp = a;

a = b;

b = temp;

}


Would look better as:
1
2
3
4
5
6
7
void swapSelectionSort(int &a, int &b)
{
    int temp = a;

    a = b;
    b = temp;
}


You have listed what the program should do, but you have not mentioned what you are having a problem with. As much as some of us would like to be mind readers we are not. You have to tell what is wrong.

Hope that helps,

Andy
Hello shortyvivian,

When I compiled the program the first error "expression must have a constant value" in your code it is in case 1 line 90. C++ does not allow an array to be sized at run time. This number, "size", needs to be a constant value like 10 or 20. You can define "size" as a constant like constexpr int MAXSIZE{ 50 };, but then you would need another variable for input because now "size" can not be changed. The array definition would be int array1[MAXSIZE]{};

You can define the array with a size of 50 and then use a variable like "size" to keep track of the amount of that 50 you will be using.

You have this somewhere:
1
2
3
4
for (int count = 0; count < array2.size(); count++)
{
    cout << array2[count] << " ";
}


This is not a big problem, but something to think about. "count" is defined as an "int", but " array2.size()" returns an "unsigned int". This just gave me a warning about type mismatch.

These were compile errors I had to deal with. Still do not know if the program will run or what you are having a problem with.

Hope that helps,

Andy
Topic archived. No new replies allowed.