#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Need help with Static 2D Array Problem


    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

    This is what I have done so far:

    #include <stdio.h>

    int main()
    {
    int array[4][5] =
    {{70, 51, 65, 83, 85}, {83, 65, 67, 71, 91},
    {90, 70, 71, 77, 80}, {92, 85, 88, 80, 79}};

    int rowTotal = 0;
    int allRowsTotal = 0;
    int maxAverageRowIndx = 0;
    int rowCounter = 0;
    int columnCounter = 0;
    float maxRowAverage = 0;
    float rowAverage = 0;
    float allRowsAverage = 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 = %f\n", rowAverage);
    }
    allRowsAverage = allRowsTotal / 4;
    printf("The overal average is: %f\n", allRowsAverage);
    printf("The highest average was found in row %d with the value: %f\n",
    maxAverageRowIndx + 1, maxRowAverage);

    return 0;
    }

    The problem is that I don't know how to proceed to get the required output. This is as far as I was able to get and even so, the program is not correct and so I was hoping you guys would be able to help me out.

    Regards,
    Jobooo
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    You are dividing integers between integers. The result will be an integer. If you assign that integer to a floating point variable (use double rather than float) there will be a conversion, but the lost decimals are not regained.

    Code:
    double x = 17 / 4; /* x = 4 */
    double y = 17 / 4.0; /* y = 4.25 */
    Also to calculate the average of all values you need to divide by the number of all values; not the number of rows.

    And ... adding to allRowsTotal inside the loop is adding the first terms more times than the last terms.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Code:
    #include <stdio.h>
    
    int main()
    {
            int array[4][5] = 
            {{70, 51, 65, 83, 85}, {83, 65, 67, 71, 91},
            {90, 70, 71, 77, 80}, {92, 85, 88, 80, 79}};
    
            int rowTotal = 0;
            int allRowsTotal = 0;
            int maxAverageRowIndx = 0;
            int rowCounter = 0;
            int columnCounter = 0;
            float maxRowAverage = 0;
            float rowAverage = 0;
            float allRowsAverage = 0;
    
            char title[4][20] = {"Fall", "Winter", "Spring", "Summer"};
    
            printf("%-16s%-3s %-3s %-3s %-3s %-3s %s\n", " ", "A", "B", "C", "D", "E", "Average");
            printf("*****************************************\n");
            printf("%-14s", title[rowCounter]);
            printf("%3d", array[rowCounter][columnCounter]);
            printf("%-3s%4.1f\n", " ", rowAverage);
            printf("*****************************************\n");
            printf("Average of all terms %3.1f\n", allRowsAverage);
            printf("Largest average is for %s term %3.1f\n",
            title[maxAverageRowIndx],
            maxRowAverage); 
    
            for (rowCounter = 0; rowCounter < 4; rowCounter++)
            {
                    rowTotal = 0;
            for (columnCounter = 0; columnCounter < 5; columnCounter++)
            {
                    rowTotal += array[rowCounter][columnCounter];
            }
    
            allRowsTotal += rowTotal;
            rowAverage = rowTotal / 5.0f;
            if (rowAverage > maxRowAverage)
            {
                    maxRowAverage = rowAverage;
                    maxAverageRowIndx = rowCounter;
            }
    
            printf("Row average = %f\n", rowAverage);
            }
            allRowsAverage = allRowsTotal / 20.0f;
            printf("The overal average is: %f\n", allRowsAverage);
            printf("The highest average was found in row %d with the value: %f\n",
            maxAverageRowIndx + 1, maxRowAverage);
    
            return 0;
    }
    I'm confused as to why the program is not displaying the required output. Please help.

    Regards,

    Jobooo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    186
    Rep Power
    82
    Unfortunately, your code requires a fair amount of rewriting to display the correct results. So, I've posted the following snippet in an attempt to get you on the right track towards successfully completing your assignment.

    Code:
    printf("%-16s%-2s %-2s %-2s %-2s %-2s %s\n", " ", "A", "B", "C", "D", "E", "Average");
    	printf("*****************************************\n");
    	//	printf("%-14s", title[rowCounter]);
    	//	printf("%3d", array[rowCounter][columnCounter]);
    	//	printf("%-3s%4.1f\n", " ", rowAverage);
    	//	printf("*****************************************\n");
    	//	printf("Average of all terms %3.1f\n", allRowsAverage);
    	//	printf("Largest average is for %s term %3.1f\n",
    	//		title[maxAverageRowIndx],
    	//		maxRowAverage); 
    
    	for (rowCounter = 0; rowCounter < 4; rowCounter++)
    	{
    		printf("%-14s", title[rowCounter]);
    		rowTotal = 0;
    		for (columnCounter = 0; columnCounter < 5; columnCounter++)
    		{
    			printf("%3d", array[rowCounter][columnCounter]);
    			rowTotal += array[rowCounter][columnCounter];
    		//	allRowsTotal += rowTotal;
    		}
    		if(columnCounter == 5)
    		{
    			if(rowTotal > hightotal)
    			{
    				// What statements belong here?
    			}
    			// What statements belong here?
    		}

IMN logo majestic logo threadwatch logo seochat tools logo