#include<iostream> using namespace std; int main() { int min = -INFINITY, max =
ID: 665255 • Letter: #
Question
#include<iostream>
using namespace std;
int main()
{
int min = -INFINITY, max = INFINITY;
int x;
int i = 0, sum = 0, counter = 0;
float average;
cout << "====================================" << endl;
cout << "Please enter a sequence of numbers: ";
cin >> x;
while (cin >> x)
{
if (x < min)
{
min = x;
counter++;
sum += x;
}
else if (x > max && counter == x)
{
max = x;
counter++;
sum += x;
}
}
cout << "Number of values entered: " << counter << endl;
cout << "Min: " << min << endl;
cout << "Max: " << max << endl;
cout << "Avg: " << (float)sum / counter << endl;
system("PAUSE");
return 0;
}
what is wrong with the code? I am supposed to find the max, min, average and number of variables entered.
Explanation / Answer
The code contain lots of error including logical and syntax error.
Try the following code:
include<iostream>
using namespace std;
int main()
{
int min = 0, max = 0;
int x[50];
int sum = 0, counter = 0;
float average;
char choice;
cout << "====================================" << endl;
do
{
cout<<"Please enter a number"<<endl;
cin>>x[counter];
if (x[counter] < min)
{
min = x[counter];
sum += x[counter];
counter++;
}
else if (x[counter] > max)
{
max = x[counter];
sum += x[counter];
counter++;
}
else
{
sum += x[counter];
counter++;
}
cout<<"Do you want to enter more, enter choice(y/n):";
cin>>choice;
}while (choice == 'Y' || choice =='y');
cout << "Number of values entered: " << counter << endl;
cout << "Min: " << min << endl;
cout << "Max: " << max << endl;
cout << "Avg: " << (float)sum / counter << endl;
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.