Question
In C++ ONLY This lab will make Lab5B more flexible. Instead of requiring the user to enter 15 numbers every time, we will allow the
In C++ ONLY
This lab will make Lab5B more flexible. Instead of requiring the user to enter 15 numbers every time, we will allow the user to enter as many numbers as they wish (assume they will enter at least 1 number). The user will enter non-negative numbers (greater than or equal to zero) one at a time. When the user wishes to no longer enter numbers, the user should enter -1 to signal they are done. When the user is done entering numbers, print in order:
How many numbers the user entered.
The numbers the user entered in order.
The smallest number they entered.
The largest number they entered.
The first number they entered.
The last number they entered.
An example interaction follows:
Hey! Witness my first vector mojo! Enter as many non-negative numbers as you'd like and I will tell you what they are. When you wish to be done, enter -1 to stop entering numbers. Your Number Is: 8 Your Number Is: 6 Your Number Is: 7 Your Number Is: 9 Your Number Is: 3 Your Number Is: 2 Your Number Is: 4 Your Number Is: -1 So awesome! You entered 7 numbers. The numbers are: 8 6 7 9 3 2 4 Have a nice day! The smallest number is: 2 The largest number is: 9 The first number is: 8 The last number is: 4 Gaze at my awesome.
A second interaction may look like:
Hey! Witness my first vector mojo! Enter as many non-negative numbers as you'd like and I will tell you what they are. When you wish to be done, enter -1 to stop entering numbers. Your Number Is: 8 Your Number Is: 6 Your Number Is: 7 Your Number Is: 9 Your Number Is: -1 So awesome! You entered 4 numbers. The numbers are: 8 6 7 9 Have a nice day! The smallest number is: 6 The largest number is: 9 The first number is: 8 The last number is: 9 Gaze at my awesome.
Functional Requirements
Your program cannot make a call to the STL Algorithm method sort(). You must iterate through the vector searching for the largest element yourself.
When printing the number of numbers entered by the user, do not use a counter inside the loop. Instead, make use of one of our vector functions to determine this value.
You must iterate through the vector again after printing all the values. You should not keep track of the smallest and largest value as the user is entering them.
Functioning code -
#includeusing namespace std; int main() { int max; int min; int arr[15]; // array dec for(int i=0;i<15;i++) { cout<<"Number "<":"; cin>>arr[i]; // read array from user } max = arr[0]; min = arr[0]; cout<<"Yaaaaaas!"< "The numbers are : "; for(int i=0;i<15;i++) cout< " "; // print the array cout< for(int i=0;i<15;i++){ if(arr[i] > max){ max= arr[i]; } } cout<<"Largest number is: "< for(int i=0;i<15;i++){ if(arr[1] < min){ min = arr[i]; } } cout<<"Smallest number is: "< " Have a nice day!"; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started