### Any Suggestions? (Arrays and Functions)

My Assignment reads as the following
Write a program that reads in a list of integers from the keyboard into an array. The list will be terminated by the value 0. The value 0 is not part of the list and should not be printed out. The integers should be read into an integer array. The maximum number of integers that can be read in is 20.

The program then prints the integers in the reverse order in which they were entered. The program also should display the largest and smallest values stored in the array.

Each operation MUST be done in its own function. Your program should include a loop that lets the user repeat the list until the user says she or he is done.

The only problem im facing is finding a way to terminate the array list with 0, any help would be appreciated Thanks!
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` `````` #include using namespace std; const int Max_Size = 20; // Determines size of array and number of intergers, Do not exceed 20! void showMenu(); // displays the menu for the program void getUserList(int a[], int size); //will get the numbers from user and put into an array making the list void showReverseList(int a[], int size); //function will reverse the numbers given from the array list and display them on the console void getMax(int a[], int size); //gets biggest value from array void getMin(int a[], int size); //gets smallest value from array int main() { int UserNum[Max_Size]; showMenu(); getUserList(UserNum, Max_Size); cout << "Numbers in reverse order" << endl; showReverseList(UserNum, Max_Size); getMax(UserNum, Max_Size); getMin(UserNum, Max_Size); return 0; } void showMenu() { cout << "------------------------------------------------------------------------" << endl; cout << "Enter up to " << Max_Size << " Integers and I will display them in reverse order!" << endl; cout << "Press 0 to end the list" << endl; cout << "------------------------------------------------------------------------" << endl; } void getUserList(int a[], int size) { for (int index = 0; index < size; index++) { cin >> a[index]; } } void showReverseList(int a[], int size) { for (int index = size-1; index >= 0; index--) { cout << a[index] << endl; } } void getMax(int a[], int size) { int Max; Max = a[0]; for (int i = 1; i < size; i++) { if (a[i] > Max) { Max = a[i]; } } cout << "The Biggest Integer of the list is: " << Max << endl; } void getMin(int a[], int size) { int Min; Min = a[0]; for (int i = 1; i < size; i++) { if (a[i] < Min) { Min = a[i]; } } cout << "The Smallest Integer of the list is: " << Min << endl; }``````
`int getUserList(int a[], int size);`

 ``12131415161718192021222324252627`` ``````int main() { int UserNum[Max_Size] { }; showMenu(); int index = getUserList(UserNum, Max_Size); std::cout << "\nNumbers in reverse order\n"; showReverseList(UserNum, index); getMax(UserNum, index); getMin(UserNum, index); }``````

 ``43444546474849505152535455565758596061`` ``````int getUserList(int a[], int size) { int input { }; int index { }; for (index; index < size; index++) { std::cin >> input; if (input == 0) { return index; } a[index] = input; } return index - 1; }``````

 ```------------------------------------------------------------------------ Enter up to 20 Integers and I will display them in reverse order! Press 0 to end the list ------------------------------------------------------------------------ 1 2 3 4 5 0 Numbers in reverse order 5 4 3 2 1 The Biggest Integer of the list is: 5 The Smallest Integer of the list is: 1```

I could suggest some other changes, they'd be mostly cosmetic changes to "prettify" your code and output.
Thank you so much! A life saver truly! If you wouldnt mind showing me how one could repeat the menu again for multiple list?
Are your multiple lists a set number, or do you want to repeat getting lists until the user wants to use a sledge hammer on the monitor? (IOWs, the user uses some input to stop the program).
Getting multiple number list lol, Basically repeat the program.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113`` ``````#include void showMenu(int); int getUserList(int[], int); void showReverseList(int[], int); void getMax(int[], int); void getMin(int[], int); void RunUntilDone(); int main() { RunUntilDone(); } void showMenu(int Max_Size) { std::cout << "------------------------------------------------------------------------\n" << "Enter up to " << Max_Size << " Integers and I will display them in reverse order!\n" << "Press 0 to end the list\n" << "------------------------------------------------------------------------\n"; } int getUserList(int a[], int size) { int input { }; int index { }; for (index; index < size; index++) { std::cin >> input; if (input == 0) { return index; } a[index] = input; } return index; } void showReverseList(int a[], int size) { for (int index = size - 1; index >= 0; index--) { std::cout << a[index] << ' '; } std::cout << "\n\n"; } void getMax(int a[], int size) { int Max { a[0] }; for (int i = 1; i < size; i++) { if (a[i] > Max) { Max = a[i]; } } std::cout << "The Biggest Integer of the list is: " << Max << "\n\n"; } void getMin(int a[], int size) { int Min { a[0] }; for (int i = 1; i < size; i++) { if (a[i] < Min) { Min = a[i]; } } std::cout << "The Smallest Integer of the list is: " << Min << "\n\n"; } void RunUntilDone() { const int Max_Size = 20; int UserNum[Max_Size] { }; while (true) { showMenu(Max_Size); int index = getUserList(UserNum, Max_Size); std::cout << "\nNumbers in reverse order\n"; showReverseList(UserNum, index); getMax(UserNum, index); getMin(UserNum, index); std::cout << "Do you want to do another list? (y or n) "; char go_again { }; std::cin >> go_again; std::cout << '\n'; if (go_again != 'y' && go_again != 'Y') { std::cout << "See ya!\n"; return; } } }``````

 ```------------------------------------------------------------------------ Enter up to 20 Integers and I will display them in reverse order! Press 0 to end the list ------------------------------------------------------------------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Numbers in reverse order 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 The Biggest Integer of the list is: 20 The Smallest Integer of the list is: 1 Do you want to do another list? (y or n) y ------------------------------------------------------------------------ Enter up to 20 Integers and I will display them in reverse order! Press 0 to end the list ------------------------------------------------------------------------ 1 2 3 4 5 0 Numbers in reverse order 5 4 3 2 1 The Biggest Integer of the list is: 5 The Smallest Integer of the list is: 1 Do you want to do another list? (y or n) n See ya!```

I actually introduced a subtle error in getUserList() that only showed up if the user tried to enter 20 numbers. OOOOPS! Fixed now!

It slipped by because I never tested entering 20 numbers until this revision.
Last edited on
Topic archived. No new replies allowed.