473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Program to print prime numbers using sieve of eratosthenes

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. 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
2 10567
sicarie
4,677 Expert Mod 4TB
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
9,208 Expert Mod 8TB
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

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

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
6
by: ckroom | last post by:
Does anyone know a good algorithm to get the next prime of a number n? prime = nextprime( n ); It's for some stuff about double rehashing, thanks. -- ckroom http://nazaries.net/~ckroom
13
by: Don | last post by:
> How do you write a program to print the prime numbers > up to 1 million? #include <stdio.h> /* This program implements a blindingly fast O(n^n) algorithm to find prime numbers, using an...
9
by: k1ckthem1dget | last post by:
Can someone please help me with this program. How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the “sieve method of Eratosthenes”....
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
4
by: ritesh.raghuvanshi | last post by:
can anybody help me in this...? i want to generate prime numbers starting from zero to the number entered by user... quick please....
21
by: =?ISO-8859-1?Q?Rog=E9rio_Brito?= | last post by:
Hi, All. I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but...
5
by: jzakiya | last post by:
This is to announce the release of my paper "Ultimate Prime Sieve -- Sieve of Zakiiya (SoZ)" in which I show and explain the development of a class of Number Theory Sieves to generate prime...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.