need help with C++ program

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

sleepy2020

Thread Starter
Joined
Jul 23, 2003
Messages
6
I am taking a introductory course in C++. and was assigned a project to get the average temperature for each month of the year and display the month and the temperature of the hottest month by using arrays I am having trouble returning the subscript from the array and using it to display the month from the month array. here is a copy of the function.


double
greatestTemperature(double temperature[],string month[], int numberMonths)



{

int kk;
double greatest = temperature[0];
for (kk = 0; kk < numberMonths; ++kk)
{
if (temperature[kk] > greatest)
greatest = temperature[kk];

}




cout << greatest;

return greatest;

}




can anyone tell me what i am doing wrong?
 

sleepy2020

Thread Starter
Joined
Jul 23, 2003
Messages
6
how do i return the subscript for temperature[kk]
and use it to get the month from the array month?
 
Joined
Nov 24, 2002
Messages
2,438
You could make your function return a type int instead of the double. Then declare your array outside the function then pass into the function the address of the start of the array and work from that. Instead of returning the temperature itself, return an int which holds the array subscript of the greatest temperature. As everything you need has been defined outside your function you will then be able to refer to that array element outside of it too.

Your code could look something like this:

First some stuff needed to use the function. Put this stuff inside your main().
Code:
float temperatures[12];
for (int loop = 0; loop < 12; ++loop) temperatures[loop] = 0.0;

int max_month = 0;
int number_months = 0;

// insert code to set number of months here

// Subscript value for maximum selected from inputs is passed out here
// Note that parameter "temperatures" is address of start of array!!!
max_month = greatestTemperature ( temperatures, number_months );

cout << "greatest temperature is: " << temperatures[greatest] << endl
       << "position of max in array is: " << greatest + 1 << endl ;
Function code is here:
Code:
int greatestTemperature(float temp_array[12] , int numberMonths)

// The parameter "float temp_array[12]" will take in the address of our
// already created array of floats, our "temperatures[]", and use it as
// the start address of temp_array[].  Any values put into these
// addresses will then remain to be accessed outside the function

{
   float next_temp = 0.0;
	
   int greatest = 0; // array subscript for greatest value of temperature

   // loop control variable can be declared at the
   // start of the loop if you want ;)
   for (int kk = 0; kk < numberMonths; ++kk)
   {
      cout << "next temperature value please: " ;
      cin >> next_temp ;

      temp_array[kk] = next_temp;

      if (temp_array[kk] > temp_array[greatest]) greatest = kk;
   }

   return greatest ;
}
I have assumed we will get our temperature values from the keyboard.

As a tip, if you post code here, in order to preserve the formatting and indenting to make it easier to follow, just stick it between two {code} {/code} tags, but with square brackets in place of the braces I have had to use to display it. ;)
 

sleepy2020

Thread Starter
Joined
Jul 23, 2003
Messages
6
thank you everyone

I now understand what it is that I was doing wrong and how to fix it. :) :) :)
 
Joined
Nov 24, 2002
Messages
2,438
Glad you got it sleepy!

One mistake I made, :eek:

cout << "greatest temperature is: " << temperatures[greatest] << endl
<< "position of max in array is: " << greatest + 1 << endl ;

in the 'main' suggestion should read:

cout << "greatest temperature is: " << temperatures[max_month] << endl
<< "position of max in array is: " << max_month + 1 << endl ;

as we passed the value out of our function into max_month didn't we! My apologies. :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top