Question Using C++ How would you write this without using the FOR statements? #include using namespace std; int main() { int a[20], n, i=0; cout<<"Enter a weight (0 to stop): "<> n; while(n!=0) { a[i] = n; i++; cout<<"Enter a weight (0 to stop): "<> n; } int total =0; for(int j=0;j<i;j++) { total+=a[j]; } double average = total/(double)i; cout<<"The total was "<<total<<endl; cout<<"The average is "<<average<<endl; cout<<"Here are all the numbers less than the average: "; for(int j=0;j<i;j++) { if(a[j]<average) cout<<a[j]<<" "; } cout<<endl; return 0; } Output: Enter a weight (0 to stop): 1 Enter a weight (0 to stop): 2 Enter a weight (0 to stop): 3 Enter a weight (0 to stop): 4 Enter a weight (0 to stop): 5 Enter a weight (0 to stop): 6 Enter a weight (0 to stop): 7 Enter a weight (0 to stop): 8 Enter a weight (0 to stop): 9 Enter a weight (0 to stop): 10 Enter a weight (0 to stop): 0 The total was 55 The average is 5.5 Here are all the numbers less than the average: 1 2 3 4 5
Question
Using C++ How would you write this without using the FOR statements?
#include
<iostream>
using namespace std;
int main() {
int a[20], n, i=0;
cout<<“Enter a weight (0 to stop): “<<endl;
cin >> n;
while(n!=0) {
a[i] = n;
i++;
cout<<“Enter a weight (0 to stop): “<<endl;
cin >> n;
}
int total =0;
for(int j=0;j<i;j++) {
total+=a[j];
}
double average = total/(double)i;
cout<<“The total was “<<total<<endl;
cout<<“The average is “<<average<<endl;
cout<<“Here are all the numbers less than the average: “;
for(int j=0;j<i;j++) {
if(a[j]<average)
cout<<a[j]<<” “;
}
cout<<endl;
return 0;
}
Output:
Enter a weight (0 to stop): 1
Enter a weight (0 to stop): 2
Enter a weight (0 to stop): 3
Enter a weight (0 to stop): 4
Enter a weight (0 to stop): 5
Enter a weight (0 to stop): 6
Enter a weight (0 to stop): 7
Enter a weight (0 to stop): 8
Enter a weight (0 to stop): 9
Enter a weight (0 to stop): 10
Enter a weight (0 to stop): 0
The total was 55
The average is 5.5
Here are all the numbers less than the average: 1 2 3 4 5