# Need help working with an array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-21-2008
DCMann2
Need help working with an array
Hi all, I'm having trouble working with an array, and I was wondering if anyone could explain it to me. What I want to do is have a 5x5 array, entered one row at a time, that will add each row and each column.

For example:

Enter row 1: 1 2 3 4 5
Enter row 2: 6 7 8 9 0

Row totals: 15 30
Column totals: 7 9 11 13 5

And so on. I'll post the code I have, but it's pretty basic stuff since I don't know where to go with it. Any help would be greatly appreciated.

Code:

```#include <stdio.h> #include <stdlib.h> #define N 5 int main() {     int a[N][N], i, rowSum, colSum;     printf("Enter %d numbers: ", N);     for (i = 0; i < N; i++)         scanf("%d", &a[i]);       return 0; }```
• 10-21-2008
Salem
a[N][N] has N*N numbers.
Use two nested for loops, so you have something like
scanf( "&#37;d", &a[row][col] );
in the innermost loop.
• 10-21-2008
DCMann2
Is it possible to use a single dimensional array? It seems that it might be easier to use a one dimensional array because it's easier to add the rows that way, but would it possible to add up the columns that way? Arrays always give me trouble, so I am thoroughly confused about this. =/
• 10-21-2008
Salem
Show some code using two loops like I suggest.
• 10-21-2008
C_ntua
Well, there are some methods to use one array. Actually, it will be better using 1D array. But, arrays are ESSENTIAL. You should learn to use them. So I ll post how this could be done with 1D array after you do this with a 2D array.

First of all do what Salem posted to read a 2D array. I ll give you an example of 2 loops and an array.
Code:

```for (i=0; i<N; ++i) {   printf("\n");   for (j=0; j<N; ++j)       printf("%d ", a[i][j]; }```
this will print everything in the 2D array a. Do you understand how the above code works? If not post and we will explain...
• 10-21-2008
DCMann2
I understand how the code works, but now that I have that code, how do I allow the user to enter the values of the array so I can add up the rows and columns?

Code:

```#include <stdio.h> #include <stdlib.h> #define N 5 int main() {     int a[N][N] = {0};     int i, j, rowSum, colSum;     for (i = 0; i < N; ++i)     {         printf("\n");         for (j = 0; j < N; ++j)         printf("&#37;d ", a[i][j]);     }     return 0; }```
• 10-22-2008
Salem
Adding up rows is easy, you just use the same code.

For columns, index using [j][i] and not [i][j]
• 10-22-2008
DCMann2
Use the same code? How would I do that? If I can get the row adding part sorted out, I could figure out the column part without too much difficulty.

I'm still having trouble with entering the values of the array though...what would I write so the user could just enter all 25 values at once instead of 5 at a time? It seems that doing it that way might be a bit easier, but I'm having trouble figuring out how to get it to work.
• 10-22-2008
Salem
Code:

```for ( i = 0 ; i < N ; i++ ) {   for ( j = 0 ; j < N ; j++ ) {     scanf( "&#37;d", &A[i][j] );   } }```
Whether the user types
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

OR
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

OR
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1

doesn't make a blind bit of difference to the for loops.
• 10-22-2008
DCMann2
That's great, but I still don't know how to actually allow the user to enter the values they want. How do I do that? I've read the chapter in my book about arrays a thousand times and I still can't seem to figure it out. =/
• 10-22-2008
Quote:

Originally Posted by DCMann2
That's great, but I still don't know how to actually allow the user to enter the values they want. How do I do that? I've read the chapter in my book about arrays a thousand times and I still can't seem to figure it out. =/

You can use scanf(), just like it was shown to you just above this post.

You need to crawl before you can walk, and you need to learn how to use scanf(), before you start writing a program that requires the user to input values.

Go back to the earlier chapters in your book, and learn the basics, THEN come back and resume your study and practice with arrays.

There are many wrinkles with scanf() that are a PITA to deal with. Take your time and learn it well.

Later, I know you'll want to use fgets() instead, but that's later - not now.
• 10-28-2008
DCMann2
Alright, I've done some more work on this since I last posted, but it's not working right. I'm pretty sure my rowSum and colSum functions are wrong, but at this point I'm mostly concerned about getting the matrix to work properly. Again, any and all help is greatly appreciated.

Code:

```#include <stdio.h> #include <stdlib.h> #define DIM 5 int main() {     int mat[DIM][DIM] = {0}, row, col, rowSum, colSum;     printf("Enter 25 numbers: ");     for (row = 0; row < DIM; row++)         for (col = 0; col < DIM; col++)             scanf("&#37;d", &mat[row][col]);     rowSum += mat[row];     colSum += mat[row];     printf("The sum of the rows is: %d", rowSum);     printf("\n");     printf("The sum of the colums is: %d", colSum);     return 0; }```
• 10-28-2008
tabstop
mat[row] cannot possibly be what you want. All elements of mat have two subscripts behind them, from [0][0] to [4][4].
• 10-28-2008
DCMann2
I know it's not what I want, but can you explain why the matrix isn't working properly? I tried doing a printf of the matrix, but all it gives me is one big number that doesn't contain a single element of the array. What am I doing wrong? :(
• 10-28-2008
tabstop
You don't try to print the matrix in the code there. But if you did, again, you can't just type "mat" and expect people (and your compiler!) not to laugh at you. If you want to print an element of the matrix, you must specify which one by using two subscripts.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last