Trouble w/ 2d array

This is a discussion on Trouble w/ 2d array within the C Programming forums, part of the General Programming Boards category; For this program, I"m trying to print a table of random grades of double value between 50-100, listing each in ...

  1. #1
    Zaz
    Zaz is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Trouble w/ 2d array

    For this program, I"m trying to print a table of random grades of double value between 50-100, listing each in the columns for the various assignments. When I compile my code, I get an error "'%6.2f' expects type 'double', but argument 2 has type 'double (*)[6]'"

    Code:
    /* 
     * File:   Assignment2.c
     * Author: Anthony
     *
     * Created on November 18, 2009, 8:45 AM
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main() {
        double grade[15][6];
        double total[15];
        double average[7];
        int i;
        int j;
    
        printf("\t\tHW1\tHW2\tQuiz1\tQuiz2\tMidterm\tFinal");
    
        for(i=0; i<15; i++)
          {
    
    	for (j=0; j<6; j++)
                {
                grade[i][j] = 50.0+rand() % 50;
                printf("%6.2f\n", grade);
                                  }
                 }
    
       
        system("pause");
    
    }
    Any idea as to how I would fix this error and print each value in the arrays?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you code
    Code:
    printf("%6.2f\n", grade);
    you are telling the compiler that "grade" is a double (er, float - but that's the not the point here). But, in fact, "grade" is a pointer to a 2 dim array of doubles.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    1
    what you are trying to print there is the pointer of the array for it to work correctly dont forget to print it like this.

    printf("%6.2f\n", grade[i][j]);

    so it points to the correct index of your 2D array. I hope that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from a 2d array
    By roaan in forum C Programming
    Replies: 5
    Last Post: 09-04-2009, 12:42 AM
  2. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 04:50 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

Tags for this Thread


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