Write a C program that inserts 10 numbers into an array and
Write a C++ program that
inserts 10 numbers into
an array, and calculates the average of
even/odd elements of the array.
The program should call the function “printArray” tolist the elements of the array.The function “printArray” should take the array as a parameter and print each element of the array.
The average should always bedisplayed with only
two decimal places using “cout.setf” and “cout.precision”.*
even element: array [0], array[2], array[4], array[6],andarray[8] odd element:array[1]. array[3], array[5], array[7],and array[9]
You will use this program:
#include
// cout.setf(ios::fixed), cout.precision()
using namespace std;
void printArray(int[]);
const int SIZE = 10;
int main(){
int array[SIZE];
//youneed to implement
return 0;
}
void printArray (int a[]){
//
you
need to implement
}
Output:
T
he program should display the following
output. (The red texts are use
r inputs)
Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
List the array elements
array[0]: 1
array[1]: 2
array[2]: 3
array[3]: 4
array[4]: 5
array[5]: 6
array[6]: 7
array[7]: 8
array[8]: 9
array[9]: 10
Average of even
elements: 5.00
Average of odd elements: 6.00
Solution
#include <iostream>
using namespace std;
void printArray(int[]);
const int SIZE = 10;
int main()
{
int array[SIZE];
int i;
double average=0;
cout<<\"\ Enter 10 numbers\";
for(i=0;i<SIZE;i++)
{
cin>>array[i];
}
printArray(array);
cout<<\"\ Average of even elements:\";
for(i=0;i<SIZE;i=i+2) //elements 0,2,4,6,8
{
average = average +array[i];
}
cout.setf(ios::fixed); //formatting output,fixed point with precision =2
cout.precision(2);
cout<<average/5;
average=0;
cout<<\"\ Average of odd elements:\";
for(i=1;i<SIZE;i=i+2) //elements at odd positions 1,3,5,7,9
{
average = average +array[i];
}
cout<<average/5;
return 0;
}
void printArray (int a[]) //definition of function
{
int i;
for(i=0;i<SIZE;i++)
{
cout<<\"\ array[\"<<i<<\"]:\"<<a[i];
}
}
output:
Success time: 0 memory: 3472 signal:0


