Question #84201

I want to write a program that finds prime numbers using a sieve of Eratosthenes method.
I must use (variable length array) vom Typ bool.
I wrote the following code, but unfortunately it does not work.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{ int n,i;

printf("Enter the limit: ");
scanf("%d",&n);
int size = sizeof(A)/ sizeof(n);

prim(A,size);

for(i=2;i<n;i++)
{
if(A[i])
{
printf("\n%d",i);
}
}

return 0;
}
bool prim(int* A, int size)
{
int i,j;
for(i=2;i<n;i++)
A[i] = true;


for(i=2;i<n;i++)
{
for(j=i;i*j<n;j++)
{
A[i*j] = false;
}
}
}
can you help me please?

Expert's answer

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool prim(int* A, int n) // function declaration must be before "main"
{
    int i, j;
    for(i=2; i<n; i++)
    {
        A[i] = true;
        for(j=i; j<n; j++)
        {
            if(A[i])
            {
                A[j] = false;
            }
        }
    }
    free(A); // free the memory
    return 0;
}
Answer provided by www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS