By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Program to print prime numbers using sieve of eratosthenes

Blackout
P: 3
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of eratosthenes and using arrays (passing arrays to functions). Whenever i run it the compiler compiles it, shows a window for half a second, and closes again. Here is what I've done so far:
Expand|Select|Wrap|Line Numbers
  1. /* This program uses the Sieve of Eratosthenes to
  2.  * find all the prime numbers in the range of 1 - 300.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "genlib.h"
  7. #include "math.h"
  8.  
  9. #define maxNums 300
  10.  
  11. bool is_Prime(bool flag[]);
  12.  
  13. main()
  14. {
  15.       bool flag[maxNums];
  16.       is_Prime(flag);
  17.       getchar();
  18. }
  19.  
  20. bool is_Prime(bool flag[])
  21. {
  22.      int x, y;
  23.      bool TRUE, FALSE;
  24.      for(x=2;x<=sqrt(maxNums);x++)
  25.      {
  26.              y=x*x;
  27.              while(y<=maxNums)
  28.              {
  29.                    flag[x*y] = FALSE;
  30.                    y++;
  31.               }
  32.  
  33.  
  34.        }
  35.        y=0;
  36.      while(y<=maxNums)
  37.      {
  38.           if(flag[y]!=FALSE)flag[y] = TRUE;
  39.           y++;
  40.      }
  41.      for(y=1;y<=maxNums;y++)
  42.      {
  43.      if(flag[y] = TRUE)printf("%d\n", flag[y]);
  44.      }
  45. }
  46.  
Help would be greatly appreciated. Thanks!
Nov 13 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of eratosthenes and using arrays (passing arrays to functions). Whenever i run it the compiler compiles it, shows a window for half a second, and closes again.
Yep, it will do that if you don't specify for the window to stay open. I would recommend putting some sort of scanf() or read call at the end to keep the window open until a certain character or sequence of characters is pressed.

Your main should also have a type, specifically int (requiring a return type at the end as well)
Nov 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Visual Studio has two type of builds: debug and release.

The default tis debug. This build has code in the .exe to operate the debugger.
When you select Start from the Debug menu (or press F5), Visual Studio sees that the .exe is a debug .exe
and assumes you are using your debugger and that means you have already set breakpoints
or some such and the code is just executed. This produces the flashing blcak screen.

That was the program executing.

On the other hand, you can select Start Without Debugging from the Debug menu (or press CTRL+F5)
and that tells Visual Studio that while this is a debug .exe that you are not using the debugger.
That prompts Visual Studio to stop at the end of main() with "Press any key to continue..." so you can
see what happened.
Nov 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.