Write it in c This lab develops a couple of functions for working with arrays. A
ID: 3680689 • Letter: W
Question
Write it in cThis lab develops a couple of functions for working with arrays. Any of the concepts might be used in future projects, or on quizzes or tests. We will declare an array in the main function, and then manipulate the array by sending it to various functions. Your job will be to write some of these functions. Here are the function prototypes void arrFill (double al, int size) // ask user for array elements void arrWrite (double al, int size) // write out the array double arrTotal (double al, int size); 1/ return total of the array elements double arrAvg (double al , int size) I/ return average of array elements void arrPlus (double c, double al, double b, int size):/ fill c with a+b Notes and hints: (1) The arrFill function should walk through the array, and, for each element, prompt the user to enter a value for this array element, as in last week's lab. The prompt for the element at index 14 should look something like this: Enter value for array element al 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 arrWrite (array write) function. void arrWrite (int al], int size)( // skip a couple lines first printf("Inln") for (int i 0; i
Explanation / Answer
#include <stdio.h>
#include <math.h>
void arrFill (double a[], int size); //ask user for array elements
void arrWrite (double a[], int size); //write out the array
double arrTotal (double a[], int size); //return total of array elements
double arrAvg (double a[], int size); //returen average of array elements
void arrPlus (double c[], double a[], double b[], int size); // fill c with a + b
int main (){
const int SIZE=22; // array size
double x[SIZE]; // array declarations
double y[SIZE];
double z[SIZE];
double s[SIZE];
double d[SIZE];
printf(" Enter %d x values: ", SIZE);
arrFill (x, SIZE);
printf(" Enter %d y vaules: ", SIZE);
arrFill (y, SIZE);
arrPlus (z, x, y, SIZE);
printf(" The x array: ");
arrWrite (x, SIZE);
printf(" The y array: ");
arrWrite(y, SIZE);
printf(" The z = x + y array: ");
arrWrite (z, SIZE);
printf(" The x array total is: %12.4f", arrTotal(x,SIZE));
printf(" The x array average is: %12.4f", arrAvg(x,SIZE));
printf(" ");
return 0;
}
void arrFill (double a[], int size){
int i;
for ( i = 0; i < size; ++i){
printf(" Enter array element a[%3d]: ", i);
scanf("%lf", &a[i]);
}
}
void arrWrite (double a[], int size){
printf(" ");
int i;
for ( i = 0; i < size; ++i){
if (i % 5 == 0) printf(" ");
if (i % 25 == 0) printf(" ");
if (i % 50 == 0) printf(" ");
printf("%12.4f", a[i]);
}
printf(" ");
}
double arrTotal (double a[], int size){
int i;
double Total=0;
for ( i = 0; i < size; i++){
Total = Total + a[i];
}
return Total;
}
double arrAvg (double a[], int size){
double Total = arrTotal(a,size);
return Total/size;
}
void arrPlus (double c[], double b[], double a[], int size){
int i;
for ( i = 0; i < size; i++){
c[i] = b[i] + a[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.