//This program is going to show the sum, average and print outputs in order. #in
ID: 3759416 • Letter: #
Question
//This program is going to show the sum, average and print outputs in order.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
int Array[10];
for (i = 0; i < 10, i++)
{
cin >> Array[i];
}
highest = Array[0];
lowest = Array[0];
for (i = 1; i < 10; i++)
{
if (Array[i] > highest) highest = Array[i];
if (Array[i] < lowest) lowest = Array[i];
}
system("pause");
return 0;
}
Modify this program to meet these requirements:
Write a program that lets the user enter 10 values into an array. The program should then display the largest, the smallest, the sum, and the average values.
Output should print:
The values are as following:
The smallest value is:
The highest value is:
The sum is:
The average is:
The values printed should be sorted from least to greatest.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,sum=0,avg;
int Array[10];
//Input 10 elements
for (i = 0; i < 10, i++)
{
cin >> Array[i];
//sum calculation
sum=sum+array[i];
}
//Finding the highest and lowest value
//initially set the first elemnt of array itself as highest and lowest
highest = Array[0];
lowest = Array[0];
for (i = 1; i < 10; i++)
{
//calculating lowest and highest
if (Array[i] > highest)highest = Array[i];
if (Array[i] < lowest) lowest = Array[i];
}
//Formula calculating average of 10 elements
avg=sum /10;
//Printing the values of array
cout<<"The values are as following:";
for(i=0;i<10;i++)
{
cout<<array[i];
}
//Print small value in array
cout<<"The smallest value is"<<lowest<<endl;
//Print large value in array
cout<<"The highest value is"<<highest<<endl;
//Print sum of all values in array
cout<<"The sum is"<<sum<<endl;
//Print average of all values in array
cout<<"The avg is"<<avg<<endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.