Home > Enterprise >  Array and loop implementation for a dynamic set?
Array and loop implementation for a dynamic set?

Time:11-12

my school project has me coding a dice with user entered number of face and throws (between 1 and 25 and 1 and 500 respectively). My code should be able to count the number of occurrences as well as a percentage and print that too. I came up with something but I am stuck with coding the occurrences bit. I know I should use arrays and loops but I think my implementation is wrong.Any help would be tremendously appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()

{
    int i;
    int rollDice;
    int diceFace[25] = {0};
    int diceThrows[500] = {0};
   
    srand(time(NULL));
   

    printf("Number of faces: ");
    scanf("%d", &diceFace);
    if (diceFace > 1 && diceFace < 25)
    printf("Range is acceptable");
    else
    printf("Range is unacceptable");

    printf("\nNumber of throws: ");
    scanf("%d", &diceThrows);
     if (diceThrows > 1 && diceThrows < 500)
    printf("Range is acceptable\n");
    else
    printf("Range is unacceptable");
   
  for (i = 0; i < ; i  )
    {
        diceThrows[i] = (rand() % diceFace   1);
        diceFace[diceThrows[i]-1]  ;
        printf("%d \n", diceThrows[i]);
       
    } for (i = 0; i < diceFace; i  )
    {
        printf("The value [%d] was rolled %d times.\n", i 1, diceFace[i]);
       
    }

This is what I got so far. The arrays do not seem to work. I know I should code an array and a loop for the throws and the accompanying faces and tally the occurrence of each number. The issue is that doing this in a dynamic set introduces a complexity to the problem.

CodePudding user response:

You are mixing up some requirements here. As far as I can tell (filling in some blanks in your example), the arrays diceThrows and diceFace should hold the sequence of dice rolls, and the frequency of each roll, respectively.

Thus,

scanf("%d", &diceFace);

and

scanf("%d", &diceThrows);

do not make much sense, as they are trying to read a single int, but are given the address of an array as the location to write the data.

You need two other variables, separate from the arrays, to hold the values the user enters.

The 1 and - 1 used here

diceThrows[i] = (rand() % diceFace   1);
diceFace[diceThrows[i]-1]  ;

are not needed.

If your die has 16 faces, then those faces are numbered 0 to 15. Make the adjustment by adding one only when presenting this data to the user.


Your program should not continue if scanf fails, or the values entered are outside the acceptable range. Doing so leaves your program susceptible to more Undefined Behaviour when it tries to utilize uninitialized or improper data.


Refactored:

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

#define MAX_FACES 25
#define MAX_THROWS 500

int get_int_in_range(int low, int high)
{
    int n;

    if ((1 != scanf("%d", &n)) || low > n || n > high) {
        fprintf(stderr, "Value is unacceptable.\n");
        exit(EXIT_FAILURE);
    }

    puts("Value is acceptable.");

    return n;
}

int main(void)
{
    srand((unsigned) time(NULL));

    printf("Number of faces: ");
    int faces = get_int_in_range(1, MAX_FACES);

    printf("Number of throws: ");
    int throws = get_int_in_range(1, MAX_THROWS);

    int frequency[MAX_FACES] = { 0 };
    int sequence[MAX_THROWS] = { 0 };

    printf("The sequence of throws is: ");

    for (int i = 0; i < throws; i  ) {
        int result = rand() % faces;

        frequency[result]  ;

        /* save the sequence, not completely necessary */
        sequence[i] = result;
        /* since we can print as we go */
        printf("%d ", result   1);
    }

    putchar('\n');

    for (int i = 0; i < faces; i  )
        printf("The value [%d] was rolled %d times (%.2lf%%).\n",
                i   1, frequency[i], (frequency[i] / (double) throws) * 100.0);
}

Using this program:

$ ./a.out
Number of faces: 6 
Value is acceptable.
Number of throws: 10
Value is acceptable.
The sequence of throws is: 5 6 5 6 6 4 2 2 1 6 
The value [1] was rolled 1 times (10.00%).
The value [2] was rolled 2 times (20.00%).
The value [3] was rolled 0 times (0.00%).
The value [4] was rolled 1 times (10.00%).
The value [5] was rolled 2 times (20.00%).
The value [6] was rolled 4 times (40.00%).
  • Related