Program to print prime numbers using sieve of eratosthenes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blackout
    New Member
    • Oct 2007
    • 3

    Program to print prime numbers using sieve of eratosthenes

    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:
    [code=c]
    /* This program uses the Sieve of Eratosthenes to
    * find all the prime numbers in the range of 1 - 300.
    */

    #include <stdio.h>
    #include "genlib.h"
    #include "math.h"

    #define maxNums 300

    bool is_Prime(bool flag[]);

    main()
    {
    bool flag[maxNums];
    is_Prime(flag);
    getchar();
    }

    bool is_Prime(bool flag[])
    {
    int x, y;
    bool TRUE, FALSE;
    for(x=2;x<=sqrt (maxNums);x++)
    {
    y=x*x;
    while(y<=maxNum s)
    {
    flag[x*y] = FALSE;
    y++;
    }


    }
    y=0;
    while(y<=maxNum s)
    {
    if(flag[y]!=FALSE)flag[y] = TRUE;
    y++;
    }
    for(y=1;y<=maxN ums;y++)
    {
    if(flag[y] = TRUE)printf("%d \n", flag[y]);
    }
    }
    [/code]
    Help would be greatly appreciated. Thanks!
    Last edited by sicarie; Nov 13 '07, 01:36 PM. Reason: Code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by Blackout
    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)

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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.

      Comment

      Working...