0

Here are two functions below that compile perfectly but I seem to be getting a weird error with the very first inputted integer. I have tried debugging in GDB but when it's only the first inputted value that is having this weird error, then it makes things complicated.

#include <stdio.h>
#include "Assg9.h"
#include <stdlib.h>
#include <assert.h>
#include <math.h>

void getPrimes(int usernum, int* count, int** array){
    (*count) = (usernum - 1);
    int sieve[usernum-1], primenums = 0, index, fillnum, multiple;

    for(index = 0, fillnum = 2; fillnum <= usernum; index++, fillnum++){
        sieve[index] = fillnum;
    }

    for (; primenums < sqrt(usernum); primenums++)  
        {
            if (sieve[primenums] != 0){                 
                   for (multiple = primenums + (sieve[primenums]); multiple < usernum - 1; multiple += sieve[primenums])//If it is not crossed out it starts deleting its multiples.
                   {  
                     if(sieve[multiple]) {      
                       --(*count);              
                       sieve[multiple] = 0;
                   }
                   }
            }
        }
        int k;

        for (k = 0; k < usernum; k++)
            if (sieve[k] != 0)
                {
                    printf("%d ", sieve[k]);
                }
        *array = malloc(sizeof(int) * (usernum +1));
         assert(array);
         (*array) = sieve;
    }



void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
    int i;
    fprintf(fpout, "There are %d prime numbers less than or equal to %d \n", count, n);
    for(i = 0; i < count; i++)
    {   
        if(*(array + i) != 0){
        fprintf(fpout, "%d ", *(array + i));

        }

    }
 }

Our Output:

Please enter an integer in the range 2 <-> 2000 both inclusive: 2
2 32664 
Do you want to try again? Press Y for Yes and N for No: y
Please enter an integer in the range 2 <-> 2000 both inclusive: 2
2 
Do you want to try again? Press Y for Yes and N for No: n
Good bye.  Have a nice day

Expected output should obviously just display 2. This is the case for any integer from 2-2000 for the very first inputted integer. The very last, or last 2, prime numbers print very large numbers, sometimes even negative numbers. I have no clue why, but after the first inputted value everything works perfectly. Tried debugging this with GDB like crazy but with no luck. Would really appreciate someone's help for this bizarre error

  • `int sieve[usernum-1]` <- That compiles? O.o (Isn't it only valid to pass in `const` values in such a case to size an array like that?) – user123 Mar 30 '13 at 03:51

2 Answers2

2

You aren't initializing the sieves array to 0s. So you're looping from 0 to usernum-1, printing out every number that isn't a 0. Since you didn't initialize the array, the 2nd element is a random value and is being printed out

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

This code is a problem:

(*array) = sieve;

You are are assigning the address of sieve, a temporary local array, to *array. You need to copy the array contents instead.

Are you also this person who has asked three questions about identical code?

Community
  • 1
  • 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70