
Multidimensional Array Help
:
: I wrote I little program to read in numbers from a user and print the
: numbers by Rows, Columns. I need to add together the Columns for an
output.
: I'm stuck on this one!
: Ex: 23 45 65 23
: 12 3 6 2
:
: 35 48 71 25 (total)
:
: This is what I should get but I can't figure out how to add the
columns
: together. I get the rows, and columns. I'm looking for suggestion on
this
: one.
:
: Thanks
: Clinton
:
: #include <stdio.h>
:
: void get_rc(int row, int col);
:
: main(void)
: {
: int row, col;
: clrscr(); /* this is not an ANSI C function */
: gotoxy(25,8); /* neither is this */
: printf("Please Enter Number of Rows : "); /*Get number of rows from
user
: */
: scanf("%d",&row); /*I would avoid using scanf see the FAQ */
: gotoxy(25,10);
: printf("Please Enter Number of Columns: ");
: scanf("%d", &col); /* Get number of columns from user */
: get_rc(row, col);
: return; /* the default return type when left out is int */
/* it would be nice if you returned a value that*/
/* meant something return 0; is the norm for success*/
:}
:
: void get_rc(int row, int col)
: {
: double table[10] [10];
: double add;
: int r,c;
: clrscr();
: gotoxy(28,4);
: printf("Please enter numbers\n\n"); /* Enter numbers into table */
: for (r=0; r<row; r++)
: {
: for (c=0; c<col; c++)
: {
: printf("Enter element [%d] [%d]:", r, c);
: scanf("%lf", &table[r] [c]);
: }
: printf("\n");
: }
: clrscr();
: for (r=0; r<row; r++) /* print table rows-columns to screen */
: {
: for (c=0; c<col; c++)
: {
: printf("%lf\t", table [r][c]);
: }
: printf("\n");
:
: }
: printf("\n"); /* PROBLEM CODE, CAN"T ADD TOGETHER */
: for (c=0; c<col; c++) /* add columns together for output total*/
/* well how would you add them up in your head */
/* you would count down the column adding as you go */
add = 0
for(c = 0; c < col; ++c)
{
add = add + table[r][c];
}
/*and you would do it for each row so it looks like */
for(r = 0; r < row; ++r)
{
add = 0;
for(c = 0; c < col; ++c)
{
add = add + table[r][c];
}
printf("%lf", add);
}
/* I havn't tested this so please anybody feel free to comment */
: printf("%lf", add);
: }
: getch();
: return; /* as the function doesn't return a value */
/* you don't strictly need this here, but it's */
/* a style issue since the end bracket will return */
/* anyway */
: }