Thread: Trouble w/ 2d array

  1. #1
    Registered User
    Join Date
    Oct 2009

    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]'"

     * 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;
        for(i=0; i<15; i++)
    	for (j=0; j<6; j++)
                grade[i][j] = 50.0+rand() % 50;
                printf("%6.2f\n", grade);
    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
    Katy, Texas
    When you 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
    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-03-2009, 11:42 PM
  2. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03: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, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

Tags for this Thread