0

I am working on a dice rolling program that I need help with. This is what I've written so far:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


int main()
{
    int i;
    int rollDice;
    int firInp;
    int secInp;

    printf("Enter the amount of faces you want your dice to have: ");
    scanf("%d", &firInp);
    printf("Enter the amount of throws you want: ");
    scanf("%d", &secInp);

    for(i = 0; i < secInp; i++){
        rollDice = (rand()%firInp) + 1;
        printf("%d \n", rollDice);
    }

    return 0;
}

for example for the following throws [1,5,5,5,5,3], 1 and 3 have a percentage of 16.6%. and 5 of 66.6% How do I print the occurrence percentage for the Dice throws?

  • 1
    1) Count how many times those values come up. 2) Divide by the number of throws. 3) Print the results. Could you please specify which one of those steps is the one you have problems implementing? – Bob__ Dec 13 '20 at 19:41
  • Does this answer your question? [Generate histogram in c](https://stackoverflow.com/questions/49468519/generate-histogram-in-c) – Robert Harvey Dec 13 '20 at 19:45
  • I did that. I made a new variable called percentage, and store in it the value of (rollDice/secInp)*100. but when i run it, it just gives me all the values as 200 – TheQuest007 Dec 13 '20 at 19:46
  • @TheQuest007 *"I did that"* - but didn't show it. The code you're *not* showing us doesn't work? Unless it's in the question we can't tell you why. We're not mind-readers. – WhozCraig Dec 13 '20 at 19:51
  • i understand that. i'll edit it – TheQuest007 Dec 13 '20 at 20:01

1 Answers1

0

To calculate a percentage, you need two variables:

  • The number of items of interest
  • The number of all items

So if you roll a dice, and it is not the number you want, add one to all_items. If it is the number you want, add one to all_items and add one to items_of_interest.

Then you divide items_of_interest by all_items and multiply by 100.

100.0d*((double)items_of_interest)/((double)all_items)

If you want to calculate multiple percentages (a percentage for 1, a percentage for 2, etc.) you will need multiple variables (or an array) to hold the different items of interest.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138