The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need help with Static 2D Array Problem
Discuss Need help with Static 2D Array Problem in the C Programming forum on Dev Shed. Need help with Static 2D Array Problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 8th, 2012, 06:08 AM
|
|
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
|

December 8th, 2012, 06:38 AM
|
|
|
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.
|

December 8th, 2012, 03:57 PM
|
|
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;
}
I'm confused as to why the program is not displaying the required output. Please help.
Regards,
Jobooo
|

December 8th, 2012, 06:01 PM
|
|
|
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?
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|