Multidimensional Array Help 
Author Message
 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();
 gotoxy(25,8);
 printf("Please Enter Number of Rows : ");  /*Get number of rows from user
*/
 scanf("%d",&row);
 gotoxy(25,10);
 printf("Please Enter Number of Columns: ");
 scanf("%d", &col);   /* Get number of columns from user */
 get_rc(row, col);
 return;

Quote:
}

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*/

    printf("%lf", add);
    }
    getch();
  return;

Quote:
}



Sat, 30 Jun 2001 03:00:00 GMT  
 Multidimensional Array Help
two dimensional arrays have to parts to the array
Array[a][b]
two for loops
for(a=0;a< YOUR MAX FOR A; a++){
for (b=0; b<YOUR MAX FOR B; b++)
{ sum=Array[a][b];}
Quote:
}

That should do it.
Quote:

> 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();
>  gotoxy(25,8);
>  printf("Please Enter Number of Rows : ");  /*Get number of rows from user */
>  scanf("%d",&row);
>  gotoxy(25,10);
>  printf("Please Enter Number of Columns: ");
>  scanf("%d", &col);                     /* Get number of columns from user */
>  get_rc(row, col);
>  return;
> }

> 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*/
>     {
>     printf("%lf", add);
>     }
>     getch();
>   return;
> }



Sat, 30 Jun 2001 03:00:00 GMT  
 Multidimensional Array Help
Array[a][b]
two for loops
for(a=0;a< YOUR MAX FOR A; a++){
sum=0;
for (b=0; b<YOUR MAX FOR B; b++)
{ sum+=Array[a][b];}
Quote:
}

print sum
assuming a represents columns and b represents rows, or else interchange the
for loop.

hope this helps

\rohit



Sat, 30 Jun 2001 03:00:00 GMT  
 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 */
: }



Sat, 30 Jun 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. HELP! Multidimensional array won't work

2. Help with structs / multidimensional arrays?

3. HELP: ways to alloc multidimensional arrays

4. help!!!, fein0001@gold.tc.umn.edu, large multidimensional arrays

5. problems with MultiDimensional Array

6. Multidimensional arrays

7. SetValue with multidimensional array

8. Confusion with the Managed C++ multidimensional array

9. Managed arrays, multidimensional

10. are multidimensional arrays contiguous?

11. Doubts on multidimensional Arrays

12. dynamic multidimensional string arrays....

 

 
Powered by phpBB® Forum Software