| | |
Sample program doesn't display all numbers. Help!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#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; }
And you can start a row from 1 just by replacing
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.
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.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Okay, I take that back. I previously tried to set rowNum = 0 to 1 and that was when the addition didn't work.
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.
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. ![]() |
Similar Threads
- TI-83 Program (Legacy and Other Languages)
- can anyone help me fix a program in functions?? (C++)
- Anyone can do this in C++ (C++)
- can you help me in this program (C)
- Help! How to pull numbers from table at random (C++)
- I want itz solution!! can anyone tell me the soltuion!! (C)
- New to Java, please help with first Assignment (Java)
- Using x86 Assembly Language with Microsoft Visual C++ (C++)
Other Threads in the C Forum
- Previous Thread: Read HTTP Headers from C program
- Next Thread: Couple questions...sorting
Views: 920 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi





