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!
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
  #include <iostream>
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);

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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);
}

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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.
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
#include <iostream>

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.