0
How is this program written?
How is this coded in a c++program Write a program that prompts the user to type a positive integer. When the user types a negative value the program outputs ERROR and asks for another value. When the user inputs 0 (zero) that means that the last value has been typed and the program must output the average of the positive integers. If the user has not input any values, the program should output 'NO AVERAGE'. [Hint Combine loops and if else statements and research about the break statement]
4 Answers
+ 5
#include <iostream>
using namespace std;
int main()
{
int b = 0, average, sum = 0, input = -1;
while (input != 0) {
cout<<"Input a positive integer";
cin>>input;
if (input < 0)
cout<<"ERROR\nInput another integer";
else {
sum += input; b++;
}
}
average=sum/b;
if (average == 0)
cout << "NO AVERAGE";
else
cout << average;
}
0
#include <iostream>
using namespace std;
int main()
{
int a,b,average,sum;
cout<<"Input a positive integer";
cin>>a;
average=sum/b;
{
if(a<0)
cout<<"ERROR\nInput another integer";
cin>>a;
else if (a>0)
cout<<"Your average is"<<average;
else
cout<<"NO AVERAGE";
}
while (a=0)
{
sum=sum+a;
a++;
if(a==0)
break;
}
}
this is my trial program
0
look at this: https://code.sololearn.com/cFvnG63csfOo/?ref=app
problems with your code:
-you calculate the average before reading any values
-you last while loop makes no sense
-all of the stuff you need for your code seems to be there but in random order
-you donât initialize your variables
0
thanks đ