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;