I have been working on this code for 7 hours. I need help. I can only use malloc
ID: 3624633 • Letter: I
Question
I have been working on this code for 7 hours. I need help. I can only use malloc to create an array (no brackets allowed in program). I am learning how to use pointers, and pointer arithmetic. The first part of the program, I have to create an array dynamically, from the user input = max.
This program is about generating prime#'s using Sieve of Eratosthenes. So, I created a method setupArrayForSieve() that puts a 1 in each address location.
The next method is required to look like: void runSieve(char *array, int max). Here we pass through the array, starting at 2, 2 is prime so we go to the next multiple of 2 and set that to zero. We make each multiple of 2 down the array = 0, until the counter gets to "max".
Then we go to the next variable, which is 3 and a prime. We go down the array and set each address that is a multiple of 3 = 0. When we reach a number that is already 0 (like 4, which was set to 0 when we did 2), we skip to the next address that has a 1, and set the multiples of that number equal to 0. We repeat these steps until we reach an address that = square root of "max" and do the process one last time.
The last method should look like: void showPrimes(char (array, int max). The output needs to be formatted to look like (each column is right justified 10):
find primes up to: 386
2 3 5 7 11 13
17 19 23 29 31 37
41 43 47 53 59 61
67 71 73 79 83 89
97 101 103 107 109 113
127 131 137 139 149 151
157 163 167 173 179 181
191 193 197 199 211 223
227 229 233 239 241 251
257 263 269 271 277 281
283 293 307 311 313 317
331 337 347 349 353 359
367 373 379 383
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*global variables*/
int max = 0; //upper limit of primes to compute
int primeNumber; //actual prime# to display
/*functions*/
void setupArrayForSieve(char *array);
void runSieve(char *array, int max);
void showPrimes(char *array, int max);
int main()
{
while(max <= 1){ //continuous loop until max >= 2
/*asks user for upper limit for prime# generater*/
printf("Enter the upper limit for the number of primes that you "
"would like to compute. Must be an integer: ");
scanf("%d", &max); //stores user input in max
}
char *array; //creates dynamic arrary from user input
array = (char *)malloc(max * sizeof(char)); //allocates memory
setupArrayForSieve(array);
runSieve(array, max);
showPrimes(array, max);
free(array); //free memory allocation
system("PAUSE");
return 0;
}
void setupArrayForSieve(char *array)
{
char *p; //pointer for working on array
p = array; //pointer is set to first address in array
p = p + 2; //we want to start array at 2
int i = 2; //counter starts at 2
if(array){ //if malloc works
while( i <= max){ //reads thru a[2] until max
*p = 1; //sets contents = 1
p = p+1; //increments one address
i++; //increment counter
primeNumber = (i - 1); //equates a prime# to address
}
}
else{
printf("Memory allocation failure. ");
exit(1); //terminates program
}
}
/*sets the composite #'s to 0. leaves the primes = 1.*/
void runSieve(char *array, int max)
{
char *p; //pointer for working on array
p = array; /point er is set to first address in array
p = p + 2; //we want to start array at 2
int i = 2; //counter starts at 2
for(i; i <= (int)sqrt(max); i++){ //sieve until <= (sqrt of max)
if( *p = 1){ //if there is a prime at the address
int multiple = 2; //multiplier for passing thru array
while(*p <= max){ //cont loop until we reach max
*p = 0; //contents of p equals 0
p = p + multiple; //move pointer up address
multiple++; //increment multiplier
i++; //increment counter
}
}
else{
while(*p == 0){
p++; //increment pointer by 1
i++; //increment counter by 1
}
int multiple = 2; //multiplier for passing thru array
while(*p <= max){ //cont. loop until we reach max
*p = 0; //contents of p equals 0
p = p + multiple; //move pointer up address
multiple++; //increment multiplier
i++; //increment counter
}
}
}
}
/*displays prime #'s. each line has 6 columns rt justified 10.*/
void showPrimes(char *array, int max)
{
//int i=0; //creates counter for the address
//use a for loop
}
Explanation / Answer
please rate - thanks
you were a little complicated
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//actual prime# to display
/*functions*/
void setupArrayForSieve(int *array,int max);
void runSieve(int *array, int max);
void showPrimes(int *array, int max);
int main()
{int max=0;
while(max <= 1){ //continuous loop until max >= 2
/*asks user for upper limit for prime# generater*/
printf("Enter the upper limit for the number of primes that you "
"would like to compute. Must be an integer: ");
scanf("%d", &max); //stores user input in max
}
//creates dynamic arrary from user input
int *array=malloc(sizeof(int)*max); //allocates memory
setupArrayForSieve(array,max);
runSieve(array, max);
showPrimes(array, max);
free(array); //free memory allocation
system("PAUSE");
return 0;
}
void setupArrayForSieve(int *array,int max)
{int i;
for(i=0;i<max;i++)
*(array+i)=1; //sets contents = 1
}
/*sets the composite #'s to 0. leaves the primes = 1.*/
void runSieve(int *array, int max)
{int i,j;
for(i=2;i<=(int)sqrt(max);i++)
if(*(array+i)==1)
for(j=i;j<max;j+=i)
if(*(array+j)==1&&j!=i)
*(array+j)=0;
}
/*displays prime #'s. each line has 6 columns rt justified 10.*/
void showPrimes(int *array, int max)
{
int i=0; //creates counter for the address
int j;
//use a for loop
printf("The prime numbers between 2 and %d are: ",max);
j=0;
for(i=2;i<max;i++)
{if(*(array+i)==1)
{ printf("%d ",i);
j++;
if(j%6==0)
printf(" ");
}
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.