C++ Write a program that reads in a list of integers from the keyboard into an a
ID: 3769229 • Letter: C
Question
C++
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.
Example Output:
Enter a list of integers (0 to end list):
12
4
4321
567
9
232
0
The integers in reverse order are:
232
9
567
4321
4
12
The largest value entered is 4321
The smallest value entered is 4
Would you like to process another list? (Y or N): N
End program.
Required Test Cases:
34,5,67,43,19,0
0
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
Note: The last list does not need a 0 to end since
it has the maximum number of elements.
Explanation / Answer
#include<iostream>
using namespace std;
void printReverse(int *arr,int n)
{
cout << "The integers in reverse order are:" << endl;
for(int i=n-1;i>=0;i--)
{
cout << arr[i] << endl;
}
}
int largest(int *arr,int n)
{
int max = arr[0];
for(int i=1;i<n;i++)
if(arr[i]>max)
max = arr[i];
return max;
}
int smallest(int *arr,int n)
{
int min = arr[0];
for(int i=1;i<n;i++)
if(arr[i]<min)
min = arr[i];
return min;
}
int main()
{
int arr[20],i;
char choice;
while(true)
{
cout << "Enter a list of 20 integers (0 to end list) : ";
for(i=0;i<20;i++)
{
cin >> arr[i];
if(arr[i]==0)
break;
}
if(i!=0)
{
printReverse(arr, i);
cout << "The largest value entered is " << largest(arr, i) << endl;
cout << "The smallest value entered is " << smallest(arr, i) << endl;
}
else
cout << "No element to process." << endl;
cout << "Would you like to process another list? (Y or N):";
cin >> choice;
if(choice == 'N')
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.