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



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 5th, 2012, 10:43 PM
forsakensun12 forsakensun12 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 forsakensun12 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m
Reputation Power: 0
C++ help with output

I need help with a program for class. The program runs but the answer isnt right at all.

Heres the directions.... Write a program that usses a 2-D array to store the highest and lowest temp for each month of the year. The program should output the average high, average low, and the highest and lowest temps for the year.

It runs and i know the answer is wrong but cant figure out what line is incorrect. The answer i get is the average high: 136627563.00 and the low: -23765427.... But the low should be: 48... and the high should be: 61...

Heres my input data:

27 38
29 42
35 50
45 61
54 71
64 79
69 84
68 83
61 75
50 64
42 54
32 32

And heres the code.....

Code:
#include<iostream>
#include<fstream>
#include<iomanip>

const int NO_OF_MONTHS = 12;
const int rows = 12;	 //make # of rows/cols a constant int
const int cols = 2;


using namespace std;

void getData( int listTemp[rows][cols]);
void averageHigh( int listTemp[rows][cols]);
void averageLow(int listTemp[rows][cols]);
void indexHighTemp(int listTemp[rows][cols]);
void indexLowTemp( int listTemp[cols][cols]);

ifstream infile;

int main()
{
		
	
  //  ofstream outfile;
	double average;
	int listTemp[rows][cols];
	
	
	infile.open("data.txt"); //open infile
	
	   if (!infile)
    {
               cout << "Error opening data.txt " << endl;
               return 1;
    }

	
   cout<<fixed<<showpoint;   //result will be able to show decimal points
   cout<<setprecision(2);
	
	
	
	getData(listTemp);	 //call functions
	averageHigh(listTemp);
	averageLow(listTemp);
	indexHighTemp(listTemp);
	indexLowTemp(listTemp);
	
	
	  
	infile.close();

	
	system("pause");
	return 0;
	
}

	void getData(int listTemp[rows][cols])
	{													 //read data in both arrays
	
			 int x;
			 int j;
			// ifstream infile;

						 
			 
			 for (x=0; x < rows; x++)
			 {
				 for( j=0; j<cols; j++)
				 {
					  
			 infile >> listTemp[x][j] >> listTemp[x][j];
			 

		   
					 
		   }
		   }
}
	void averageHigh ( int listTemp[rows][cols]) 
	{
			int x=1;		 
			int Sum = 0;								//read first column and find the average
			double Average;							//from the highs	   
			
			
			 for (x=0; x < rows; x++)
			 {
			 Sum = listTemp[x][0] + Sum;			
			// Average = Sum/x;			 
			 }
	   // Average = Sum/12;
		
		Average = Sum/x;
		
	   
	   
		cout << "Average high for the year: " << Average << endl<<endl;
		
		  
		}
		
	void averageLow ( int listTemp[rows][cols])
	 {
	 int Sum = 0;											//read the second column and find the
	 double Average;	   
									  //average of the lows	   
	 
			 for (int x=0; x < rows; x++)
			 {
			 Sum = listTemp [x][1] + Sum;
			 }
			 //  Average = Sum/12;
			 
			 Average = Sum/12;
	 
			 cout << "Average low for the year: " << Average << endl<<endl;
	 }
	 
	 
		
	void indexHighTemp ( int listTemp[rows][cols])
	 {
	 int highestIndex = 0;  
		
														//find highest in the "high" column					  
	 for(int x = 0; x < rows; x++)
	 {
	  if(listTemp[0][x] > highestIndex)
	  highestIndex = listTemp[0][x];
			 
	 }
	 cout << "The index high temperature is " << highestIndex <<endl<<endl;

	 }
	 
	void indexLowTemp ( int listTemp[rows][cols])
	 {
	 int lowestIndex = 0;	  
							  //find the lowest in the low column
	 
	 for(int x = 0; x < rows; x++)

	 {
		  if(lowestIndex > listTemp[0][x])
		  lowestIndex = listTemp[0][x];
			 
	 }		
	 cout << "The index low temperature is " << lowestIndex << endl<<endl;
	 }

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ help with output


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 - 2014, Jelsoft Enterprises Ltd.

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