![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2008
Posts: 10
Rep Power: 0
![]() |
Can someone tell me?
I was to write a code that sums the integers from 0 to 10,000
we can't use the fact that 1 + 2 + 3 + ...+ n = n(n+1) / 2 So here's what I did. Can you please check through it for me? I haven 't had a C or C++ class yet so I'm not sure. I downloaded Dev C++ compiler, and it compiles but the window which shows the answer disappears quicker than I can see it. I searched for the exe file, but didn't find it. All help appreciated. Feel free to post a revised code; thanks! c Syntax (Toggle Plain Text)
Last edited by Ancient Dragon; Aug 20th, 2008 at 11:33 PM. Reason: add code tags |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 649
Rep Power: 4
![]() |
Re: Can someone tell me?
>>but the window which shows the answer disappears quicker than I can see it.
you need to add something that will make the program stop before returning from main(). There are several ways to do it, this is the simplest getch();. That will wait for you to hit a key.If you want to sum all the numbers between 1 and 10,000 then the loop should be those numbers for(i = 1; i <= 10000; i++) sum += i; >>return (sum); Its not necessary to return the value of sum from main(). Most programs return 0 on successful completion or something else to indicate an error condition. |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Aug 2008
Posts: 6
Rep Power: 0
![]() |
Re: Can someone tell me?
Quote:
|
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Aug 2008
Posts: 10
Rep Power: 0
![]() |
Re: Can someone tell me?
#include <stdio.h>
int main(void) { int sum = 0; for (int i=1; i<=10000; i++) { sum += i; } printf("the sum is %d ", sum); } Like this?? Where do I put the getch(); ? Thanks |
|
|
|
|
|
#5 |
|
Hobbyist C# Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 213
Rep Power: 2
![]() |
Re: Can someone tell me?
As daiguoliang suggested...
You can also just open your command prompt window( START > RUN > type "cmd" and hit Enter ). If you can't find the EXE, simply open the project in your DEV - CPP and click Ctrl + F12, then save it somewehere you can find it, and rebuild your project to create the new .EXE. ![]() Now, once your command window is open, simply drag the .EXE into the black space, hit Enter to start your program. It would be easier to follow Ancient Dragon's advice, especially if you are going to be doing a lot of these programs. :-)
__________________
"Common sense is the collection of prejudices acquired by age eighteen." - Albert Einstein Last edited by BstrucT; Aug 21st, 2008 at 1:46 AM. |
|
|
|
|
|
#6 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 649
Rep Power: 4
![]() |
Re: Can someone tell me?
immediately after the printf() statement.
|
|
|
|
|
|
#7 |
|
Expert Bug Developer
Join Date: Apr 2008
Posts: 22
Rep Power: 0
![]() |
Re: Can someone tell me?
>There are several ways to do it, this is the simplest getch();
Of course, it'd be nice if you recommended standard solutions. getch() is a compiler extension, and not standard by any stretch. getchar() will do approximately the same thing while maintaining portability. Thus, your final solution will look like #include <stdio.h>
int main(void)
{
int sum = 0;
for (int i=1; i<=10000; i++)
{
sum += i;
}
printf("the sum is %d ", sum);
getchar();
}Other points: - declaring the variable inside of the for() loop isn't defined in standard C. You should really declare all your variables at the beginning of your block to ensure maximum compatiblity. - make sure to flush the output buffer with fflush(), don't assume that it'll happen automatically. |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Aug 2008
Posts: 11
Rep Power: 0
![]() |
Quote:
I didn't see anywhere that Chem.E had to use c89.For production code I would agree that following c89 would maximize portability but MinGW has pretty good c99 support for stuff like that. I'm on linux so my console is really easy to execute programs but I remember how annoying CMD is because it starts you in your user name dir instead of My Documents. So use what Sorrofix said and use getchar(). |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Aug 2008
Posts: 10
Rep Power: 0
![]() |
Re: Can someone tell me?
Thanks to all the helpful responses. I re-wrote it as here:
#include <stdio.h> int main(void) { int sum = 0; int i, N; N=10000; i=0; for (i=0; i<=N; i++) { sum=sum+i; } printf ("The sum of the consecutive integers from 0-10000 is = %d", sum); getch(); return (0); } I think maybe some ways here are more efficient, yet I do not want to copy. Thanks for the help with get character command. Many thanks to all |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Aug 2008
Posts: 11
Rep Power: 0
![]() |
Re: Can someone tell me?
Quote:
C Syntax (Toggle Plain Text)
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|