Hey I have been in C+ for two weeks now and my teacher asked this question
Write a program that calculates and prints the sum of all even numbers between 10 and 40?
Well after alot of time I am stuck, I know my code right...at least I think but it doesn't show anything on the screen. The command prompt pops up a split second then disappers....so it has to be something really simple right?
Here's the code
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
int x;
int i;
i=0;
x=10;
while (x <= 40)
{
i=i+x;
x=x+2;
}
printf("Sum of all even numbers between 10 and 40(inclusive) is %d\n",i);
return 0;
}
BTW, a for loop would be a better choice for what you a trying to do:
1 2 3 4 5
for(int x = 10; x <= 40; x+=2) { //int x = 10 is defining the variable
//x <= 40 is the condition under which the loop runs
//and x+=2 is run after every loop "cycle"
i+=x; //i += x is the same as i = i + x
}
Hey guys so far this site has taught me alot...better than this book they gave. But I looked at the forum that helios posted and some of it make sense and some doesn't apparently their is tons of ways to pause the screen to see your output. But could someone show me the basic way of doing this with the isostream, with my code included? Please?
@jsmith
I test your code, later, thank you very much
AND to the rest thank you so much for helping me. I will probabley be on this site alot now. This is a homework assignment, but you can see I have tried. And my teacher doesn't care about forum help as long as I get it.
Thats BS. You could as well compute the result by hand, insert the number and claim it's fast. Just that you use the C++ compiler to calculate it doesn't make your algorithm fast (on the contrary, one could argue that you make C++ into a interpreted language, and considering my compile times, a fairly slow one). Furthermore it is "neat", and "neat" solutions should be avoided. (Yes, there are cases where TMP is a Good Thing. But this isn't one of them)
Use the formula from jsmith, but don't use the nifty template stuff. It's kinda cool, but you should stay away from it until you have a very good understanding of the language (here, templates are used in a completely different way than they were intendet to.)
In terms of execution time of the program, it is the fastest since it simply outputs 400 without
doing any calculations. In terms of compilation time it is probably slightly slower.
Compilation speed is a hardware problem. Upgrade your computer.
Absolutely disagree on your argument re: neat solutions. If execution time is of concern then
doing the calculation at compile time rather than execution time is the best possible solution.
I posted it knowing full well that the original poster couldn't use my code for his/her homework
since it used concepts well beyond his/her class. My point was simply to plant a seed that
there are these things called "templates" in the language and to show that there is more than one way to solve the problem.
I think that is the most convoluted solution suggested yet. No looping is even needed to solve the problem. An algorithm that runs in constant time for all inputs is always better than one that runs in linear time for all inputs.