Write a program that reads in a series of positive integers terminted by a -1, e
ID: 3683457 • 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);
Explanation / Answer
#include <iostream>
using namespace std;
void reverse(int A[]);
double calAverage(int A[]);
int size=0;
int main()
{
int a[100],flag=1;
cout << "Enter integers. Enter -1 to end the list" << endl;
while(flag==1)
{
cin>>a[size];
if(a[size]==-1)
flag=0;
else
size++;
}
cout << "Reverse of array";
reverse(a);
cout <<endl <<" Average :" << calAverage(a) <<endl;
return 0;
}
void reverse(int A[])
{
for(int i=size;i>=0;i--)
cout<<A[i]<<" ";
}
double calAverage(int A[])
{
double total=0;
for(int i=size;i>=0;i--)
total+=A[i];
return total/(size);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.