Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Can someone tell me? (http://www.programmingforums.org/showthread.php?t=16348)

Chem.E Aug 20th, 2008 8:51 PM

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!


:

  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. }


Ancient Dragon Aug 20th, 2008 11:37 PM

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.

daiguoliang Aug 21st, 2008 12:16 AM

Re: Can someone tell me?
 
Quote:

Originally Posted by Chem.E (Post 147781)
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!


:

  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

Chem.E Aug 21st, 2008 12:27 AM

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

BstrucT Aug 21st, 2008 1:23 AM

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. :-)

Ancient Dragon Aug 21st, 2008 8:31 AM

Re: Can someone tell me?
 
Quote:

Originally Posted by Chem.E (Post 147789)
Like this?? Where do I put the getch(); ? Thanks

immediately after the printf() statement.

Sorrofix Aug 26th, 2008 5:15 AM

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.

Goblin Aug 26th, 2008 10:30 PM

Re: Can someone tell me?
 
Quote:

Originally Posted by Sorrofix (Post 147909)
>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().

Chem.E Aug 26th, 2008 11:47 PM

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

Goblin Aug 27th, 2008 12:13 AM

Re: Can someone tell me?
 
Quote:

Originally Posted by Chem.E (Post 147946)
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

:

  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


All times are GMT -5. The time now is 12:20 PM.

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