I am recently new to C++ and I wondered if someone could show me how to modify my program below. I want the program to test if there is more than 1 day that has the highest rainfall and susequently to output those corresponding days. Than you :)

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

double rain[] = {18.0, 18.0, 3.2, 12.1, 14.6, 17.8, 3.2};

int main ()
{
	double rainiestValue (0.0);
	int rainyDayNo (-1);

	for (int dayNo ( 0); dayNo < 7; ++dayNo)

		if (rainiestValue < rain [dayNo])


		{
			rainiestValue = rain [dayNo];
			rainyDayNo = dayNo;
		}
		cout << fixed << setprecision (2);
		cout << "\nThe highest rainfall is: " << rainiestValue << "mm" << " on day " << rainyDayNo;


		while (!_kbhit());
		return 0;
}

Recommended Answers

All 13 Replies

Looks like you could do it with another if and a counter variable. Just make sure that the second if is also part of the for loop.

And please use [CODE=syntax] ... code tags ... [/code]. They make your code easier to read. I suggest you read an article about formatting as well. I'm sure WaltP will be along soon to fix your tags, you can thank him for that and for the blog then.

first of all u should warp tags around your code. its vital no one would help u like this and ur post is misty. go to tags. do it quickly.

commented: It's also vital to us English on these forums. U is not a word, neither is UR. -2

What they said^^.
Here's one way, may not be the best:
Just looking at what you need to accomplish, you can use two "for" loops.

Set a double (say maxRain) equal to the first number in the array, then use the first for loop to cycle through and compare the current value in the array to maxRain, if the value in the array is greater than maxRain, set that value equal to maxRain.

At this point, your variable maxRain will equal to the highest value of rainfall.

Use your second loop to cycle through the array to see if any are equal to maxRain. I'll give you a hint, here's what I had in my for loop:

if(rain[i]==maxRain)
    cout << "\nThe highest rainfall is: " << maxRain << "mm" << " on day " << i+1;

Good Luck

I am recently new to C++ and I wondered if someone could show me how to modify my program below. I want the program to test if there is more than 1 day that has the highest rainfall and susequently to output those corresponding days. Than you :)

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

double rain[] = {18.0, 18.0, 3.2, 12.1, 14.6, 17.8, 3.2};

int main ()
{
    double rainiestValue (0.0);
    int rainyDayNo (-1);

    for (int dayNo ( 0); dayNo < 7; ++dayNo)

        if (rainiestValue < rain [dayNo])


        {
            rainiestValue = rain [dayNo];
            rainyDayNo = dayNo;
        }
        cout << fixed << setprecision (2);
        cout << "\nThe highest rainfall is: " << rainiestValue << "mm" << " on day " << rainyDayNo;


        while (!_kbhit());
        return 0;
}

end quote.

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
double rain[] = {18.0, 18.0, 2.0, 18.0, 18.0, 3.0, 18.0};
int MAX;
int main (){
    double rainiestValue (0.0);
    int rainyDayNo (-1);

    for (int dayNo ( 0); dayNo < 7; ++dayNo)

        if (rainiestValue < rain [dayNo])
        {
            rainiestValue = rain [dayNo];
        }

    for (int dayNo ( 0); dayNo < 7; ++dayNo)

        if (rainiestValue == rain [dayNo])
        {
            MAX++;
        }

    cout << rainiestValue <<  MAX;


    while (!_kbhit());
    return 0;
}

Apologies for not following the above. I am a first year student and my programming abilities are poor. I think I have followed your instructions correctly now. I have bben working on the program but I think I have gone off on a tangent with 'not very promising results.' Any further help would be appreciated. Thank you.

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

double rain[] = {18.0, 18.0, 2.0, 18.0, 18.0, 3.0, 18.0};
int MAX;
int main ()
{
    double rainiestValue (0.0);
    int rainyDayNo (-1);

    for (int dayNo ( 0); dayNo < 7; ++dayNo)

        if (rainiestValue < rain [dayNo])
        {
            rainiestValue = rain [dayNo];
        }

        for (int dayNo ( 0); dayNo < 7; ++dayNo)

            if (rainiestValue == rain [dayNo])
            {
                MAX++;
            }

            cout << rainiestValue <<  MAX;  





            while (!_kbhit());
            return 0;}

Get rid of line 11, it does nothing for you, you don't use rainyDayNo anywhere else in your program.

Also, you are probably getting very "weird" numbers like 92561432 for "MAX". This is because you never initialize MAX to 0 before you use it, so the value is what is known as "garbage". You should ALWAYS initialize variables before you do anything with them. If you don't, you don't know what you'll get.

In going with that Fbody said above, initialize your MAX value to rain[0]. That will solve what he is saying and save you a line or two as well.

Set MAX equal to the first number in the array rain (MAX=rain[0]). Don't forget that arrays store information in the [0] slot as well. From there, you can start at 1, and go all the way to 7 and compare the current value in the array to your value for MAX, if it is larger, then you can set MAX=rain[whatever_integer_you_designate_in_the_for_loop].

The in your second for loop, you can start at rain[0], and if it equals MAX, then use something similar to what I posted above. (I initialized my int to be "i")

If I were you, I'd familiarize myself with for loops. They are an excellent tool to go through arrays.

Hint from above:

if(rain[i]==maxRain)
    cout << "\nThe highest rainfall is: " << maxRain << "mm" << " on day " << i+1;

In going with that Fbody said above, initialize your MAX value to rain[0]. That will solve what he is saying and save you a line or two as well.

Set MAX equal to the first number in the array rain (MAX=rain[0]). Don't forget that arrays store information in the [0] slot as well. From there, you can start at 1, and go all the way to 7 and compare the current value in the array to your value for MAX, if it is larger, then you can set MAX=rain[whatever_integer_you_designate_in_the_for_loop].

The in your second for loop, you can start at rain[0], and if it equals MAX, then use something similar to what I posted above. (I initialized my int to be "i")

If I were you, I'd familiarize myself with for loops. They are an excellent tool to go through arrays.

Hint from above:

if(rain[i]==maxRain)
    cout << "\nThe highest rainfall is: " << maxRain << "mm" << " on day " << i+1;

Make sure you check usage and context before you make suggestions. As drew has written the program, MAX is the count of days that had rainfall equivalent to the highest rainfall total. It is NOT the maximum rainfall value in the series as you seem to have assumed. The variable "rainiestValue" is that number. Despite the naming and display issues, the usage and process are correct, drew just forgot to initialize MAX before doing anything with it.

As such, the use of "MAX = rain[0]" is incorrect and will lead to invalid results, especially when you consider that you would be truncating a double to an int and losing accuracy/data. MAX MUST be initialized to 0 so that the counter works correctly.

@OP
To correct the issue in question, all you need to do is add "= 0" to the declaration of MAX on line 7. I would advise you to change the name of the variable to something more descriptive such as "maxValueCount" while you are adding that.

Make sure you check usage and context before you make suggestions. As drew has written the program, MAX is the count of days that had rainfall equivalent to the highest rainfall total. It is NOT the maximum rainfall value in the series as you seem to have assumed. The variable "rainiestValue" is that number. Despite the naming and display issues, the usage and process are correct, drew just forgot to initialize MAX before doing anything with it.

As such, the use of "MAX = rain[0]" is incorrect and will lead to invalid results, especially when you consider that you would be truncating a double to an int and losing accuracy/data. MAX MUST be initialized to 0 so that the counter works correctly.

@OP
To correct the issue in question, all you need to do is add "= 0" to the declaration of MAX on line 7. I would advise you to change the name of the variable to something more descriptive such as "maxValueCount" while you are adding that.

In my version I initialized my value to rain[6], but I also created it as a double. I did not initialize his value to 0 and it still worked however.

drew (nice name. it's mine too):

-If you're going to do your first loop as you do, you'll want to include dayNo<= 7, so you don't miss the last day due to your pre-incrementation in the loop.

-I'd revise your second for loop. Currently, if you go through your loop, you're only increasing your MAX value if rainiestValue is equal to the amount of rain on that day. Thus, if the "if" statement is false, it will not increment MAX and your next day that is equal to rainiestValue will simply be MAX++ instead of MAX+2. Also, add your cout statement within the "if" statement brackets, this way it will only output the statement if there is a max amount of rain on the specific day.
How to fix this:: Get rid of MAX. You can accomplish what you need to do with your integer that you initialize in your for loop (after all, you did name it dayNo ;)). Just remember that you will have to put dayNo+1 in your cout statement, since arrays start at 0, but your day number starts at 1.

commented: Ugh, no more warnings. Pay attention to the poster's algorithm. Yet again you have provided incorrect advice. +0

In my version I initialized my value to rain[6], but I also created it as a double. I did not initialize his value to 0 and it still worked however.

drew (nice name. it's mine too):

-If you're going to do your first loop as you do, you'll want to include dayNo<= 7, so you don't miss the last day due to your pre-incrementation in the loop.

-I'd revise your second for loop. Currently, if you go through your loop, you're only increasing your MAX value if rainiestValue is equal to the amount of rain on that day. Thus, if the "if" statement is false, it will not increment MAX and your next day that is equal to rainiestValue will simply be MAX++ instead of MAX+2. Also, add your cout statement within the "if" statement brackets, this way it will only output the statement if there is a max amount of rain on the specific day.
How to fix this:: Get rid of MAX. You can accomplish what you need to do with your integer that you initialize in your for loop (after all, you did name it dayNo ;)). Just remember that you will have to put dayNo+1 in your cout statement, since arrays start at 0, but your day number starts at 1.

You are still not paying attention to the OP's algorithm and/or requirements...

Pre-Increment and Post-Increment make no difference to a for loop. They both occur after the loop iterates, regardless of the specific operation. The only difference is that the pre-increment form is marginally more efficient. If the OP runs from 0-7, they'll overrun their array. It is correct as written.

The second for loop is also correct. MAX is NOT the index of the day with the highest rainfall amount, nor is it intended to be. IT IS A COUNTER. It is intended to track how many days had that same amount of rainfall. For the array of values provided by the OP, the value in the variable at the end of the program should be 5 because there were 5 days with the maximum rainfall total of 18.0.

I want the program to test if there is more than 1 day that has the highest rainfall and susequently to output those corresponding days.

Each corresponding day with the max rainfall, or the corresponding amount of days that has the max rainfall?

Each corresponding day with the max rainfall, or the corresponding amount of days that has the max rainfall?

Ok, I'll give you that, I forgot that from the starting post. But, if you look at his algorithms, he is trying to count the number of days. I think some additional clarification from the OP is required. If my suggestions concerning the usage of MAX are incorrect, I apologize for the error.

However, your for loop structure suggestion is still incorrect.

Ambiguity FTL ha. Ignore my suggestions about what is contained within the parenthesis of the for loop, just ensure that you cover all of the values within the array. I apologize for the misinformation.

If you want each corresponding day with the rainfall, my suggestion about dayNo+1 in the cout instead of incrementing MAX.

If you want the total max amount of days, disregard everything I've said and I apologize for the misleading information.

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.