Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 20th, 2008, 8:51 PM   #1
Chem.E
Newbie
 
Join Date: Aug 2008
Posts: 10
Rep Power: 0 Chem.E is on a distinguished road
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)
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int sum = 0;
  5. for (int i=0; i<=10000; i++)
  6. {
  7. sum=sum+i;
  8. }
  9. printf("the sum is %d ", sum);
  10. return (sum);
  11. }

Last edited by Ancient Dragon; Aug 20th, 2008 at 11:33 PM. Reason: add code tags
Chem.E is offline   Reply With Quote
Old Aug 20th, 2008, 11:37 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 649
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
__________________
<<Hire Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Aug 21st, 2008, 12:16 AM   #3
daiguoliang
Newbie
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0 daiguoliang is on a distinguished road
Re: Can someone tell me?

Quote:
Originally Posted by Chem.E View Post
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)
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int sum = 0;
  5. for (int i=0; i<=10000; i++)
  6. {
  7. sum=sum+i;
  8. }
  9. printf("the sum is %d ", sum);
  10. return (sum);
  11. }
You also can run the program throught commandline,in this way ,you will see the result
daiguoliang is offline   Reply With Quote
Old Aug 21st, 2008, 12:27 AM   #4
Chem.E
Newbie
 
Join Date: Aug 2008
Posts: 10
Rep Power: 0 Chem.E is on a distinguished road
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
Chem.E is offline   Reply With Quote
Old Aug 21st, 2008, 1:23 AM   #5
BstrucT
Hobbyist C# Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 213
Rep Power: 2 BstrucT is on a distinguished road
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.
BstrucT is offline   Reply With Quote
Old Aug 21st, 2008, 8:31 AM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 649
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Can someone tell me?

Quote:
Originally Posted by Chem.E View Post
Like this?? Where do I put the getch(); ? Thanks
immediately after the printf() statement.
__________________
<<Hire Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Aug 26th, 2008, 5:15 AM   #7
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 22
Rep Power: 0 Sorrofix is on a distinguished road
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.
Sorrofix is offline   Reply With Quote
Old Aug 26th, 2008, 10:30 PM   #8
Goblin
Newbie
 
Goblin's Avatar
 
Join Date: Aug 2008
Posts: 11
Rep Power: 0 Goblin is on a distinguished road
Question Re: Can someone tell me?

Quote:
Originally Posted by Sorrofix View Post
>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.
c99 allows declaring variables in for loops
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().
Goblin is offline   Reply With Quote
Old Aug 26th, 2008, 11:47 PM   #9
Chem.E
Newbie
 
Join Date: Aug 2008
Posts: 10
Rep Power: 0 Chem.E is on a distinguished road
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
Chem.E is offline   Reply With Quote
Old Aug 27th, 2008, 12:13 AM   #10
Goblin
Newbie
 
Goblin's Avatar
 
Join Date: Aug 2008
Posts: 11
Rep Power: 0 Goblin is on a distinguished road
Re: Can someone tell me?

Quote:
Originally Posted by Chem.E View Post
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
C Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int sum = 0;
  5. int i, N;
  6. N=10000;
  7. i=0;
  8.  
  9. for (i=0; i<=N; i++)
  10. {
  11. sum=sum+i;
  12. }
  13. printf ("The sum of the consecutive integers from 0-10000 is = %d", sum);
  14. getch();
  15. return (0);
  16. }
Line 14 isn't getchar() still
Goblin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC