Prompt the user to ee a positive integer number If the user does not enter a pos
ID: 3590809 • Letter: P
Question
Prompt the user to ee a positive integer number If the user does not enter a positive integer, prompt the user again (continually, if necessary) until the user successfully enters a positive whole number. This process of validating a user's input is sometimes known as input validation, while checking for erroneous input more generally is known as error trapping. Once the user has successfully entered a positive integer, prompt the user to enter numbers. Once the user has entered their numbers, print the average of their numbers to the screen. Hint: An interesting approach to this problem is to see if the floor and the ceiling of the input are equal using the floor and ceil commands respectively. Checking if the user has entered inf or NaN input is harder, so neglect these two special cases.Explanation / Answer
// CPP Program
//headers part
#include <iostream>
#include<stdio.h>
#include<conio.h>
#include <math.h> /* round, floor, ceil, trunc */
using namespace std;
//main function begins
int main()
{
//varible declaration partg
int startnum,a[100],check=1,i,temp,sum=0;
double avg;
//loop to take user valid input
while(check)
{
cout<<"Enter a positive number"<<endl;
cin >> startnum;
if(cin<=0)
continue;
else
check=0;
}
//input taking n elements into array
cout<<"Enter n numbers"<<endl;
for(i=0;i<startnum;i++)
{
cin >> temp;
a[i]=temp;
sum=sum+a[i];
}
//display average vlaue of entered values and display
cout<<"Average value of entere numbers is "<<round(sum/startnum)<<endl;
return 0;
}
Ouput :
Enter a positive number
7
Enter n numbers
1
90
45
3
6
7
3
Average value of entere numbers is 22
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.