I am a beginner and need help with my program in C. My assignment is to find all the prime number up to the number the user input using a sieve algorithm. for example if user input 6 , the prime numbers will be 2, 3, 5. Then it should display the final set of prime numbers in an 8 column table format for example if there are 20 prime numbers(this are not prime numbers but simply the location in the output table each prime would take.) At the end it should prompt the user and accepting a Y or N character to determine whether to repeat or not.

0 3 6 9 12 14 16 18
1 4 7 10 13 15 17 19
2 5 8 11

Right now when I run the program below it shows a runtime check failure #2- stack around variable 'prime' was corrupted. How do I fixed it, and also how do I print all the primes up to the number the user input in the above table format

``````#include <limits.h>
#include <stdio.h>

int main() /* main program starts here*/
{
/*initialize variables for sieve algorithm*/

short int A[SHRT_MAX+1]= {0};
short int prime [5000];
int n, j;
int prime_count = 1;
int cur_prime = 2;

printf ("You want prime numbers up to: ");
scanf("%d", &n);
while (cur_prime <= SHRT_MAX && prime_count < n) {
for (j=2 ; j*cur_prime < SHRT_MAX ; j++)
{
prime[j*cur_prime] = 1; /* cross out all multiples of the number*/
}
/* this while loop advance to the next un-crossed out number, which is prime*/
while (A[cur_prime] == 0 && cur_prime <= SHRT_MAX)
{
cur_prime++;
}

/* this prints the numbers left which is prime */
if (cur_prime > SHRT_MAX)
printf ("no more primes");
else
prime[prime_count++] = cur_prime;
}
return 0;``````

## All 6 Replies

I added an ending bracket after "return 0" and ran your code and it ran to completion. I didn't get any run-time error.

I ran my code with the ending bracket and it still show a run-time error. I am using visual studio express 2008. Do you know what could be the problem? Also do you know how to print the prime number in the specified table format.

I'd use printf. This link gives an example of how to display an integer with a certain width of spaces. Constant width will make them line up.

I've never run a C program on Visual Studio, so no idea there. I did it on Dev C++ and it ran without error.

i used visula studio 2005, it is not giving any run time error. can you tell what error you are getting.

The error I am getting with visual studio 2008 is (runtime check failure #2- stack around variable 'prime' was corrupted)

I would do one or both of two things. One, scatter some printf statements through your code and see how far it gets. I usually display stuff like "Got this far 1", "Got this far 2", etc. in different parts of the code to help me nail down where the error is. Also, experiment with different numbers. Maybe it crashes when you input 13 but runs when you input 29 or something. I didn't get the error.

Second, perhaps install another free compiler like Dev C++ and see whether the same thing occurs. Danger Dev made it work on VS 2005, but you're running it on VS 2008. I can't imagine why that would matter. If it runs on Dev C++ on your machine but fails on VS 2008, the problem likely is not the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.