Basically what the title says. I have to find the maximum and minimum values of a set of numbers contained in a file. I also have to find the number of even numbers and the number of odd numbers. Here is my code so far. I get the right values for the maximum and minimum, but for the sum of the even and odd numbers the program says there are 0 for each. I'm thinking it has something to do with how the number of occurrences is stored.
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
int number(0), max(0), min(0), counter, temp_num, sum_evens(0), sum_odds(0);
ifstream infile;
infile.open ("Input.txt");
if (infile.fail ())
{
cout << "There was an error opening the file.\n";
}
while (!infile.fail() && !infile.eof())
{
for (counter = 0; counter < 51; counter++)
{
infile >> number;
temp_num = number;
if (temp_num > max)
{
max = temp_num;
}
elseif (temp_num < min)
{
min = temp_num;
}
}
while (!infile.eof())
{
if (number % 2 == 0)
{
counter = 1;
sum_evens = sum_evens + counter;
counter++;
}
elseif (number % 2 != 0)
{
counter = 1;
sum_odds = sum_odds + counter;
counter++;
}
}
}
cout << "The maximum of the data set is " << max << endl;
cout << "The minimum of the data set is " << min << endl;
cout << "The number of even numbers in the data set is " << sum_evens << endl;
cout << "The number of odd numbers in the data set is " << sum_odds << endl;
infile.close ();
return 0;
}
for (counter = 0; counter < 51; counter++)
{
infile >> number;
temp_num = number;
if (temp_num > max)
{
max = temp_num;
}
elseif (temp_num < min)
{
min = temp_num;
}
}
you are reading all numbers or at least 51 numbers. So there is no any number left for the loop that follows this loop. You should use a single loop insetad of these two.:)
You need to fix your initialization of min. unless you have a number less than zero it will always be 0 (same for your max).
Ok, so you have a for loop that scans through the first 50 numbers in a file and then your second for loop (that has the even/odd check) would start at the 51st character. If you want these checks to be over the entire cache of numbers remove the second for loop and put all the checks in the same loop. Then you could remove the 51st number test and just go till the end of the file.
This is an assignment. I was told to check two conditions in the while loop. I assumed those would be the two conditions to check. I'm using the for loop because that is the only way I know how to "scan" through a set of numbers. The data set is 50 numbers with the max being 83 and the min being -7. The program finds those values correctly. I'm not sure what else I would initialize those min and max to.
Would this be better? I compiled and the program still says the sum for both evens and odds is 0. Should I use a for loop that is sentinel controlled instead of count controlled? Would the expression for the sentinel control be the eof function? If so, how does that work?
I would set the initial min and max values to the first number. That way you know that the max and min will be in the data set.
Also, you are not going to want else if statements there because if the temp_num > max (for instance) it will not trigger the other if statements. You can use one set of if/else ifs for max and min and then another set of if/else ifs for the even/odd tests.
And keeping this all in one loop over the data makes your program much more efficient.
There's no reason for the for loop at all. I understand your assignment calls for you to check two conditions in the while loop.. but that's silly. If you must check two conditions make it:
1 2 3 4 5 6 7 8 9 10
while ( infile && (infile >> number) )
{
// no for loop here
if ( number > max )
max = number ;
if ( number < min )
min = number ;
// ...
}
Don't chain if/else where the conditions are not related or exclusive. A number can be both the smallest number in the file and an even number. Depending on how you initialize min and max, the else may or may not make sense to have there.
I took out the for loop and this is what I have now. Everything seems to work now, but I just have one little problem. The amount of even numbers displays is right, bt the amount of odd numbers is one more than it should be. I think that has to do with the increment operator I'm using in the number % 2 != 0 if statement. I'm not sure how to make it so the value stored in sum_odds still increments, while not having the number of odd numbers displays be one to many.
I suspect you're trying to make too many changes at one time. Instead of saying "Tried that, didn't work", perhaps you could say "Tried this," supply the code you tried and then ask "why isn't this working the way I think it should?"
I tried just using if statements for getting the min and max, but that doesn't seem to work. I t seems like the for loop is necessary
.
What you've done is try it with the for loop and it didn't work and you've tried it without a for loop and it didn't work. All you can say from that, logically speaking, is that: "Everything I've tried hasn't worked."
Here is the complete code again. I think the issue is how I'm initializing min and max. I'm not sure how to set min and max to the first number in the data set and still have the code function properly.
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
int number(0), max(0), min(0), counter, sum_evens(0), sum_odds(0);
ifstream infile;
infile.open ("Input.txt");
if (infile.fail ())
{
cout << "There was an error opening the file.\n";
}
while (!infile.fail() && !infile.eof())
{
infile >> number;
if (number > max)
{
max = number;
}
if (number < min)
{
min = number;
}
if (number % 2 == 0)
{
counter = 0;
counter++;
sum_evens = sum_evens + counter;
}
if (number % 2 != 0)
{
counter = 0;
counter++;
sum_odds = sum_odds + counter;
}
}
cout << "The maximum of the data set is " << max << endl;
cout << "The minimum of the data set is " << min << endl;
cout << "The number of even numbers in the data set is " << sum_evens << endl;
cout << "The number of odd numbers in the data set is " << sum_odds << endl;
infile.close ();
return 0;
}
#include <iostream>
#include <fstream>
#include <limits>
usingnamespace std;
int main ()
{
ifstream infile("Input.txt") ;
if (infile.fail ())
{
cout << "There was an error opening the file.\n";
}
// set max to the lowest possible value for its type.
int max = std::numeric_limits<int>::min() ;
// set min to the highest possible value for its type.
int min = std::numeric_limits<int>::max() ;
// we're not keeping track of the sum of the even and odd numbers,
// so let's change the name to something that reflects what we are
// keeping track of.
int num_evens = 0 ;
int num_odds = 0 ;
while (!infile.fail() && !infile.eof())
{
int number = 0 ;
infile >> number;
// note that infile >> number can fail (which is why I suggested
// using it in your while control condition.)
if (number > max)
max = number;
if (number < min)
min = number;
if ( number != 0 ) // Is 0 even or odd?
{
if ( number % 2 == 0 ) // these two conditions are exclusive,
++num_evens ; // so using if/else here makes sense.
else
++num_odds ;
}
}
cout << "The maximum of the data set is " << max << endl;
cout << "The minimum of the data set is " << min << endl;
cout << "The number of even numbers in the data set is " << num_evens << endl;
cout << "The number of odd numbers in the data set is " << num_odds << endl;
infile.close ();
return 0;
}
I'm not sure what this line of code means. while ( infile && (infile >> number) )
What does infile by itself mean and where did the eof function go?
I think 0 is an even number. I went through the input file and counted 21 even numbers and one of the numbers is 0. The program says there is 21 evens numbers so that part of the code seems right.
The teacher of the class is very strict on only using what we have learned in class so far, so I won't be able to use that limit library.
Well, while ( infile >> number ) would be sufficient for your loop. When you use infile in a conditional by itself it returns a boolean value that is true if neither of the failbit or badbits are set. the operation infile >> number returns the istream used in the operator, so while (infile >> number) is equivalent to while ((infile>>number), infile) which may not be entirely useful if you're not familiar with the comma operator (It evaluates the first expression, discards it, and evaluates the second condition.)
You could use: while ( !infile.eof() && (infile >> number) ) to satisfy your assignment's requirement.
I think 0 is an even number.
You are correct.
I won't be able to use that limit library.
You could note what those values are and use them directly, or as suggested earlier, you could read in the first value outside of the loop and update variables accordingly to prime the pump.
Thanks to everyone that helped. I ended up just subtracting one from the odd number total since the counter incremented once more than I wanted it to. Not the best solution, but I think the class is more about getting the work done rather than learning to actually be a good programmer.