Question: This lab develops few functions for working with arrays. We will declare arrays in the main function, and then manipulate the arrays by sending them
This lab develops few functions for working with arrays. We will declare arrays in the main function, and then manipulate the arrays by sending them to various functions. Your job will be to write some of these functions. Here are the function prototypes:
1. void fill_array (double a[], int size); Ask user for array elements. The size should be entered by the user in the main program.
2. void print_array(double a[], int size); write out the array.
3. double array_total(double a[], int size); return total of the array elements
4. double array_average (double a[], int size); return average of array elements
5. void add_arrays (double c[], double a[], double b[], int size); fill c with a + b
Notes and hints:
(1) The fill_array function should walk through the array, and, for each element, prompt the user to enter a value for this array element, as in last weeks lab. The prompt for the element at index 14 should look something like this: Enter value for array element a[14]: and the cursor should be right after the ':'. Be sure the correct index is displayed.
(2) Here is a cool way to do the print_array function. void print_array (double a[], int size){ printf( ); // skip a couple lines first for (int i = 0; i < size; ++i){ if (i % 5 == 0) printf( ); // 5 numbers per line if (i % 25 == 0) printf( ); // skip a line after each 5 if (i % 50 == 0) printf( ); // skip two lines after 10 printf(%12.4f, a[i]); // width 12, four decimal places } printf( ); // skip a couple more lines }
(3) For example, if size is 4 and a is [1,2,3,4], then array_total should return 10, and array_average should return 2.5. Hint: The easy way to code array_average is to use array_total.
(4) The add_arrays function puts the sum of the arrays a and b into the array c. For example, if size is 4, a is [1,2,3,4], and b is [7,5,3,1], then your code should fill c with [8,7,6,5]. ET 2100 Lab 11, Arrays 30 Points
(5) Remember that functions will have a prototype above the main and a definition below the main. Notice that the []s that show an array is being passed, appear in the prototype and the definition, but the call will have only the name of the array.
Use the following main function to show off your work.
Remember: Do one thing at a time!!! As soon as you code one function, put it into the main so you can see how it works before going on.
#define MAX_SIZE 50 // Maximum array size
int main()
{
// array declarations double first[MAX_SIZE];
double second[MAX_SIZE];
double third[MAX_SIZE];
int size = 0;
//input the value of size
fill_array(first, size); //read first array
fill_array(second,size); // read second array
//output the first array
printf(" The frist array: ");
print_array(first, size);
//output the second array
printf(" The second array: ");
print_array (second, size);
printf(" The third = first + second array: ");
// put first + second into third array
add_arrays(third, first, second, size);
//output the third array
print_array (third,size);
//output the total and the average of the first array
printf(" The first array total is: %12.4lf", array_total(first, size));
printf(" The first array average is: %12.4lf", array_average(first, size));
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
