Hey jus a small prob with percentages

doing some work for tafe here and got everything working for a dice simulator that needs to simulate 10000 throws of 2 dice and calculating how many times each number (2-12) comes up along with percentages and unfortunately it has to also stop between each loop thus the whole not using a switch and all the messy printf spam
really new to programming tbh but got everything working except the percentages and cant work out why any help would be much appreciated :D

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

void main()

{
float Percentage1;
float Percentage2;
float Percentage3;
float Percentage4;
float Percentage5;
float Percentage6;
float Percentage7;
float Percentage8;
float Percentage9;
float Percentage10;
float Percentage11;
int Throws = 0;
int Target1 = 2;
int Target2 = 3;
int Target3 = 4;
int Target4 = 5;
int Target5 = 6;
int Target6 = 7;
int Target7 = 8;
int Target8 = 9;
int Target9 = 10;
int Target10 = 11;
int Target11 = 12;
int Hit1 = 0;
int Hit2 = 0;
int Hit3 = 0;
int Hit4 = 0;
int Hit5 = 0;
int Hit6 = 0;
int Hit7 = 0;
int Hit8 = 0;
int Hit9 = 0;
int Hit10 = 0;
int Hit11 = 0;
int Roll1 = 0;
int Roll2 = 0;
int Dice = 0;

for ( Throws = 0;Throws < 10000;Throws++)
{
Roll1 = rand() % 6 + 1;
Roll2 = rand() % 6 + 1;
Dice =Roll1 + Roll2;

if (Dice == 2)
{
Hit1++;
}
if (Dice == 3)
{
Hit2++;
}
if (Dice == 4)
{
Hit3++;
}
if (Dice == 5)
{
Hit4++;
}
if (Dice == 6)
{
Hit5++;
}
if (Dice == 7)
{
Hit6++;
}
if (Dice == 8)
{
Hit7++;
}
if (Dice == 9)
{
Hit8++;
}
if (Dice == 10)
{
Hit9++;
}
if (Dice == 11)
{
Hit10++;
}
if (Dice == 12)
{
Hit11++;
}

}
Percentage1 = Hit1 / 100;
Percentage2 = Hit2 / 100;
Percentage3 = Hit3 / 100;
Percentage4 = Hit4 / 100;
Percentage5 = Hit5 / 100;
Percentage6 = Hit6 / 100;
Percentage7 = Hit7 / 100;
Percentage8 = Hit8 / 100;
Percentage9 = Hit9 / 100;
Percentage10 = Hit10 / 100;
Percentage11 = Hit11 / 100;
printf(" Target Hits Percentage \n");
printf(" %d, %d, %f",Target1 ,Hit1 , Percentage1);
system("PAUSE");
printf(" %d, %d, %f",Target2 ,Hit2 , Percentage2);
system("PAUSE");
printf(" %d, %d, %f",Target3 ,Hit3 , Percentage3);
system("PAUSE");
printf(" %d, %d, %f",Target4 ,Hit4 , Percentage4);
system("PAUSE");
printf(" %d, %d, %f",Target5 ,Hit5 , Percentage5);
system("PAUSE");
printf(" %d, %d, %f",Target6 ,Hit6 , Percentage6);
system("PAUSE");
printf(" %d, %d, %f",Target7 ,Hit7 , Percentage7);
system("PAUSE");
printf(" %d, %d, %f",Target8 ,Hit8 , Percentage8);
system("PAUSE");
printf(" %d, %d, %f",Target9 ,Hit9 , Percentage9);
system("PAUSE");
printf(" %d, %d, %f",Target10 ,Hit10 , Percentage10);
system("PAUSE");
printf(" %d, %d, %f",Target11 ,Hit11 , Percentage11);
system("PAUSE");
}

The problem is it comes up with whole percentages (2.0000 instead of 2.5) and i cant figure out why as im using a float
Last edited on
You need to learn how to use arrays before continuing with this.
Well everything in this assignment is working to a T except the percentages and if there a problem with the float arrays or mayhap one of the int arrays thats exactly why ive posted this to find out what the problem is and learn from it
The problem is that you're not using arrays. This program could be less than ten lines.
ah okay unfortunately we havent been taught arrays yet so i might just have to see if i can find some information online about them and mayhap shorten/neaten this up in the process
thank you very much for your help
I have played around with it a bit n this is now what ive got (switched a lot of them from int to float now to try and fix it)

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

void main()

{
float Percentage[11];
int Throws = 0;
float Target [11];
Target[0] = 2, Target[1] = 3, Target[2] = 4, Target[3] = 5, Target[4] = 6, Target[5] = 7, Target[6] = 8, Target[7] = 9, Target[8] = 10, Target[9] = 11, Target[10] = 12;
float Hit[11];
Hit[0] = 0,Hit[1] = 0, Hit[2] = 0, Hit[3] = 0, Hit[4] = 0, Hit[5] = 0, Hit[6] = 0, Hit[7] = 0, Hit[8] = 0, Hit[9] = 0, Hit[10] = 0;
float Roll1 = 0;
float Roll2 = 0;
float Dice = 0;

for ( Throws = 0;Throws < 10000;Throws++)
{
Roll1 = rand() % 6 + 1;
Roll2 = rand() % 6 + 1;
Dice =Roll1 + Roll2;

if (Dice == 2)
{
Hit[0]++;
}
if (Dice == 3)
{
Hit[1]++;
}
if (Dice == 4)
{
Hit[2]++;
}
if (Dice == 5)
{
Hit[3]++;
}
if (Dice == 6)
{
Hit[4]++;
}
if (Dice == 7)
{
Hit[5]++;
}
if (Dice == 8)
{
Hit[6]++;
}
if (Dice == 9)
{
Hit[7]++;
}
if (Dice == 10)
{
Hit[8]++;
}
if (Dice == 11)
{
Hit[9]++;
}
if (Dice == 12)
{
Hit[10]++;
}

}
Percentage[0]= Hit[0] / 100, Percentage[1] = Hit[1] / 100, Percentage[2] = Hit[2] / 100, Percentage[3] = Hit[3] / 100, Percentage[4] =Hit[4] / 100;
Percentage[5] =Hit[5] / 100, Percentage[6] =Hit[6] / 100, Percentage[7] =Hit[7] / 100 , Percentage[8] =Hit[8] / 100, Percentage[9] =Hit[9] / 100, Percentage[10] =Hit[10] / 100;
printf(" Target Hits Percentage \n");
printf(" %d, %d, %f",Target[0] ,Hit[0] , Percentage[0]);
system("PAUSE");
printf(" %d, %d, %f",Target[1] ,Hit[1] , Percentage[1]);
system("PAUSE");
printf(" %d, %d, %f",Target[2] ,Hit[2] , Percentage[2]);
system("PAUSE");
printf(" %d, %d, %f",Target[3] ,Hit[3] , Percentage[3]);
system("PAUSE");
printf(" %d, %d, %f",Target[4] ,Hit[4] , Percentage[4]);
system("PAUSE");
printf(" %d, %d, %f",Target[5] ,Hit[5] , Percentage[5]);
system("PAUSE");
printf(" %d, %d, %f",Target[6] ,Hit[6] , Percentage[6]);
system("PAUSE");
printf(" %d, %d, %f",Target[7] ,Hit[7] , Percentage[7]);
system("PAUSE");
printf(" %d, %d, %f",Target[8] ,Hit[8] , Percentage[8]);
system("PAUSE");
printf(" %d, %d, %f",Target[9] ,Hit[9] , Percentage[9]);
system("PAUSE");
printf(" %d, %d, %f",Target[10] ,Hit[10] , Percentage[10]);
system("PAUSE");
}

So ive fixed the array problem took a bit of learning but it was pretty easy but does anyone have a different way of fixing the percentages problem other than type-casting them? as the teacher is not very happy about doing that.... ever! :/
Last edited on
#include<stdlib.h>
#include<stdio.h>

void main()

{
float Percentage[11];
int Throws = 0;
int Hit[11] = {0,0,0,0,0,0,0,0,0,0,0};
int Roll1 = 0;
int Roll2 = 0;
int Target = 0;

for ( Throws = 0;Throws < 10000;Throws++)
{
Roll1 = rand() % 6 + 1;
Roll2 = rand() % 6 + 1;
Hit[Roll1 + Roll2 -2]++;
}
for (Target = 0; Target < 11; Target ++)
{
Percentage[Target] = (float)Hit[Target] / 100;
}
printf(" Target Hits Percentage \n");
for (Target = 0; Target < 11; Target ++)
{
printf(" %d %d %f", Target +2, Hit[Target] , Percentage[Target]);
system("PAUSE");
}
}

And then my dad helped and everything was K all solved ty nyway
Topic archived. No new replies allowed.