Write a program that reads in a series of positive integers terminted by a -1, e
ID: 3683841 • Letter: W
Question
Write a program that reads in a series of positive integers terminted by a -1, e.g.
73 95 61 21 90 85 14 78 -1
The values should be stored in an array. The program then prints the values in reverse order as well as the average (to one decimal place). For example,
Please enter the integers: 73 95 61 21 90 85 14 78 -1
The values in reverse order are
78 14 85 90 21 61 95 73
The average is 64.6
You must use a function for computing the reverse, passing the array as an argument. Thus the function header will be similar to
void reverse(int[] A);
NOTE: The program standards are given below:
1. Program Header . In addition to you name and project number, include a brief description of the program functionality in the header comment for the program
2. Variables and Constants. All variables and constants in a program must be declared at the beginning of the program
3. Initialization. Variables may not be initialized in their declaration.
4. Printing of if-then. Use one of the following
if (expression) statement
if (expression)
Single-statement
if (expression) {
Multiple-statements
}
5. Printing of if-then-else. Use one of the following
if (expression) statement
else statement
if (expression)
Single-statement
else
Single-statement
if (expression) {
Multiple-statements
} else {
Multiple-statements
}
6. Printing of while loops. Use one of the following
while (expression) statement
while (expression)
Single-statement
while (expression) {
Multiple-statements
}
7. For loops. The bodies of all for-loops must be indented at least 3 (but no more than 5) spaces:
for(int i = 0; i < n; i++) {
cout << "Enter the number:" << flush;
cin >> num;
if (num < 0) num_neg++;
else if (num > 0) num_pos++;
else num_zero++;
}
8. Methods. The bodies of all functions must be indented at least 3 (but no more than 5) spaces:
double CircleArea(double r) {
const double Pi = 3.1415;
return Pi * r * r;
}
Explanation / Answer
#include <iostream.h>
#include <conio.h>
void reverse(int A[],int s)
{
for(int i=s;i>=0;i--)
{
cout<<A[i]<<endl;
}
}
void main()
{
int a[100],i=0,sum=0,j,n;
double av;
while (1)
{
cout<<"Enter number";
cin>>n;
if(n==-1)
break;
else
{
a[i]=n;
sum=sum+a[i];
i++;
}
}
reverse(a,i-1);
av=sum/(i);
cout<<"average="<<av;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.