1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

need help with C++ program

Discussion in 'Software Development' started by sleepy2020, Apr 23, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. sleepy2020

    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?
     
  2. mussavcom

    mussavcom

    Joined:
    Mar 28, 2004
    Messages:
    51
    It looks like it should work. What's going wrong?
     
  3. sleepy2020

    sleepy2020 Thread Starter

    Joined:
    Jul 23, 2003
    Messages:
    6
    if I return kk it gives me the last month
     
  4. mussavcom

    mussavcom

    Joined:
    Mar 28, 2004
    Messages:
    51
    That's because kk will have been incremented to 11... you're returning greatest.
     
  5. sleepy2020

    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?
     
  6. AlbertB

    AlbertB

    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. ;)
     
  7. sleepy2020

    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. :) :) :)
     
  8. AlbertB

    AlbertB

    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. :)
     
  9. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/223331

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice