Thread: 2D Arrays

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    2D Arrays

    Code:
    #include <stdio.h>
     
    int main()
    {
        float array[4][5] = 
            {{70, 51, 65, 83, 85}, {83, 65, 67, 71, 91},
            {90, 70, 71, 77, 80}, {92, 85, 88, 80, 79}};
         
        float rowTotal = 0;
        float allRowsTotal = 0;
        float maxAverageRowIndx = 0;
        float rowCounter = 0;
        float columnCounter = 0;
        float maxRowAverage = 0;
        float rowAverage = 0;
         
        for (rowCounter = 0; rowCounter < 4; rowCounter++)
        {
            rowTotal = 0;
            for (columnCounter = 0; columnCounter < 5; columnCounter++)
            {
                rowTotal += array[rowCounter][columnCounter];
                allRowsTotal += rowTotal;
            }
             
            rowAverage = rowTotal / 5;
            if (rowAverage > maxRowAverage)
            {
                maxRowAverage = rowAverage;
                maxAverageRowIndx = rowCounter;
            }
            
            printf("Row average = %.1f\n", rowAverage);
        }
        printf("The highest average was found in row %.1f with the value: %.1f\n",
            maxAverageRowIndx + 1, maxRowAverage);
      
    return 0;
    }



    This is the program so far and this is what I want to output in the end:

    Sample Input :
    70 51 65 83 85
    83 65 67 71 91
    90 70 71 77 80
    92 85 88 80 79

    Output :
    ............A...B..C..D..E..Average
    ************************************************** ****************
    Fall.......70 51 65 83 85 70.8
    Winter...83 65 67 71 91 75.4
    Spring...90 70 71 77 80 77.6
    Summer.92 85 88 80 79 84.8
    ************************************************** ****************
    Average of all terms = 77.15.
    Largest average is for Summer term = 84.8

    Please help me complete this program, thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Move line 23 to line 25. I'd prefer int's for rowCounter and columnCounter, and doubles for all the variables that really need floating point data types.

    floats were a big compromise to save space - not really up to snuff with their accuracy.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thanks, do you know how I can make the output have numbers after the decimal for the averages?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use this printf() format:
    Code:
    printf("%5.2f",rowAverage);
    That will give you a field 5 chars wide, with 2 digits printed after the decimal place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM