473,322 Members | 1,734 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,322 software developers and data experts.

Sieve of Eratosthenes Help

So I've been assigned a program that sieves all non prime numbers from 0-1000. I've written the meat of this code but have a problem. Normally I have a Linux computer with a different compiler, but for this one I'm stuck using Visual Studio 2010. Here's what I have so far. My only problem is the line "int *array = calloc(topval + 1, sizeof(int));" The error is that type void* cannot initialize type int*. Any solutions would be appreciated!!

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int i;
int prime;
int multiple;
int topval = 1000;
int count = topval - 1;
int *array = calloc(topval + 1, sizeof(int));


/*Marks all elements of the array as possible primes.*/
for (i=2; i <= topval; i++)
{
array[i] = 1;
}

/*Marks multiples as non-prime.*/
for (prime = 2; prime <= topval; prime++)
{
if (array[prime])
for (multiple = 2*prime; multiple <= topval; multiple += prime)
{
if (array[multiple])
{
array[multiple] = 0;
count--;
}
}
}

/*Prints the remaining numbers, which are prime. */
for (i=2; i <= topval; ++i)
{
if (array[i])
printf("%d ", i);
}
printf("\n\n %d primes up to %d found.\n", count, topval);
exit(0);
}
Apr 1 '15 #1
1 1464
weaknessforcats
9,208 Expert Mod 8TB
An int* has to contain the address of an int otherwise the pointer arithmetic won't work. Put any old address in an int* and the program is now ready to crash. For this reason void* can't be assigned to an int*.

All you have to do is cast the return of calloc to an int*. That way when you crash the compiler will not be responsible.
Apr 1 '15 #2

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

Similar topics

20
by: MSiegel | last post by:
hi there! i have to program the sieve of eratosthenes in php as a homework. after i had created an html file where the maximum is set i wrote a php script which doesn't work properly - actually...
0
by: Mark A. Washburn | last post by:
/* SIEVE OF ERATOSTHENES from BYTE magazine -------------------- -- compiled with jdk 1.1.7b with optimize on ( -O) -- Run times on 300 MHz Pentium 2 Windows 95 -- in order of output, from...
15
by: Steve Bergman | last post by:
Just wanted to report a delightful little surprise while experimenting with psyco. The program below performs astonoshingly well with psyco. It finds all the prime numbers < 10,000,000 ...
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”....
4
by: knuxus | last post by:
Hey everyone.... I would appreciate any type of help if anyone could explain me how to translate this algorithm to Visual Basic, im still learning and i would appreciate this algorithm to my prime...
2
Blackout
by: Blackout | last post by:
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...
3
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...
4
by: jzakiya | last post by:
Update: 2008/11/03 Architecture & coding improvements. Renamed generators. I am 90% finished writing up a mathematical analysis of my method. In the process I found an architectural...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.