lundi 28 janvier 2019

allocate random float numbers to an array withing a range specified by the user

I do not know where the problem lies

int main()
{

    int antalVarde = 0;
    low=0, high=0;
    float* arr;
    srand((int)time(NULL));

    printf("Hur många värden ska skapas: ");
    scanf_s("%d", &antalVarde);
    printf("Ange min-värde: ");
    scanf_s("%d", &low);
    printf("Ange max-värde: ");
    scanf_s("%d", &high);


    arr = (float*)malloc(antalVarde * sizeof(float));

    for (int i = 0; i <= antalVarde; i++)
    {
        *arr = RandomReal(low, high);
    }
    printf("%f", *arr);

    getchar();
    return 0;
}


float RandomReal(float low, float high)
{


    float d;

    d = (float) rand() / ((float) RAND_MAX + 1);
    return (low + d * (high - low));
}




Aucun commentaire:

Enregistrer un commentaire