Day 7 HW
1) Two Dice Role
At first I managed to program a dice to generate random numbers between 1-6. So i just repeated that for the second dice. The problem now is to allow input from the user to allow how many times they wanted to role the dice. Instead of using a "for" loop and creating a new variable and setting it less then and equal to the desired user input, I used a while statement. Basically telling the program to increment the numbers of roles by one until it is equal to the number of the user input. My program ran infinitely, not knowing at the time I told the program while the number of roles is equal to user input, keep incrementing the number of roles. But since the number of roles always equal the number of the input it keeps looping infinitely. I tried debugging for an hour before rage quitting for another two hours. After I went on google and went on different forums then figured out that I have to set the number of roles < user input because in a while loop it will keep looping until the statement is false. After more hours of suffering I managed to get everything but the probability to work out it just reads zero.Not too sure how to fix it right now i'll probably ask the SI tutor for help.
2) For the second problem I had to ask my cousin who is a cs major to help me with this problem. Especially with the while loop and the returning the Stirling value. It's 3:30 in the morning also so I'm really glad I finished debugging the code with him, god bless him. I'll probably ask the SI tutor the return part, and the if statment in the while loop.
Example code problem1:
/* Two dice problem */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
//Declare the required variables
int No_of_rolls=0,First_die,Second_die,requested=0,count=0, TOTAL = 8;
//Declare the function prototype
int rand_int(int min, int max);
//Refresh the screen
//Get the roll count
printf("\n\n Enter the requested number of dice rolls:");
scanf("%u", &requested);
//Loop executes until the condition is FALSE
while(No_of_rolls < requested)
{
//increment the rolls
No_of_rolls++;
//Store the random value in First_die
First_die=rand_int(1,6);
//Store the second value in Second_die
Second_die=rand_int(1,6);
//CHeck if the sum of two dices is equal to TOTAL
if(First_die+Second_die==TOTAL)
//Increment the value of count
count++;
printf("Output for Dice 1 and 2: %u %u\n",First_die,Second_die);
}
//Print the number of rolls
printf("\n Total numbers of rolls: %i\n",No_of_rolls);
//Print the total and count
printf("Number of 8s: %d \n",count);
printf("Percentage of %is: %f\n",TOTAL,(100*count/No_of_rolls));
return 0;
}
int rand_int(int min, int max)
{
return rand()%(max-min+1)+min;
}
Example code for problem 2:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define EULER 2.718282
#define PI 3.141593
int main(void)
{
//Declare the variable
int n_val;
//Functiuon Prototype
int n_val_fact(int n_val);
//Read the user input
printf("Enter the value for n:");
scanf("%d",&n_val);
// Check whether the input value is negativen or not, if yes then the while loops executes until a positive value is acheieved
while(n_val<0)
{
printf(" The n value must be positive \n");
printf("Enter the value of n:");
scanf("%d",&n_val);
}
printf("Stirling approzimation value about %i is %i \n", n_val, n_val_fact(n_val));
return 0;
}
// THe function n_val is being used to calculate the stirling approximation
int n_val_fact(int n_val)
{
//return approximate value
if(n_val == 0)
{
return 0;
}
else
{
return (int)((sqrt(2*PI*n_val))*pow((n_val/EULER),n_val)+0.55
);
}
}
Example of code1 not working (number of roles = user input)
Example of code 1 working (mostly):
Example of Sterling approximation:
At first I managed to program a dice to generate random numbers between 1-6. So i just repeated that for the second dice. The problem now is to allow input from the user to allow how many times they wanted to role the dice. Instead of using a "for" loop and creating a new variable and setting it less then and equal to the desired user input, I used a while statement. Basically telling the program to increment the numbers of roles by one until it is equal to the number of the user input. My program ran infinitely, not knowing at the time I told the program while the number of roles is equal to user input, keep incrementing the number of roles. But since the number of roles always equal the number of the input it keeps looping infinitely. I tried debugging for an hour before rage quitting for another two hours. After I went on google and went on different forums then figured out that I have to set the number of roles < user input because in a while loop it will keep looping until the statement is false. After more hours of suffering I managed to get everything but the probability to work out it just reads zero.Not too sure how to fix it right now i'll probably ask the SI tutor for help.
2) For the second problem I had to ask my cousin who is a cs major to help me with this problem. Especially with the while loop and the returning the Stirling value. It's 3:30 in the morning also so I'm really glad I finished debugging the code with him, god bless him. I'll probably ask the SI tutor the return part, and the if statment in the while loop.
Example code problem1:
/* Two dice problem */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
//Declare the required variables
int No_of_rolls=0,First_die,Second_die,requested=0,count=0, TOTAL = 8;
//Declare the function prototype
int rand_int(int min, int max);
//Refresh the screen
//Get the roll count
printf("\n\n Enter the requested number of dice rolls:");
scanf("%u", &requested);
//Loop executes until the condition is FALSE
while(No_of_rolls < requested)
{
//increment the rolls
No_of_rolls++;
//Store the random value in First_die
First_die=rand_int(1,6);
//Store the second value in Second_die
Second_die=rand_int(1,6);
//CHeck if the sum of two dices is equal to TOTAL
if(First_die+Second_die==TOTAL)
//Increment the value of count
count++;
printf("Output for Dice 1 and 2: %u %u\n",First_die,Second_die);
}
//Print the number of rolls
printf("\n Total numbers of rolls: %i\n",No_of_rolls);
//Print the total and count
printf("Number of 8s: %d \n",count);
printf("Percentage of %is: %f\n",TOTAL,(100*count/No_of_rolls));
return 0;
}
int rand_int(int min, int max)
{
return rand()%(max-min+1)+min;
}
Example code for problem 2:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define EULER 2.718282
#define PI 3.141593
int main(void)
{
//Declare the variable
int n_val;
//Functiuon Prototype
int n_val_fact(int n_val);
//Read the user input
printf("Enter the value for n:");
scanf("%d",&n_val);
// Check whether the input value is negativen or not, if yes then the while loops executes until a positive value is acheieved
while(n_val<0)
{
printf(" The n value must be positive \n");
printf("Enter the value of n:");
scanf("%d",&n_val);
}
printf("Stirling approzimation value about %i is %i \n", n_val, n_val_fact(n_val));
return 0;
}
// THe function n_val is being used to calculate the stirling approximation
int n_val_fact(int n_val)
{
//return approximate value
if(n_val == 0)
{
return 0;
}
else
{
return (int)((sqrt(2*PI*n_val))*pow((n_val/EULER),n_val)+0.55
);
}
}
Example of code1 not working (number of roles = user input)
Example of code 1 working (mostly):
Example of Sterling approximation:
Comments
Post a Comment