Program to perform the average of a series of random dice rolls by means of functions

1

good, the program is based on making a series of random rolls of a die and make the average of this series of rolls. The number of runs is requested by the screen. The program is executed using the getRoundand perform Media functions. I do not know why but the average or I get -0.000000 or a very very large number. I'm going crazy.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000
#define M 6


int obtenerRonda(int tirada[]);
double obtenerMedia(int tirada[],int n);


int main(void) {

int tirada[N];
double media=0.0;
int n;

obtenerRonda(tirada);

media=obtenerMedia(tirada,n);

printf("\nLa media es %lf ",media);


return 0;

}


int obtenerRonda(int tirada[]){

int i;
int n;
srand(time(NULL));

printf("Introduzca el numero de tiradas : ");
scanf("%d",&n);

        for(i=0;i<n;i++){
            tirada[i]=1+(rand()%M);
            printf("   %d   ",tirada[i]);
                        }

         
}


double obtenerMedia(int tirada[],int n){

int i;
double media=0.0;
int suma=0;

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

    suma=suma + tirada[i];

}

media=suma/n;

return media;

}
    
asked by cornetto 07.11.2017 в 22:29
source

1 answer

0

The error you have is that the variable int n defined in main() , has a garbage value, therefore it is likely that you are accessing undefined memory sections within the array tirada[] .

In the obtenerRonda(tirada) function you pass as a parameter only the tirada[] array that will store the random dice rolls. In the function you also ask the user to define the number of runs, which you keep in the local variable n . Recall that once the execution leaves the function, the value stored in n will no longer be available.

The three options you have are:

  • Define int n as global
  • Pass a reference or pointer to n as an argument for obtenerRonda()
  • Order the number of runs from main() and pass that number by value to both functions.

The option that I consider the most simple is the third one. The code then is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000
#define M 6

int obtenerRonda(int tirada[], int);
double obtenerMedia(int tirada[],int n);

int main(void) 
{
    int tirada[N];
    double media=0.0;
    int n;

    printf("Introduzca el numero de tiradas : ");
    scanf("%d",&n);

    obtenerRonda(tirada, n);

    media=obtenerMedia(tirada,n);

    printf("\nLa media es %lf ",media);

    return 0;
}


int obtenerRonda(int tirada[], int n)
{
    int i;

    srand(time(NULL));

    for(i=0;i<n;i++)
    {
        tirada[i]=1+(rand()%M);
        printf("   %d   ",tirada[i]);
    } 
}

double obtenerMedia(int tirada[],int n)
{
    int i;
    double media = 0.0;
    float suma = 0.0;

    for(i=0;i<n;i++)
    {
        suma=suma + tirada[i];
    }

    media = suma / n;

    return media;
}

You'll notice that I just put the following code in main() :

printf("Introduzca el numero de tiradas : ");
scanf("%d",&n);

And I also added a new argument n to the function obtenerRonda()

Now, you will notice that I also changed the type of suma to float . This assuming that you want to have decimal values in the media = suma / n operation (apparently if you defined media as double ). The change is because an entire division will truncate the decimals.

    
answered by 07.11.2017 в 23:31