Member Avatar for RenFromPenn

Hi,

I am trying to get an example program to run, but it isn't working. I've probably typed something incorrectly, but I have checked three times and I just don't see it. Anyway, it's a sample program from the C Primer Plus book that is supposed to add the rows and columns from an array. The book says that the output should be:

row 0 = 20
row 1 = 24
row 2 = 36
col 1 = 19
col 2 = 21
col 3 = 23

Now, when I run the program, the rows add up just fine. The cols, however, all display the letter d. Could you please have a look at the code and tell me what I missed? Also, could you please tell me how to start the row with 1 instead of 0 as it does in the book? Thanks!

Ren

#include <stdio.h>
#define ROWS 3
#define COLS 4
void sum_rows(int ar[][COLS], int rows);
void sum_cols(int [][COLS], int rows);
int sum2d(int (*ar) [COLS], int rows);
int main(void)
{
	int junk[ROWS][COLS] = {
		{2,4,6,8},
		{3,5,7,9},
		{12,10,8,6}
};
sum_rows(junk, ROWS);
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 = 0;
				for (c = 0; c <COLS; c++)
					tot += ar[r][c];
			printf("row %d sum = %d\n", r, tot);
		}
}

void sum_cols(int ar[][COLS], int rows)
{
	int r;
	int c;
	int tot;

	for(c = 0; c < COLS; c++)
	{
			tot = 0;
			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;
}
Member Avatar for RenFromPenn

Okay, I managed to figure out that I had my % in the wrong place, so that is why the ds were printing. I have now, however, just noticed that the line Sum of all elements = %d\n is not displaying. Any ideas why?

The same program works fine in my compiler (code::blocks using GCC). It prints the line "Sum of all elements = %d" too.

And you can start a row from 1 just by replacing for (r = 0; r < rows; r++) with for (r = 1; r < rows; r++) , but you shouldn't do it(
unless you really know what y-ou're doing) since arrays' subscript always starts from 0. If you're not using the loop to access an array, you can use the loop by starting it from 1 and iterate the same no of times, but it's advisable not to.

Member Avatar for RenFromPenn

Hmm, mine doesn't display the Sum of all elements. Weird. Other than that, it works fine once I discovered the problem that I mentioned above. Anyway, I thought of replacing the 0 with a 1, but that caused the addition to be off. Any other ideas?

Member Avatar for RenFromPenn

Hmm, mine doesn't display the Sum of all elements. Weird. Other than that, it works fine once I discovered the problem that I mentioned above. Anyway, I thought of replacing the 0 with a 1, but that caused the addition to be off. Any other ideas?

Okay, I take that back. I previously tried to set rowNum = 0 to 1 and that was when the addition didn't work.

Member Avatar for RenFromPenn

Okay, I tried it the way that you suggested and it still starts with row 0. It also causes other problems with the program.

What way did I suggest? If you're talking about starting from row 1 by using the for loop I've "suggested", then you're obviously gonna land in trouble since you're skipping the data in row 0(which is technically the first row). You can't expect to get correct results then; even if you modify the loop to run the exact number of times.

Member Avatar for RenFromPenn

Yes, that is what I was referring to when I mentioned your suggestion and I have also found that it skips row 0. Is there any way that the rows could be labeled starting with row 1 that wouldn't be a major headache?

You could actually do it by declaring one extra row and column for each matrix that you declare. Then you can make the first row and the first column redundant, then do all the operations on the rest of the array. In this case you could use for (r = 1; r < =rows; r++) . But this is a really really BAD IDEA. I'm telling you this cause I've found a few textbooks where, for the sake of "simplicity", authors do use the above method(especially in simple programs like bubble sort and the like), but this can cause quite a lot of problems when you start to write much complex programs. Maintaining the array itself can be a big chore, at best. So, even if it seems weird to start looping from zero, get used to it. The alternative is not worth the trouble.

Umm, that sounded pretty unnecessary. Try:

printf("row %d sum = %d\n", r + 1, tot);

Keep the loops the same. It will start the labeling at 1 (0 + 1) and go to 3 (2 + 1).

Umm, that sounded pretty unnecessary. Try:

printf("row %d sum = %d\n", r + 1, tot);

Keep the loops the same. It will start the labeling at 1 (0 + 1) and go to 3 (2 + 1).

I guess that was a li'l too much information, but he wasn't just asking about the labeling(I thought it would be too obvious a thing to ask). I assumed he was talking about array subscripts. Oh well...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.