What we are supposed to do is answer the question "How far do we have to go to add 1,2,3,4,5,6... to reach 25000? The program is supposed to print out the sum and the last number added to make it go over 25000.
I am visually impaired, and our teacher only does lecture, he isn't very hands on. I have a note taker, but that isn't helping. Here's what I have so far.
# include <iostream>
using namespace std;
void main (void)
{
int sum = 1, n;
for (n=1; n<=25000; n=n+1)
sum= sum+n
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
}
I think you should also print n in the output as they have also asked to print the last number added to make it go over 25000.
so
# include <iostream>
using namespace std;
void main (void)
{
int sum = 1, n;
for (n=1; n<=25000; n=n+1)
{
sum= sum+n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
}
The end condition for the loop should be sum < 25000, surely?
The initial value of sum should be zero, not one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int sum = 0;
int n = 0;
while (sum < 25000)
{
n++;
sum += n;
}
cout <<"the sum of 1 through " << n << " is "<<sum<<endl;
}
I'm not really good with all those shortcuts yet, so please be paitent with me. I have a few questions about this but this is what I came up with in well stupid beginner's terms.
# include <iostream>
using namespace std;
void main (void)
{
int sum = 0;
int n = 0;
while (sum < 25000)
{
n=n+1;
sum += n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
}
The first question I have is what is the purpose of the int n = 0; line?
Second, what is sum += n; ?
n needs to be given an initial value, before it is used later. Otherwise n could contain some random value, whatever happened to already reside in that memory location.
what is sum += n; ?
That's just a more concise way of putting sum = sum + n;
GRR, it won't compile, this is where I wish I had access to my Windows PC and Visual Studio, I'm getting these errors.
untitled.cpp:5:210: warning: missing terminating ' character
untitled.cpp:5: error: missing terminating ' character
untitled.cpp:6: error: stray ‘\’ in program
untitled.cpp:40:60: warning: backslash-newline at end of file
untitled.cpp:40: error: stray ‘\’ in program
untitled.cpp:1: error: expected unqualified-id before ‘{’ token
# include <isotherm>
usingnamespace std;
void main (void)
{
int sum = 0;
int n = 0;
while (sum < 25000);
{
n=n+1;
sum += n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<end;
cout<<"The last number added is : "<<n<<endl;
}
Thanks for sharing the code. I don't get any of the errors listed above. But there are some other errors.
Line 1, isotherm should be iostream
Line 4. void main should read int main - at least for my compiler. My understanding is that the use of void here is non-standard won't work on all compilers.
Line 10 - remove the semicolon - not needed here.
Line 15 - end should be endl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main ()
{
int sum = 0;
int n = 0;
while (sum < 25000)
{
n=n+1;
sum += n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
}
#include <iostream>
usingnamespace std;
int main ()
{
int sum = 0;
int n = 0;
while (sum < 25000)
{
n=n+1;
sum =sum+n;
}
cout <<"The sum of 1 through 25000 is "<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
return 0;
}
This should compile. Also, you should use proper indentation. I noticed that you put ; after while() which would run the loop infinitely as ; itself is a valid statement hence loop would return to it's condition after terminating at ;. So it would continue to repeat this cycle until condition becomes false (which wouldn't become false as you are not incrementing/decrementing anything..)I've fixed the issues in the above code and it should work fine.
Thanks guys, I don't know how the first line got messed up, probably got goofed while editing it so many times, whoops. Let me fix it, and try to compile again.
EDIT, I see what you mean about the infinite loop, I'm gonna keep these posts as a reference for future programs, it will help me a lot.
I've no idea what that error means.
However, I should ask, are you re-typing the code fresh each time? If you do that, you may get rid of one error and introduce another.
Better to just copy and paste the complete working code all in one step.
That's what I'm doing. My exact method is gcc -O program untitled.cpp then it would give me these stupid errors. I don't know why our school has C++ as a first year course.