vendredi 12 février 2021

My function for generating random numbers isn't going past 500000

I'll get right into my problem. So basically what I want to do is to generate an array of random numbers of different amounts. So one with 10,000, 50,000, 100,000, 500,000, 600,000, etc. Then I would sort them using quicksort and print the sorted array to the screen. Additionally, the time taken for it to run would be recorded and printed as well. The only part I'm having problems with however is generating the array. For some reason generating past 500,000 random numbers does not work and returns this:


Process exited after 2.112 seconds with return value 3221225725

Press any key to continue . . . ([1]: https://i.stack.imgur.com/m83el.png)

This is my code:

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

void randNums(int array[], int range){
    int i, num;
    for(i=0; i < range; i++){
        num = rand () % range;
        array[i] = num;
    }
}


//prints elements of given array
void display(int array[], int size){
    int i;
    for(i = 0; i < size; i++){
        printf("#%d. %d\n", i, array[i]);
    }
}

//displays time taken for sorting algorithm to run
void timeTaken(char sortingAlgo[], int size, clock_t start, clock_t end){
    double seconds = end - start;
    double milliseconds = seconds / 1000;
    printf("Time taken for %s Sort to sort %d numbers was %f milliseconds or %f seconds", sortingAlgo, size, milliseconds, seconds);        
}
 
//quick sort
void quickSort(int array[],int first,int last){
   int i, j, pivot, temp;
   if(first<last){
      pivot=first;
      i=first;
      j=last;
      while(i<j){
         while(array[i]<=array[pivot]&&i<last)
            i++;
         while(array[j]>array[pivot])
            j--;
         if(i<j){
            temp=array[i];
            array[i]=array[j];
            array[j]=temp;
         }
      }
      temp=array[pivot];
      array[pivot]=array[j];
      array[j]=temp;
      quickSort(array,first,j-1);
      quickSort(array,j+1,last);
   }
}

int main(){ 
    int size = 600000;
    int myArray[size];
    time_t end, start;
    int first, last;

    randNums(myArray, size);    
    first = myArray[0];
    last = sizeof(myArray)/sizeof(myArray[0]);
    
    time(&start);
    quickSort(myArray, first, last);
    time(&end); 
    display(myArray, size);
    timeTaken("Quick", size, start, end);

    
    
    return 0;
}

Any help would be greatly appreciated, Thank you!




Aucun commentaire:

Enregistrer un commentaire