mardi 22 mars 2016

how to get my random function to work?

The random function is not working in the parameters set and I do not know why. Can anyone help? I need random numbers between 18 and 38 and I can't seem to get that and I do not know why.

Here's my code

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

struct tires
{
    char Manufacturer[40];
    int tire_pressure[2];
    int pressure_change;
}typedef tires;

void getTireInformation(tires*, int);
void tirePressure(tires*, int);
int main()
{
    tires tire[4];
    tires* ptire = &tire[0];

    getTireInformation(ptire, 4);
    tirePressure(ptire, 4);

    return 0;
}

void getTireInformation(tires* ptire, int size)
{
    int i = 0;
    for (i = 0; i < size; i++)
    {
        printf("please enter Make for the tire: \n");
        scanf("%s", &(ptire + i) ->Manufacturer);
    }

    printf("all tire make you entered ...just for verification:\n");
    for(i = 0; i < size; i++)
        printf("%s\n",(ptire +i) ->Manufacturer);
}

void tirePressure(tires* ptire, int size)
{
    int i = 0;
    int min = 18;
    int max = 38;
    for (i = 0; i < size; i++)
    {
        srand(time(NULL));
        ptire = rand()%(max - min)-min;
        printf("%d\n", (ptire + i) -> tire_pressure);
    }
}


Edit: Here's my updated function after making the suggested fixes

void tirePressure(tires* ptire, int size)
{
    int i = 0;
    int min = 18;
    int max = 38;
    for (i = 0; i < size; i++)
    {
        ptire = rand()%(max - min + 1) + min;
        printf("%d\n", (ptire + i) -> tire_pressure);
    }
}




Aucun commentaire:

Enregistrer un commentaire