Write a program that reads from 1 up to 20 positive integers. The input is ended
ID: 3627028 • Letter: W
Question
Write a program that reads from 1 up to 20 positive integers. The input is ended by the user entering 20 numbers or by entering 0. After reading the numbers entered by user:
1. Display the numbers read and finish with the total of numbers read.
2. Display the range of values entered. (For this write two function one to compute the max and the other the min of the numbers read.)
3. Finish by asking the user for a number. The program displays the total of numbers in the sequence that are equal to the number entered.
4. Do 3 in a loop until the user enters 0 again
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
int maxx(int[],int);
int minn(int[],int);
int count(int[],int,int);
int main()
{int n[20],i=0,j,sum=0,max,min,num;
cout<<"enter number "<<i+1<<" - 0 to exit: ";
cin>>n[i];
max=n[i];
min=n[i];
while(i<20&&n[i]!=0)
{sum+=n[i];
i++;
if(i<20)
{cout<<"enter number "<<i+1<<" - 0 to exit: ";
cin>>n[i];
}
}
cout<<"There were "<<i<<" numbers read, there sum is "<<sum<<endl;
cout<<"The numbers ";
for(j=0;j<i;j++)
cout<<n[j]<<" ";
cout<<endl;
cout<<"The range of numbers is "<<minn(n,i)<<" to "<<maxx(n,i)<<endl;
cout<<"What number do you want to see how many there are(0 to exit)? ";
cin>>num;
while(num!=0)
{
cout<<num<<" occurs "<<count(n,i,num)<<" times ";
cout<<"What number do you want to see how many there are(0 to exit)? ";
cin>>num;
}
}
int maxx(int a[],int n)
{int i,m;
m=a[0];
for(i=1;i<n;i++)
if(a[i]>m)
m=a[i];
return m;
}
int minn(int a[],int n)
{int i,m;
m=a[0];
for(i=1;i<n;i++)
if(a[i]<m)
m=a[i];
return m;
}
int count(int a[],int n,int m)
{int c=0,i;
for(i=0;i<n;i++)
if(a[i]==m)
c++;
return c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.