Write a program to implement the following steps. (1) In your main( ) function,
ID: 3545608 • Letter: W
Question
Write a program to implement the following steps.
(1) In your main( ) function, declare an array to hold 10 integers.
(2) From main( ), call a function named initarray to initialize your array - read a group of
integers from user input into your own function. Note the array should be visible in the main( )
function.
(3) From main( ), call a function named average to calculate the average of all integers in the
array you declared in the main( ) and return the average back to the main( ) function and assign
it to a variable named avg.
(4) From main( ), call a function named maxval to find the maximum from all integers in the
array you declared in the main( ) and return it back to the main( ) function and assign it to a
variable named max.
(5) In the main( ) function, use the formula y = avg + max to calculate y and print it out.
Explanation / Answer
#include <stdio.h>
void initarray(int data[], int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("Enter integer %d: ", i+1);
scanf("%d", &data[i]);
}
}
double average(int data[], int len)
{
double sum = 0;
int i;
for (i = 0; i < len; i++)
sum += data[i];
return sum / len;
}
int maxval(int data[], int len)
{
int m = 0;
int i;
for (i = 1; i < len; i++)
if (data[i] > data[m])
m = i;
return data[m];
}
int main()
{
double avg;
int max;
double y;
int data[10];
initarray(data, 10);
avg = average(data, 10);
max = maxval(data, 10);
y = avg + max;
printf("y = %f ", y);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.