C multidimensional arrays

This is a discussion on C multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Hey, I'm naturally a vb .net programmer and I'm lost with C's syntax. I'm trying to create an application where ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    C multidimensional arrays

    Hey, I'm naturally a vb.net programmer and I'm lost with C's syntax. I'm trying to create an application where the user inputs some values and then have it run some statistical analysis, but I can't get past the user input part.


    Code:
    #include <stdio.h>
    #include <stdafx.h>
    #define N 25
    int main(void)
    
    {
        double numberarray[N]];
    	int row, col;
    	for (row = 0; row < N; row++)
    			for (col = 0; col < N; col++)
    					if (row == col)
    						scanf("%d", numberarray[row][col]);
    					else
    						scanf("%d", numberarray[row][col]);
        return 0;
    }
    Any help will be greatly appreciated, thank you!
    -sin2win

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    double numberarray[N]];  /* missing the 2nd dimension */

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Code:
    {
        double gradearray[N][N];
        int row =0 , col = 0, i = 1, factor = 1;
     
        printf("Enter max error: ");
     for (row = 0; row < N; row++)
    	 for (col = 0; col < N; col++)
    	 {
    		 if (row == col)
    		 {
    			 gradearray[row][col]=getchar();
    		 }
    		 else
    		 {
    			 gradearray[row][col]=getchar();
    		 }
    	 }
    	 for (row = 0; row < N; row++)
    	 for (col = 0; col < N; col++)
    	 {
    		 if (row == col)
    		 {
    			 printf("%d", gradearray[row][col]);
    		 }
    		 else
    		 {
    			 printf("%d", gradearray[row][col]);
    		 }
    	 }
    This is what I have, it allows me to enter the values for the array but it doesn't seem to be storying them.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    I am trying to loop through the array one row at a time and input the values for each column in a row.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're on the right track. A simple example:

    Code:
     for (row = 0; row < N; row++)  {
        for (col = 0; col < N; col++)  {
       	 scanf("%d", &gradearray[row][col]);
    	 printf("%3d  ", gradearray[row][col]);
       }
    }
    The ampersand '&', gives scanf() the address of the variable, so it can actually change it. Note how printf() doesn't need that address, since it won't be changing anything.

    In general, it's a good idea to get all your input done first, then work on it in another pair of nested loops.

    Trying to input data into a 2D array, and also do some computational work, will make things very prone to errors.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Thanks for your help, I got it to where it properly allows the user to enter the values into the array, now I need to have each row summed.

    Code:
    st1ttl = (gradearray[0][0] + gradearray[0][1] + gradearray[0][2] + gradearray[0][3] + gradearray[0][4]);
    printf("\t", st1ttl, "\n");
    This code apparently isn't saving any value to st1tll....Is there a simpler function to sum the values in a row of the array?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by sin2win View Post
    Thanks for your help, I got it to where it properly allows the user to enter the values into the array, now I need to have each row summed.

    Code:
    st1ttl = (gradearray[0][0] + gradearray[0][1] + gradearray[0][2] + gradearray[0][3] + gradearray[0][4]);
    printf("\t", st1ttl, "\n");
    This code apparently isn't saving any value to st1tll....Is there a simpler function to sum the values in a row of the array?
    The problem is your printf() call is mal-formed. You need to tell printf() what data type you want printed. Perhaps %d for integers?

    printf("\t %d", st1ttl);

    There is no function to sum up a row. You went about it the hard way. Easy way is to use a nested loop:
    Code:
    for(j = 0; j < NumColumnsInEachRow; j++)
      rowTotal += gradearray[row][j];
    Just remember to use your totals and such, and to reset them to zero, before the next loop starts. That should work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays in a function prototype
    By Enanito01478 in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:10 AM
  2. Multidimensional arrays
    By Niels_M in forum C Programming
    Replies: 51
    Last Post: 09-12-2009, 03:16 PM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM

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