|
|
highest = getHighest(rainfall, SIZE, SIZE);
double getHighest(double [ ], int, const int&);
|
|
double getHighest(double rainfall[], int SIZE, int & highIndex)
|
|
highIndex = rainfall[count]
?Would it look like highIndex = rainfall[count] ? |
int
double
int
and finally double
double
should be stored in an variable of type int
. Of course that's possible, but it should give an uneasy feeling as it involves discarding the fractional part of the value.I'm pretty confused on what variable the month is even assigned to. Do I need to put another string variable inside this function with the month names and make a for loop specifically for the highIndex to output the string? |
main()
function. Do you see this at line 36:
|
|
highIndex
value, you can use this array of month names, still keeping within the main() function.
|
|
string [months]
variable how do I do that into the int& and keep it in the right format?If I'm trying to pass the string [months] variable how do I do that into the int& and keep it in the right format? |
int &
parameter I think you correctly understand is passed by reference. What that means is that the variable is defined in main() as an ordinary integer. The reason it's used is because in the ordinary way, a function can return only one value, and the function is already using that to return the actual rainfall value. So this integer passed by reference becomes a second value returned from the function, in effect.How do I get it to look at the count from the for loop that comes right before it? Should it be part of that loop in some way? |
|
|
highest
changes, the subscript of the corresponding array element should be stored in our 'integer passed by reference' which I gave the name highIndex
- though you can call it what you like, so long as it is meaningful.highest
is modified in two places, at lines 102 and 108. Simply add a line of code immediately before or after each of those lines, to store the subscript.highIndex
variable is what is confusing me. I understand that I am supposed to link it to the subscript of the months array but am having trouble understanding how exactly to do that.highIndex = months[0];
on line 103 I get an error that says "assigning to 'int' from incompatible type 'string'.highIndex = months[count];
I get the same errorhighIndex = rainfall [count]
but that just ends up returning a numeric value.months
is not even visible, since it is defined in main()
and we are discussing the code inside function getHighest()
highIndex = months[0];
completely. That the compiler gave the message, "assigning to 'int' from incompatible type 'string'" suggests you are really trying too hard, you must have done extra, unnecessary work to get that string visible in there. "what needs to happen is, every time the value stored in highest changes, the subscript of the corresponding array element should be stored in highIndex " |
|
|
|
|
|
|
cout
statement returns 0 instead something like "January
. rainfall[count]
loop. highIndex = 0
line but keeping in the highIndex = rainfall[count];
and that still assigns a 0 to highIndex.
|
|
highest
gets the actual value of rainfall (double), but wouldn't the highIndex
be the index number (int) of the array that held that highest value? Essentially count
, not rainfall[count]
?highIndex
|
|
highIndex = [count]
I get an error that says expected body of a lambda expression
The cout statement returns 0 instead something like "January |
highIndex
is indeed an integer. That's what we expect. It's defined as an integer so naturally if you output it, you will see a number. Though it should take any value in the range 0 to 11 since those are the possible values of the subscript for the array.
|
|
|
|
main()
, not in the function getHighest()
cout << "The month of " << months[highIndex] << " had the highest rainfall." << endl;
Really helped me out and made it click how the highIndex
was supposed to work.
|
|
double
. Instead you are in effect returning the same value via two different routes. Let me quote from one of my own previous posts, then I'll have to leave this for a while:"The int & parameter I think you correctly understand is passed by reference. What that means is that the variable is defined in main() as an ordinary integer. The reason it's used is because in the ordinary way, a function can return only one value, and the function is already using that to return the actual rainfall value. So this integer passed by reference becomes a second value returned from the function, in effect." |