Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C Random Number generator returning address

A part of my code is to randomly generate an array with numbers between 0 and 999. I do this with a random number generator. However everytime it does it and returns the array, the intArray[8] to intArray[12] have what I presume is the address. How can I fix this. I have printed each part of the array in the randomArr function and they are fine.

Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
#include "sorting.h"


void initRandom()
{
    srand(time(NULL));
}

int random(int low, int high)
{
    int number = -1;

    if(low <= high)
    {
        number = (rand() % (high-low+1)) + low;
    }

    return number;
}

void randomArr(int* numArray, int intNum){
    int j = 0;
    initRandom();
    for (j = 0; j < intNum; j++){
        numArray[j] = random(0, 999);
    }
} 

int main(int argc, char* argv[]){
    int* intArray = (int*)malloc(sizeof(double));
    if (argc < 2){
        printf("Error! Not enough arguments!");
    } else if (argc > 3){
        printf("Error! Too many arguments!");
    } else{
        int intNum, i;

        if (argc == 2){
            printf("Please specify number of integers: ");
            scanf("%d", &intNum);
            randomArr(intArray, intNum);

        }
        for (i = 0; i < intNum; i++){
            printf("Num %d: %d\n", i+1, intArray[i]);
        }
    }
    free(intArray);
    return 0;
}

One output example with 20 ints is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

./sorting 1
Please specify number of integers: 20
Num 1: 97
Num 2: 971
Num 3: 579
Num 4: 223
Num 5: 905
Num 6: 883
Num 7: 711
Num 8: 757
Num 9: 544044366
Num 10: 891304505
Num 11: 858863928
Num 12: 842609462
Num 13: 2610
Num 14: 283
Num 15: 568
Num 16: 319
Num 17: 426
Num 18: 443
Num 19: 848
Num 20: 572

>Solution :

the int* intArray = (int*)malloc(sizeof(double)); is allocating a memory buffer of a single sizeof(double) (typically 4 or 8 bytes). Ive changed that to int* intArray = (int*)malloc(sizeof(int) * intNum); to malloc the amount of ints you are going to store. To do that i moved the malloc down into the if statement and moved the free up into the same if statement.

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


void initRandom()
{
    srand(time(NULL));
}

int random(int low, int high)
{
    int number = -1;

    if (low <= high)
    {
        number = (rand() % (high - low + 1)) + low;
    }

    return number;
}

void randomArr(int* numArray, int intNum) {
    int j = 0;
    initRandom();
    for (j = 0; j < intNum; j++) {
        numArray[j] = random(0, 999);
    }
}

int main(int argc, char* argv[]) {
    
    if (argc < 2) {
        printf("Error! Not enough arguments!");
    }
    else if (argc > 3) {
        printf("Error! Too many arguments!");
    }
    else {
        int intNum, i;

        if (argc == 2) {
            printf("Please specify number of integers: ");
            scanf("%d", &intNum);
            int* intArray = (int*)malloc(sizeof(int) * intNum);
            randomArr(intArray, intNum);

            for (i = 0; i < intNum; i++) {
                printf("Num %d: %d\n", i + 1, intArray[i]);
            }
            free(intArray);
        }
    }
    
    return 0;
}

output:

Please specify number of integers: 20
Num 1: 218
Num 2: 837
Num 3: 797
Num 4: 190
Num 5: 555
Num 6: 644
Num 7: 158
Num 8: 673
Num 9: 837
Num 10: 963
Num 11: 936
Num 12: 101
Num 13: 721
Num 14: 257
Num 15: 541
Num 16: 941
Num 17: 285
Num 18: 958
Num 19: 591
Num 20: 971
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading