Complete a fragment which reads in a sequence of numbers entered by the user, an
ID: 672000 • Letter: C
Question
Complete a fragment which reads in a sequence of numbers entered by the user, and then displays the number and average of the values entered. The input sequence is terminated by the user entering zero. However, if the first number entered is zero, the user must be re-prompted to enter numbers using the modified prompt “You must enter at least one non-zero number.” until a sequence with at least one non-zero number is obtained. Fill in the conditions controlling both the inner and outer loops, and the places where the variable first try is checked and modified, to make this work.
Explanation / Answer
# include <iostream.h>
# include <conio.h>
#include <math.h>
int main ( )
{
int n, average, sum;
int a[100];
cout << “Please enter the length of the sequence”;
cin >> n;
for ( i=1; i<=n; i++)
{
cin >> a[i];
} // end of for loop
for ( i=1; i<=n; i++)
{
try
{
if ( a[n] = = 0)
{
if ( a[1] = = 0)
{
cout << “you must enter at-least one non-zero number ”;
cin >> a[i];
}
else
{
Sum =sum + a[i];
average = sum/n;
}
} // end of outer if..
cout << “The sequence of numbers entered is” << a[i];
} // end of try
catch ( Exception e)
{
cout << “Error”;
}
} // end of for loop
cout << “The average of numbers in a sequence is ” << average;
return 0;
} // end of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.