Write a program to sum up either all the even numbers or all the odd numbers from 0 to N where you will prompt the user for the word 'even' or 'odd' and the value of N. Test your program with 'even values to 180' then with 'odd values to 160', by reading in the data. You will enter the word 'even' or 'odd' and a value for N. Print result to file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
ofstream range;
range.open("EvenOrOdd.out");
int i=160,//even
n=190,//odd
sum=0;
string c;
cout << "Enter even or odd: ";
cin >> c;
cout << "Enter the number: ";
cin >> n;
if(c=="even")
{
i=0;
while(i<=n)
{
sum+=i;
i+=2;
}
}
if(c=="odd")
{
i=1;
while(i<n)
{
sum+=i;
i+=2;
}
}
range << "The total is:" << sum;
range.close();
return 0;
}
I have gotten this far with the program and it works fine, but I need help knowing if this is how the problem is being asked? I know I can't use cin and cout but I'm unsure how to go around it. Also, I can use %2, but unsure how to use it.
Think first, code second.
What you have looks like it might work, but I did not go looking for problems, because you did not ask about any problems or explain what is going wrong etc.
why not try it with some small #s? 1-5 odd is 1+3+5 = 9, 1-10 even is 2+4+6+8+10 is 30 ...
do those work??
%2 is remainder when divided by 2, which happens to be even/odd indicator: 3%2 = 1, 10%2 = 0, 51%2 = 1, 100000000092%2 = 0 ... (I see no use for this today, but you asked).
ok, so lets think. both even and odd are +2.
all that changes is where you start.
to sum evens, start at 0 and add 2 every time:
0,2,4,6,8,10,... //adding 0 does nothing
and to sum odds, start at 1: 1,3,5,7....
so a single loop will do it.
all you need, then, is one loop and a starting condition:
i = 0;
if(c == "odd")
i = 1;
while(i < n) //assignment unclear, this could be <= ??!!
{
sum+= i;
i += 2;
}
the above still works if you want a sum over a range that starts anywhere.. that would need the %. you need to figure out if start is even or odd, and then adjust accordingly. want to give it a try?
you can open a file in the constructor
ofstream ofs(filename); //don't need .open() ... its not wrong, just wordy.
> you will prompt the user for the word 'even' or 'odd' and the value of N.
> (...) I know I can't use cin and cout but I'm unsure how to go around it.
¿how did you reach that conclusion?
You're overthinking the problem. Your first version works fine. Still, it could use improvement.
Indent your code consistently. I cannot stress the importance of this. No bug is harder to find and easier to avoid than putting { and } in the wrong place.
Ideally, you'd use the math formula for an arithmetic series like lastchance. Why make the computer do this with a loop when we've known for hundreds of years that problems like this can be solved with a simple calculation?
If you really want to use a loop then
- recognize that the only difference between the "even" and "odd" cases is the starting value for the loop.
- prefer a for loop to a while loop. For loops usually produce clearer code.
Here is your code, still using a loop but with the other changes:
@ne555, The professor has these preprinted sheets that have the assignments on them. I have no idea where he gets them or if he created them. But we, as a class, can not use cin or cout on our codes. everything has to write to a file. I know the assignment says one thing, but the professor says another.
@dhayden, thank you for your input, as of right now we can't use FOR loops yet. We were just caught about WHILE.
My hardest learning curve on these programs is thinking of how to write these codes. Once completed they aren't that difficult but the beginning is my hardest issue.
@Jonin, the second code wasn't properly running, I wasn't able to get any value other than 0 for sum 1. Then sum 2 would always be a lot larger of a number that isn't the value I needed. Also, on the first code, How would I alleviate the cin and cout, so I don't have any inputs, that the program just begins with the two values I am searching for?
Prefer doing it as a single statement: std::fstream ofs("output.txt", std::ofstream::out);
Opening a file for input could be: std::fstream ifs("input.txt", std::ofstream::in);
what you have is fine if you want a for loop (which is correct for the problem)
as a while loop, you can't use i anymore (the for loop overrides the i you already have)
I was not sure about what the variables were supposed to be initially, so I used i above
1 2 3 4 5 6 7 8
if (c == "odd") //its already zero.
startVal = 1;
while(startVal <= n)
{
sum += startVal;
startVal +=2;
}
not sure what you want. you want to hard code 160 and 180?
... the duh (this the direct approach) version of that, assuming you don't have a function:
1 2 3 4 5 6 7 8 9 10 11 12
int startVal = 1;
for(int duh = 160; duh <= 180; duh+= 20)
{
sum = 0;
while(startVal <= duh)
{
sum += startVal;
startVal +=2;
}
cout << "The total for " <<duh << " is: " << sum << endl; //change to file after debugging
startVal = 0;
}