Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters
Register

Reply
 
LinkBack Thread Tools
Old 29.03.2006, 00:06   #1
Младенец
 
imjustagirl's Avatar
 
Join Date: 03 2006
Location: san fan
Age: 36
Posts: 15
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default rain

girls should not take c++ but can some one help

Do not use global variables
i need to calculate total rainfall
average rain fall
and show highest and lowest month

i cant seem to get the total or the highest or lowest to display any help thank you.






#include <iostream>
#include <string>
using namespace std;

int getActualRain(int j);
string print_month(int month);
//char January, February;


int main()

{
int i;
float AverageRain[13];
float ActualRain[13];


for (i = 1; i < 13; i++)
{
cout << "Enter Monthly Rainfall for " << print_month(i) << ": ";
cin >> AverageRain[i];
}
double TotalMonths,
monthly;
int total = 0;

for(int currMonth = 0; currMonth < TotalMonths; currMonth++)
{
total = total + ActualRain[currMonth];

}


return 0;
}

string print_month(int month)
{
string result;
switch (month)
{
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
cout << "Invalid Value!\a";
break;
}
return result;
Reply With Quote
Old 29.03.2006, 00:19   #2
Easy rider
 
Silver's Avatar
 
Join Date: 11 2005
Location: tristeza
Age: 32
Posts: 1,082
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

Can you use classes?
Reply With Quote
Old 29.03.2006, 00:36   #3
...overwined...
 
noone's Avatar
 
Join Date: 03 2003
Location: ...tortuga...
Posts: 3,429
Downloads: 3
Uploads: 0
Reputation: 158 | 3
Default Re: rain

something like this?...

Code:
   int main()
   {
   double ActualRain[13];
    double AverageRain=0,TotalRain=0,LowestRain=MAX_DOUBLE,HighestRain=0;
   int LowestMonth=1,HighestMonth=1;
  
   for(int i = 1; i < 13; i++)
   {
     cout << "Enter Monthly Rainfall for " << print_month(i) << ": ";
     cin >> ActualRain[i];
   }
 
   for(i = 1; i < 13; i++)
   {
     TotalRain += ActualRain[i];
     if( ActualRain[i] > HighestRain ) { HighestRain = ActualRain[i]; HighestMonth = i; }
     if( ActualRain[i] < LowestRain ) { LowestRain = ActualRain[i]; LowestMonth = i; }
   }
   
   AverageRain = TotalRain / 12;
 
     cout << "total: " << TotalRain << endl;
     cout << "average: " << AverageRain << endl;
     cout << "highest month: " << print_month(HighestRain) << endl;
     cout << "lowest month: " << print_month(LowestRain) << endl;
   
   return 0;
   }
Reply With Quote
Old 29.03.2006, 01:00   #4
Easy rider
 
Silver's Avatar
 
Join Date: 11 2005
Location: tristeza
Age: 32
Posts: 1,082
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

Not exactly C++, but well a quick dirty solution

Code:
// Rain.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>


std::string getMonthName(int);
float getTotal(float*, const int);
int getMinMonth(float*, const int);
int getMaxMonth(float*, const int);
float getAvg(float total, const int nMonths);


int _tmain(int argc, _TCHAR* argv[])
{
	const int nMonths = 12;
	float rain[nMonths];

	for (int i=0; i<nMonths; ++i)
	{
		std::cout << "Enter Monthly Rainfall for " << getMonthName(i) << ": ";
		std::cin >> rain[i];
	}

	std::cout << "Total: " << getTotal(rain, nMonths) << std::endl;
	std::cout << std::endl;
	std::cout << "Min month: " << getMonthName(getMinMonth(rain, nMonths)) << std::endl;
	std::cout << "Max month: " << getMonthName(getMaxMonth(rain, nMonths)) << std::endl;
	std::cout << "Average: " << getAvg(getTotal(rain, nMonths), nMonths) << std::endl;



	return 0;
}

std::string getMonthName(int monthID)
{
	std::string result;
	switch (monthID)
	{
	case 0:
		result = "January";
		break;
	case 1:
		result = "February";
		break;
	case 2:
		result = "March";
		break;
	case 3:
		result = "April";
		break;
	case 4:
		result = "May";
		break;
	case 5:
		result = "June";
		break;
	case 6:
		result = "July";
		break;
	case 7:
		result = "August";
		break;
	case 8:
		result = "September";
		break;
	case 9:
		result = "October";
		break;
	case 10:
		result = "November";
		break;
	case 11:
		result = "December";
		break;
	default:
		result = "";
		break;
	}

	return result;
}

float getTotal(float* data, const int nMonths)
{
	float sum = 0;

	for (int i=0; i<nMonths; ++i)
		sum += *data++;

	return sum;
}

int getMinMonth(float* data, const int nMonths)
{
	float min = *data++;
	int minMonth = 0;
	
	for (int i=1; i<nMonths; ++i, ++data)
	{
		if (min > *data)
		{
			min = *data;
			minMonth = i;
		}
	}

	return minMonth;
}

int getMaxMonth(float* data, const int nMonths)
{
	float max = *data++;
	int maxMonth = 0;
	
	for (int i=1; i<nMonths; ++i, ++data)
	{
		if (max < *data)
		{
			max = *data;
			maxMonth = i;
		}
	}

	return maxMonth;
}

float getAvg(float total, const int nMonths)
{
	return total / static_cast<float>(nMonths);
}
Reply With Quote
Old 29.03.2006, 06:24   #5
Младенец
 
imjustagirl's Avatar
 
Join Date: 03 2006
Location: san fan
Age: 36
Posts: 15
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

thanks for the reply but i cant use the quick solution it needs to be in c++
Reply With Quote
Old 29.03.2006, 06:33   #6
Easy rider
 
Silver's Avatar
 
Join Date: 11 2005
Location: tristeza
Age: 32
Posts: 1,082
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

It is on C++.
Reply With Quote
Old 29.03.2006, 19:20   #7
Младенец
 
imjustagirl's Avatar
 
Join Date: 03 2006
Location: san fan
Age: 36
Posts: 15
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

int _tmain(int argc, _TCHAR* argv[])


keeps givin me an error though
Reply With Quote
Old 29.03.2006, 19:42   #8
Easy rider
 
Silver's Avatar
 
Join Date: 11 2005
Location: tristeza
Age: 32
Posts: 1,082
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

try replacing _tmain(int argc, _TCHAR* argv[]) with main(int argc, _TCHAR* argv[]) or main(). That doesn't play much role here.
Reply With Quote
Old 30.03.2006, 02:44   #9
Какое небо, *, Багдад!
 
knightmare's Avatar
 
Join Date: 10 2005
Location: Ереван
Posts: 1,682
Downloads: 16
Uploads: 0
Reputation: 99 | 3
Default Re: rain

_TCHAR* => char*
Reply With Quote
Old 04.04.2006, 00:05   #10
Младенец
 
imjustagirl's Avatar
 
Join Date: 03 2006
Location: san fan
Age: 36
Posts: 15
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

sorry guys ive been away for a few days enjoying the sunshine what does the std: stand for ?
Reply With Quote
Old 04.04.2006, 08:38   #11
Easy rider
 
Silver's Avatar
 
Join Date: 11 2005
Location: tristeza
Age: 32
Posts: 1,082
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

std is a namespace.
'cout', 'cin' and some more belong to that namespace.
In C++, you can either do 'using namespace std;' or just add 'std::' in front of the classes/functions/variables belonging to that namespace (replace 'std' with any other namespace name you may be using). I prefer the second version, since it helps avoid mess when using different namespaces in the same code. There's a bunch of info on C++ in general and namespaces in particular available on the net and in the books. You might want to look through.
__________________
http://sssilver.googlepages.com/blog...ner_300x30.jpg

http://www.hra.am/img/Gulyan1.gif

You must spread before giving to Silver again.
Reply With Quote
Old 04.04.2006, 19:15   #12
Младенец
 
imjustagirl's Avatar
 
Join Date: 03 2006
Location: san fan
Age: 36
Posts: 15
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: rain

thanks i looked at http://www.cplusplus.com thanks guys for the help
Reply With Quote
Sponsored Links
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 00:02.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.