Hi, I am having trouble with a C++ problem for school. Im writing a program that takes input for the highest and lowest temperatures of each month, and stores them in a 2 dimensional array. I then wrote 2 fuctions to find the average high and low temps. Part of my program is to write a function. Those work fine but the problem then goes on to state that I need 2 functions that returns the INDEX of the high and low temps. Im using my colleges "dos style" environment to program in and it does not let me copy and paste outside of that program but here is what im thinking for the first function:

int indexHighTemp(int YrTemp [12][2])
{
int highTempIndex;     //place were I will store the index position
int x;
int highTemp;     //place where i will store the high temp

highTemp = 0;  
    for(x = 0; x < 12; x++)
     {
        if(highTemp >= YrTemp[x][0])
           highTemp = YrTemp[x][0];
     }
     cout << highTempIndex << endl;
}

 //I think this function will give me the high temperature but I dont know about the index.

If anyon could help me out a little, i would greatly appreciate it :)

Recommended Answers

All 3 Replies

Index would be similar

if(highTemp >= YrTemp[x][0])
      {
           highTemp = YrTemp[x][0];
           highIndex = x; // set index to be returned
     }

>>Im using my colleges "dos style" environment to program in and it does not let me
>>copy and paste outside of that program
You mean something like Turbo C++? Well, every compiler stores the while somewhere or the other. You you can definitely copy-paste the code if you are able to find where those files are stored.

I think you should be switching to a new compiler: http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-use-old-c.html

Well regarding the problem in hand, you've already got the solution above. A slight advice which I would like to give if you respect that:
1. Rather than defining then assigning, define and initialize variables: As in Line 5 you define the highTemp and then on line 7 you assign value zero to it. Better thing to do would be to initialize highTemp on line 5 itself:

int highTemp=0;

Same mistake has been repeated on line 4 and 8 for the variable x. You should have been initializing it on line 4 itself.
2.Another thing to note is that among all the temperatures in the array, there will be at least one highest temperature. So rather than initilizeing highTemp to zero, you should have been initilized it with the first element of the array

int highTemp=YrTemp[0][0];
    for(x = 1; x < 12; x++) //not that I am starting the counter from 1
     {
        if(highTemp >= YrTemp[x][0])
           {
                      highTemp = YrTemp[x][0];
                      highIndex = x;
           }
     }

Affect of these two things would be very insignificant for now but it will be reflected when the amount of data is huge. Nonetheless it is a good programming practice.

Hey thanx alot guys!!! you have been very helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.