1. ## 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. 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. 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. 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. 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);```

6. i have to use a function its the way my assignment is

7. 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.

8. 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;
}
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. Originally Posted by KBriggs
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. Originally Posted by ZaC
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

Originally Posted by youngvito
i have to use a function its the way my assignment is
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 :/

11. 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.

12. Originally Posted by quzah
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][])

13. Originally Posted by ZaC
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.

14. Can I also pass it as a **?
Thanks for the rest

15. Originally Posted by ZaC
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.