adding up the rows of a 2D array

This is a discussion on adding up the rows of a 2D array within the C Programming forums, part of the General Programming Boards category; my problem is to add up the rows of a 2D array. im reading the values from a file using ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

    adding up the rows of a 2D array

    my problem is to add up the rows of a 2D array. im reading the values from a file using I/O redirection. <file3data.txt > out1.txt. My first function adds up the rows and it works fine but my second function is supposed to add up the product of the value and the position of the number in the array and i cant get it to work can someone please tell me what im doing wrong.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    double calcSumAs(double x[], int row);
    double calcSumAXs(double x[], int row);
    
    int main(void)
    {
    	int m;
    	int n;
    	int i;
    	int j;
    	double sumA;
    	double sumAX;
    	double a[100][100];
    	scanf("%d%d", &m, &n);
    	
    	for (i = 0; i < m; i++)
    	{
    		for (j = 0; j < n; j++)
    			scanf("%lf", &a[i][j]);
    	}
    	
    	
    	for (i = 0; i < m; i++)	   	   
    		sumA = calcSumAs(a[i], n);	  	  
    		
    	 
    	
    	for (i = 0; i < m; i++);
    	{
    		sumAX = calcSumAXs(a[i], n);
    		printf("\n%lf", sumAX);
    	}
    	
    	
    	return 0;
    }
    
    double calcSumAs(double x[], int row)
    {
    	int i;
    	double sumA = 0;
    	for (i = 0; i < row; i++)
    		sumA += x[i];
    	
    	return sumA;
    }
    
    double calcSumAXs (double x[], int row)
    {
    	int i;
    	int j = 1;
    	double sumAX = 0;
    	for (i = 0; i < row; i++)
    	{
    		sumAX += x[i] * j;
    		j++;
    	}
    	
    	return sumAX;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    isn't a[i] in a 2D array a pointer to a 1D array that makes up the elements of that row? If it is, then adding a[i] to something is pointer arithmetic, not what you wan I think. you need to sum the elements of it instead.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    can you please give me an idea of how to do that please. Im totally new to this i dont have an idea of what to do

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    COuld you clarify what you are trying to do?

    For example, if you were given the array

    {{5,6,7 }
    {8,9,10}
    {11,12,13}}

    what would you need for the output? If I understand you right, you want the sum of each row, so:

    5 + 6 + 7 = 18
    8 + 9 + 10 = 27
    11 + 12 + 13 = 36

    Is this correct?

  5. #5
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Sorry, why you want to use a function?
    I think this is easier:
    Code:
       	for (i = 0,sumAX=0; i < m; i++)
    	    for (j=0;j<n;j++)
                sumAX += a[i][j] * (j+1);
    Last edited by ZaC; 06-10-2009 at 04:05 PM.
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i have to use a function its the way my assignment is

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    	for (i = 0; i < m; i++);
    	{
    		sumAX = calcSumAXs(a[i], n);
    		printf("\n%lf", sumAX);
    	}
    Careful where you put your semicolons! That one is causing your loop to do nothing; and the the code block that follows uses an invalid value of i . . . .

    Also, in calcSumAXs, you can eliminate j and just use "i+1" instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your first function is adding up only the first element's value, in each row. So you're adding up a column, now a row.

    What you're adding up is in blue:

    Code:
    2 4 6
    8 3 1
    0 9 7
    
    What you want is:
    
    for(r = 0, sumA = 0; r < rows; r++)  {
       for(c = 0, sumRows = 0; c < cols; c++)  {
          sumRows += a[row][col];
       }
       printf("\nSum for Row %d: %d", r, sumRows);
       sumA += sumRows;
    }
    //add your printf line of code for your sumA.
    Your second function is wrong because you based your code off your first function, which you thought was right, but wasn't.

    Now take a second try at your second function, based off this function, and you should be able to make progress.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by KBriggs View Post
    isn't a[i] in a 2D array a pointer to a 1D array that makes up the elements of that row? If it is, then adding a[i] to something is pointer arithmetic, not what you wan I think. you need to sum the elements of it instead.
    Yes - and No.

    a[i] is the ith element in the a[] array. It is also very much like a constant pointer in the 2D a[][] array.

    Which is which, depends on the context of your code.

    Which is why I nearly always use index based code when I'm working with arrays.

  10. #10
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    Question

    Quote Originally Posted by ZaC View Post
    Sorry, why you want to use a function?
    I think this is easier:
    Code:
       	for (i = 0,sumAX=0; i < m; i++)
    	    for (j=0;j<n;j++)
                sumAX += a[i][j] * (j+1);
    @Adak: I guess I didn't understand what youngvito means exactly (probably for my english lacking in skills).
    @youngvito: the code I posted sums up each element multiplied for its position in the column, but I think it's not what you want

    Quote Originally Posted by youngvito View Post
    i have to use a function its the way my assignment is
    Quote Originally Posted by Adak View Post
    Yes - and No.

    a[i] is the ith element in the a[] array. It is also very much like a constant pointer in the 2D a[][] array.

    Which is which, depends on the context of your code.

    Which is why I nearly always use index based code when I'm working with arrays.
    Just to try I rewrote the posted code as a function as follows:
    Code:
    double calcSumAXs (double x[][], int col, int row)
    {
    	double sumAX;
    	for (sumAX=0,col-=1; col>=0; col--)
                for(row-=1;row>=0;row--)
                    sumAX += x[col][row] * (row+1);
    	return sumAX;
    }
    (I don't know if it's what young vito is trying to do, it's just for practicing)
    Why does this code give the error "invalid use of array with unspecified bounds"?
    As I wrote in other posts I'm rusty in coding :/
    Last edited by ZaC; 06-11-2009 at 05:57 AM.
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to tell it all but the right most [] size. IE:
    Code:
    void foo( int bar[ SIZE ][] );
    Otherwise, it doesn't know how to advance down the rows.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Quote Originally Posted by quzah View Post
    You have to tell it all but the right most [] size. IE:
    Code:
    void foo( int bar[ SIZE ][] );
    Otherwise, it doesn't know how to advance down the rows.


    Quzah.
    Uhm... and if I know the size on runtime (using malloc for example) I should use int **bar, isn't it?

    EDIT: I guess: if i'm using a 3D matrix i should tell also the second size, right? (int bar[SIZE1][SIZE2][])
    Last edited by ZaC; 06-11-2009 at 06:11 AM.
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ZaC View Post
    Uhm... and if I know the size on runtime (using malloc for example) I should use int **bar, isn't it?
    You can create a 2D array "dynamically" using malloc with this:
    Code:
    int **matrix=malloc(number_of_rows*sizeof(int*));
    That is actually an array of int pointers, each one to a row. So for each row:
    Code:
    matrix[i] = malloc(number_of_columns*sizeof(int));
    Notice this is an array of ints, not int*s.

    You can pass this around as a parameter:
    Code:
    somefunc(int *matrix[]);
    Meaning you do not have to hardcode either dimension, however, there is no way to tell how big the array is then (it is not null terminated like a string, so there is no way to know where it begins and ends in memory). A normal way to deal with that is to include the size in the parameters:
    Code:
    somefunc(int *matrix[], int num_of_rows, int num_of_columns);
    I don't have much experience working with 3D arrays, but you should be able to work out a variation on this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Can I also pass it as a **?
    Thanks for the rest
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ZaC View Post
    Can I also pass it as a **?
    Thanks for the rest
    I think so. Try. I usually use *matrix[]; they would mean the same thing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Replies: 1
    Last Post: 10-21-2007, 08:44 AM
  3. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 03:38 PM
  4. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21