Lab Exercise For this lab you must: Declare and use an array of 10 integers in y
ID: 653232 • Letter: L
Question
Lab Exercise For this lab you must: Declare and use an array of 10 integers in your main function. Collect 10 integer values from the user and store these in the array Create and call a function that takes the array as an argument and returns the smallest value in the array. Create and call a function that takes the array as an argument and returns the largest value in the array. Create and call a function that takes the array as an argument and returns the sum of the array values. Create and call a function that takes the array as an argument and returns the average of the array values. Create and call a function that takes the array as an argument and reverses the order of the values in the array. For example: if you pass an array with these values in this order {2,5,3,1,4} Then the array will contain these values in this order {4,1,3,5,2} after the call to the function. Create and call a function takes the array as an argument and that will print out the values in the array. For example: if the array contains these values in this order {2,5,34,4} it will print out: 2, 5, 3, 1, 4 Display the smallest value in the array for the user. Display the largest value in the array for the user. Display the sum of the values in the array for the user. Display the average of the values in the array for the user Display the values in the array before it is reversed and after it is reversedExplanation / 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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.