Thread: array array and more arrays

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    array array and more arrays

    I want to print the following prices to screen:
    10.95 16.32 12.15
    8.22 15.98 26.22
    13.54 6.45 18.59

    here is the code
    Code:
    #include <stdio.h>
    int main()
    {
    #define NUMROWS 3
    #define NUMCOLS 3
      float prices[NUMROWS] [NUMCOLS] = {10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, 18.59};
      int i, j;
    
         printf("\n%.2f %.2f %.2f", prices[0][0], prices[0][1], prices[0][2]);
         printf("\n%.2f %.2f %.2f", prices[1][0], prices[1][1], prices[1][2]);
         printf("\n%.2f %.2f %.2f", prices[2][0], prices[2][1], prices[2][2]);
         for (i = 0; i < NUMROWS; i++)
           {
    	 printf("\n");
    	 for (j - 0; j < NUMCOLS; j++)
    	 printf("%.2f ", prices[i][j]);
           }
         printf("\n");
    
         return 0;
    }
    sorry, i got it
    Last edited by wonderpoop; 12-20-2006 at 06:09 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Code:
    float prices[NUMROWS] [NUMCOLS] = 
    { { 10.95, 16.32, 12.15 } , { 8.22, 15.98, 26.22 }, { 13.54, 6.45, 18.59 } };

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I presume you got this.
    Code:
    for (j - 0; j < NUMCOLS; j++)
    '-' -> '='

    BTW, instead of printing a newline before the second for loop and after both for loops, you could print a single newline after the second for loop.
    Code:
    for(x = 0; x < X; x ++) {
        for(y = 0; y < Y; y ++) {
            /* ... */
        }
        putchar('\n');
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed