write a program that: 1. prompts the user to enter 15 integers and stores the nu
ID: 3555474 • Letter: W
Question
write a program that:
1. prompts the user to enter 15 integers and stores the numbers into an array.
2. Calls the function computerSum and store the return value into an integer variable.
3. Calls the function findLargest and store the return value into an integer variable.
4. Calls the function countEven and store the return value into an integer variable.
5. Calls the function printResults by passing the array and all numbers returned from the previous functions.
The three functions are constructed as the following:
1. Sum function which computes the sum of all numbers in the array. The function header should looked like:
int computeSum(int anyArray[], int arraySize) {
// your code goes here
Explanation / Answer
#include <iostream>
using namespace std;
int computeSum(int anyArray[], int arraySize) ;
int findLargest(int anyArray[], int arraySize);
int countEven (int anyArray[], int arraySize) ;
void printResults (int anyArray[], int arraySize, int sumOfArray,int largestOfArray, int countOfEven);
int main()
{
int array[15];
int i,largest,sum,even;
/*take input*/
for(i=0;i<15;i++)
cin>>array[i];
/*call every function store values*/
sum=computeSum(array,15) ;
largest=findLargest(array,15) ;
even=countEven(array,15) ;
/*call print*/
printResults(array,15,sum,largest,even) ;
return 0;
}
int computeSum(int anyArray[], int arraySize)
{
int i,sum=0;
for(i=0;i<arraySize;i++)
sum+=anyArray[i];
return sum;
}
int findLargest(int anyArray[], int arraySize)
{
int i,largest;
largest=anyArray[0];
for(i=1;i<arraySize;i++)
{
if(anyArray[i]>largest) /*check array[i] is greater than largest we found till */
largest=anyArray[i];
}
return largest;
}
int countEven (int anyArray[], int arraySize)
{
int i,even=0;
for(i=0;i<arraySize;i++)
{
if(anyArray[i]%2==0) /*check array[i] is divisible by 0 or not */
even++;
}
return even;
}
void printResults (int anyArray[], int arraySize, int sumOfArray,int largestOfArray, int countOfEven)
{
int i;
cout<<"entered array:";
for(i=0;i<arraySize;i++)
{
cout<<anyArray[i]<<" ";
}
cout<<endl<<"sum of array"<<sumOfArray<<endl;
cout<<"largest of array"<<largestOfArray<<endl;
cout<<"no of even numbers in array"<<countOfEven<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.