1-1. Write a function that returns the sum of all elements in an int array. The
ID: 3917546 • Letter: 1
Question
1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return O if the size parameter, n, is less than 1 Signature : int sum-array(int array[], int n) (Note. This program should be in the file function-1-1.cpp and the main function that call it should be in the file main-1- 1.cpp) 1-2. Write a function that returns the average of all elements in an integer array. The function takes as parameters the array and the number of elements in the array. Your function should return O.0 if the size parameter, n, is less than 1 Signature: double average(int arrayl], int n) 1-3 Write a function that returns the number of elements in an array that are equal to a given parameter. The function should take as parameters the array, its size, and the desired number. The function should return O if the size parameter, n, is less than 1. signature: int count (int arrayt], int n, int number) 1-4.Write a function that returns the sum of all of the elements of two arrays of equal size. The function should take as parameters an array, another array and their size. The function should return 0 if the size parameter, n, is less than 1. signature: int sumtwo(int array, int secondarray[], int n) 1-5 Using a for loop, count the number of even numbers between 1 and a number we supply (inclusive). Return O if the parameter is less than 1. signature: int count_even(int number)Explanation / Answer
#include <iostream>
using namespace std;
int sum_array(int array[], int n)
{
if(n < 1)
return 0;
int result = 0;
for(int i=0; i<n; i++)
{
// adding one number at a time
result = result + array[i];
}
return result;
}
double average(int array[], int n)
{
if(n < 1)
return 0;
int result = 0;
for(int i=0; i<n; i++)
{
// calculating sum
result = result + array[i];
}
// returning average
return result*1.0/n;
}
int count(int array[], int n, int number)
{
if(n < 1)
return 0;
int result = 0;
for(int i=0; i<n; i++)
{
// whenever number is in array, incrementing by 1
if(array[i] == number)
result++;
}
return result;
}
int sumtwo(int array[], int secondarray[], int n)
{
if(n < 1)
return 0;
int result = 0;
for(int i=0; i<n; i++)
{
// ADDING one number from first array and other from second
result = result + array[i];
result = result + secondarray[i];
}
return result;
}
int main() {
int a[5] = {2, 3, 4, 5, 2};
int n = 5;
cout << sum_array(a, n) << endl;
cout << average(a, n) << endl;
cout << count(a, n, 2) << endl;
cout << sumtwo(a, a, n) << endl;
}
/*SAMPLE OUTPUTS
16
3.2
2
32
*/
// Note : 4 sub parts at a time please -- Policy of Chegg
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.