Jclee
Jclee

Reputation: 39

C, using random and storing them in an array

Just trying to generate 25 random numbers between 60-100 and storing them in an array. Need some confirmation though. Is this how it would look?

int main () {

int temp[25], i, sum;


srand( (unsigned) time(NULL) );

for (i=0; i <= 25; i++) {

get_value(i);
sum += temp[25];  //Eventually will be added together to find avg

};


}


int get_value(int temp_random) {
    return((rand()%40)+60);

}

Upvotes: 0

Views: 118

Answers (3)

MikeCAT
MikeCAT

Reputation: 75062

You invoked undefined behavior by

  • using out-of-range array subscript.
  • using value of uninitialized variable sum having automatic storage duration, which is indeterminate.

The line sum += temp[25]; should be sum += temp[i];,
and the loop condition should be i < 25 or i < (int)(sizeof(temp)/sizeof(*temp)) instead of i <= 25.

Also sum have to be initialized.

Bisides of the undefined behavior, your program has following problems:

  • What get_value() returns is thrown away.
  • get_value() has unused and seems meaningless arugment temp_random.
  • Functions are used before they are declared.
  • get_value() won't return 100 because rand()%40 will be only numbers between 0 and 39, inclusive.

Also note that the semicolon fter the block that belongs to the for loop is not needed. (This is not harmful though)

Your code should be like this:

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

#define GENERATE_NUM 25
#define GENVALUE_MIN 60
#define GENVALUE_MAX 100

int get_value(void);

int main (void) {

    int temp[GENERATE_NUM], i, sum = 0;

    srand( (unsigned) time(NULL) );

    for (i=0; i < GENERATE_NUM; i++) {

        temp[i] = get_value();
        sum += temp[i];  //Eventually will be added together to find avg

    }

    return 0;
}

int get_value(void) {
    return((rand() % (GENVALUE_MAX - GENVALUE_MIN + 1)) + GENVALUE_MIN);

}

Upvotes: 2

tdao
tdao

Reputation: 17678

Many issues:

  • Your get_value taking a param but never used it.
  • Your random calculation wasn't correct.
  • Your index i was wrong because it ran out-of-bound.
  • Your for loop ended with ; which was incorrect as well.

Refer to this running code - don't forget #include<limits.h>

int get_value( int a, int b ); // <-- allow to get a random number in range [a, b]

int main() 
{
    int temp[25], i, sum;
    srand( (unsigned) time(NULL) );
    for (i=0; i < 25; i++) {
        temp[i] = get_value( 60, 100 );
        printf( "tmp[%d] = %d\n", i, temp[i] );
    }
}

int get_value( int a, int b ) {
    return a + rand() % (b + 1 - a);
}

Upvotes: 2

bobasti
bobasti

Reputation: 1918

The formula for generating a random number between a and b is: rand() % (b-a+1) + a.
That said, your for-loop should be looking something like this:

for (i = 0; i < 25; i++) {
    temp[i] = rand() % (100-60+1) + 60;
    sum += temp[i];
}

Also, pay attention to what you did in the for-loop condition (i <= 25). This would go from 0 (including) to 25 (including), but the last index of your array is 24.

Upvotes: 1

Related Questions