Write a complete C++ program, including at least 5 good comme nts (one for each
ID: 3687533 • Letter: W
Question
Write a complete C++ program, including at least 5 good comments (one for each function), to do the following:
Create a function int smallest(int a[], int n) that is passed an array of integers A, and a number n indicating the size of the array and returns the smallest value stored in the array A.
Create a function int largest(int a[], int n) that is passed an array of integers A, and a number n indicating the size of the array and returns the largest value stored in the array A.
Create a function double largest(int a[], int n) that is passed an array of integers A, and a number n indicating the size of the array and returns the average of the items inthe array.
Write a main program which reads into an array 10 integers, prints them, then uses the functions above to print the smallest and largest values in the array, followed by the average of the array
Explanation / Answer
Answer:
Here is the complete code...tested :
#include <stdio.h>
#include<iostream>
using namespace std;
int smallest(int[],int);
int largest(int[],int);
double average(int[],int);
int main(){
int x[10];
cout<<"Enter your 10 element array ";
for (int i=0;i<10;++i){
cin>>x[i];
}
cout<<"array largest number is "<<largest(x,10)<<endl;
cout<<"array smallest number is "<<smallest(x,10)<<endl;
cout<<"array average number is "<<average(x,10)<<endl;
}
int smallest(int x[],int n){
int smallest=x[0];
for(int i=0;i<n;++i){
if(x[i]<smallest)
smallest=x[i];
}
return smallest;
}
int largest(int x[],int n){
int largest=x[0];
for(int i=0;i<n;++i){
if(x[i]>largest)
largest=x[i];
}
return largest;
}
double average(int x[],int n){
double average=0;
double sum=0;
for(int i=0;i<n;++i)
sum+=x[i];
average=sum/n;
return average;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.