please write a C program includes #include <stdio.h> Observe the usual guideline
ID: 3820782 • Letter: P
Question
please write a C program includes
#include <stdio.h>
Observe the usual guidelines regarding the initial comment section, indenting, and so on. In addition, when functions are required, place function definitions following main. At the beginning of the program and before each function use comments to explain what the function does. Do not use global variables.
2. Yearly rainfall data for four weather stations in a certain county is recorded in the table below.
County Rainfall in Inches
Station
2005
2006
2007
2008
2009
2010
1
39.4
45.6
42.3
44.7
37.3
41.6
2
41.0
47.2
43.8
42.0
36.9
38.2
3
38.1
47.9
42.9
43.6
36.2
42.8
4
41.8
46.5
44.1
45.0
38.1
43.3
Initialize at declaration a double subscripted array of doubles with the rainfall data at the four stations for the six years shown. It may also be convenient to initialize an array of years and an array of station numbers.
Call a function to find the average rainfall for each year. Function parameters will include the two-dimensional array of rainfall data and the one-dimensional array of yearly averages. The number of rows and columns in the array may be defined as symbolic constants or may be passed as array parameters.
Call a second function to find the average rainfall in the county for the 6-year period. Return the overall average pass by value. The array of rainfall data will be a function parameter. The number of rows and columns in the array may be defined as symbolic constants or may be passed as array parameters.
Call a third function to print the labeled table of rainfall data, the yearly averages, and the overall average. Print the yearly averages directly below the column data for the appropriate year. Determine with an if statement the years in which the rainfall was above the overall average; then print.
Station
2005
2006
2007
2008
2009
2010
1
39.4
45.6
42.3
44.7
37.3
41.6
2
41.0
47.2
43.8
42.0
36.9
38.2
3
38.1
47.9
42.9
43.6
36.2
42.8
4
41.8
46.5
44.1
45.0
38.1
43.3
Explanation / Answer
#include <stdio.h>
/* function declaration for getting the yearly average */
void getAverageYearly(double arr[][6], double yearlyAvg[]);
/* function declaration for getting average for the county over 6 six years*/
double getTotalRainFall(double arr[][6]);
/* function declaration */
double printTable(double arr[][6], double yearlyAvg[], double totalAvg, int years[],int station[]);
int main()
{
int i=0;
double totalAvg=0;
//initialiazing the rainfall data from 2005-2010
double rain[4][6]={
{39.4,45.6,42.3,44.7,37.3,41.6},
{41.0,47.2,43.8,42.0,36.9,38.2},
{38.1,47.9,42.9,43.6,36.2,42.8},
{41.8,46.5,44.1,45.0,38.1,43.3}
};
int years[]={2005,2006,2007,2008,2009,2010};
int station[]={1,2,3,4};
double yearlyAvg[6];
getAverageYearly(rain,yearlyAvg);
for(i=0;i<6;i++){
printf("yearly average is %.2f",yearlyAvg[i]);
}
totalAvg = getTotalRainFall(rain)/6;
//printf("Total average is %.2f",totalAvg);
printTable(rain,yearlyAvg,totalAvg,years,station);
return 0;
}
/* function declaration for getting the yearly average */
void getAverageYearly(double arr[][6], double yearlyAvg[]){
int j=0,i=0,k=0;
for(j =0;j< 6;j++){
double total =0;
for(i=0;i<4;i++){
total += arr[i][j];
}
yearlyAvg[j]=total/4;
//printf("Sum of the %d column is = %.2f ", j, total);
}
/*for(k=0;k<6;k++){
printf("yearly average is %.2f",yearlyAvg[k]);
}*/
}
double getTotalRainFall(double arr[][6]){
int j=0,i=0;
double total;
for(j =0;j< 6;j++){
double subtotal =0;
for(i=0;i<4;i++){
subtotal += arr[i][j];
}
total +=subtotal;
}
//printf("Total is %.2f ", total);
return total/4;
}
double printTable(double arr[][6], double yearlyAvg[], double totalAvg, int years[],int station[]){
int i=0,j=0;
printf(" %10s %10i %10i %10i %10i %10i %10i ","Station", years[0], years[1], years[2], years[3], years[4], years[5]);
for(i=0;i<4;i++){
printf("%10i ", i+1);
for(j=0;j<6;j++){
printf(" %10.2lf", arr[i][j]);
}
printf(" ");
}
printf("%10s ","Yearly Avg");
for(i=0;i<6;i++){
printf("%10.2lf",yearlyAvg[i]);
}
printf(" Years when the average was more than the total average in 6 years");
for(i=0;i<6;i++){
if(totalAvg<yearlyAvg[i]){
printf(" %10i ",years[i]);
}
}
}
-------output----------
Station 2005 2006 2007 2008 2009 2010
1 39.40 45.60 42.30 44.70 37.30 41.60
2 41.00 47.20 43.80 42.00 36.90 38.20
3 38.10 47.90 42.90 43.60 36.20 42.80
4 41.80 46.50 44.10 45.00 38.10 43.30
Yearly Avg 40.08 46.80 43.27 43.83 37.12 41.48
Years when the average was more than the total average in 6 years
2006
2007
2008
--------------output----------
Note: Feel free to ask any doubts. Happy to help. God bless you!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.