C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2012, 06:08 AM
Jobooo Jobooo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 Jobooo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old December 8th, 2012, 06:38 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.

Reply With Quote
  #3  
Old December 8th, 2012, 03:57 PM
Jobooo Jobooo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 Jobooo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old December 8th, 2012, 06:01 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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?
		}
		

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help with Static 2D Array Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap