Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the rain program in Listing 10.7 so that the calculations using pointers

ID: 3772158 • Letter: M

Question

Modify the rain program in Listing 10.7 so that the calculations using pointers instead of subscripts. (You still have to declare and initilized the array)

//array2d.c -- functions for 2d arrays

#include <stidio.h>

#define ROWS 3

#define COLS 4

void sum_rows(int ar[] [COLS], int rows);

void sum_cols(int [] [COLS], int ); // ok to omit

int sum2d(int (*ar) [COLS], int rows); // another syntax

int main(void)

{

int junk[ROWS][COLS] = {

       {2,4,6,8},

       {3,5,7,9},

        {12, 10, 8, 6}

}

sum_rows(junk, ROW);

sum_cols(junk, ROWS);

printf("Sum of all elements = %d/n", sum2d(junk, ROWS));

return 0;

}

void sum_rows(int ar[] [COLS], int rows)

{

int r;

int c;

int tot;

for (r=0; r < rows; r++)

      tot += ar[r][c];

   printf("col %d: sum = %d/n", c, tot);

    }

}

int sum2d(int ar[] [COLS], int rows)

{

    int r;

   int c;

  int tot= 0;

for (r=0; r < rows; r++)

     for (c=0; c< COLS; c++)

         tot += ar[r][c];

return tot;

}

Explanation / Answer

/*program using pointers*/

#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main(void)
{
// initializing rainfall data for 2000 - 2004
const float rain[YEARS][MONTHS] =
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
int year, month;
float subtot, total;

/*my pointer to rain[YEARS][MONTHS]*/
const float (*myRain)[2];


You should use MONTHS as the dimension just to avoid the typo here.
You need 12, not 2.

/*assign rain[][] to myRain pointer*/
myRain = rain;



printf(" YEAR RAINFALL (inches) ");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
/*pointer to rain*/
subtot += *(*(myRain + year) + month);
printf("%5d %15.1f ", 2000 + year, subtot);
total += subtot; // total for all years
}
printf(" The yearly average is %.1f inches. ",
total/YEARS);
printf("MONTHLY AVERAGES: ");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec ");

for (month = 0; month < MONTHS; month++)
{ // for each month, sum rainfall over years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot/YEARS);
}
printf(" ");

return 0;
}


output:

YEAR RAINFALL (inches)
2000 32.4
2001 40.5
2002 36.0
2003 35.2
2004 40.9

The yearly average is 37.0 inches.

MONTHLY AVERAGES:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
7.3 7.3 4.9 3.0 2.3 0.6 1.2 0.3 0.5 1.7 3.6 6.7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote