Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
December 8th, 2012, 06:08 AM
 Jobooo
Registered User

Join Date: Dec 2012
Posts: 2
Time spent in forums: 33 m 5 sec
Reputation 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
December 8th, 2012, 06:38 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

#3
December 8th, 2012, 03:57 PM
 Jobooo
Registered User

Join Date: Dec 2012
Posts: 2
Time spent in forums: 33 m 5 sec
Reputation 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;
}```

Regards,

Jobooo

#4
December 8th, 2012, 06:01 PM
 BobS0327
Contributing User

Join Date: Oct 2012
Posts: 118
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
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?
}
```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Need help with Static 2D Array Problem