I am having trouble with a programming assignment in my c programming course and
ID: 3808176 • Letter: I
Question
I am having trouble with a programming assignment in my c programming course and was wondering if I could get any help or guidance.
Here is the description of what the program should do: the program should ask the user how many die rolls to simulate. the program should ask the user how many die rolls to simulate. The program should support from 1 to at least 100,000 simulated rolls. Once the user has entered the number of die rolls to simulate, the program should use repeated calls to C's rand() function (along with proper scaling and shifting) to simulate the proper number of rolls of a single six-sided die. After the rolls have been simulated, your program should print out some statistics about the rolls. This should include: 1. Total number of rolls simulated. 2. Number of times each number was rolled. 3. Percentage of the time each number was rolled, as a percentage of the total number of rolls. 4. Average value of rolls (i.e., sum of the value of all rolls divided by the total number of rolls). 5. Standard deviation of rolls (i.e., square root of average of squared differences from the mean).
And these are some other things about the assignment: 1. All die rolls should be stored in an array (remember you'll need an array of at least size 100,000 since the program should be able to handle at least 100,000 rolls). 2. The array which holds the die rolls should be declared inside of main(). 3. The code which actually simulates the die rolls should be in its own function, and not part of main. You'll have to pass the array of rolls and the number of rolls into this function. 4. The code which prints out the statistics about the rolls should be in its own function, and not part of main. You'll have to pass the array of rolls and the number of rolls into this function. 5. At the start of the program, the random number generator should be seeded using the current system time (if you have a question on this, check in the text – it covers it explicitly). 6. Each function should have a comment block preceding it which explains what the function does (this includes main). 7. Your program should check to ensure the user hasn't entered an invalid number of rolls (for example 0, -1 are invalid) or more rolls than your program can handle (for example, if your array is only size 100,000 and the user enters 200,000). If the user enters an invalid number, it should ask the user to enter another number until a valid number is entered. I know it is a lot, but any help or guidance would be greatly appreciated.
Also, I am using Visual Studio if that makes any difference.
Explanation / Answer
#include<stdio.h>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
void dice(int arr[],int n) /*Dice Rolled*/
{
int iloop;
for(iloop=0;iloop<n;iloop++)
{
arr[iloop]=((rand() % 6) + 1);
}
}
void total(int arr[],int n) /*prints total times dice rolled*/
{
printf("Total Number Of Times Rolls Simulated:%d ",n);
}
void calc(int arr[],int n) /*calculate occurence and percentage of elements and prints it*/
{
int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0,iloop;
float per1,per2,per3,per4,per5,per6;
for(iloop=0;iloop<n;iloop++)
{
if(arr[iloop]==1)
cnt1++;
if(arr[iloop]==2)
cnt2++;
if(arr[iloop]==3)
cnt3++;
if(arr[iloop]==4)
cnt4++;
if(arr[iloop]==5)
cnt5++;
if(arr[iloop]==6)
cnt6++;
}
per1=((float)cnt1/n)*100;
per2=((float)cnt2/n)*100;
per3=((float)cnt3/n)*100;
per4=((float)cnt4/n)*100;
per5=((float)cnt5/n)*100;
per6=((float)cnt6/n)*100;
printf("OCCURENCES OF: 1:%d 2:%d 3:%d 4:%d 5:%d 6:%d ",cnt1,cnt2,cnt3,cnt4,cnt5,cnt6);
printf("PERCENTAGE OF: 1:%f 2:%f 3:%f 4:%f 5:%f 6:%f ",per1,per2,per3,per4,per5,per6);
}
void stddaviation(int arr[],int n) /*calculate standard daviation and prints it..*/
{
long int sum=0;
int iloop;
float avg,standardDaviation=0.0,sqroot;
for(iloop=0;iloop<n;iloop++)
{
sum=sum+arr[iloop];
}
avg=(float)sum/n;
for(iloop=0; iloop<n; iloop++)
standardDaviation =standardDaviation+pow(arr[iloop] - avg, 2);
sqroot=((float)standardDaviation/n);
printf("STANDARD DAVIATION:%f ",sqroot);
}
void avg(int arr[],int n) /*calculate average and prints it*/
{
long int sum=0;
float avg;
int iloop;
for(iloop=0;iloop<n;iloop++)
{
sum=sum+arr[iloop];
}
avg=(float)sum/n;
printf("AVERAGE OF ALL DICES ROLLED:%f ",avg);
}
int main()
{
long int n,iloop;
int arr[100000];
printf("How Many times do u want To roll the Dice:");
scanf("%d",&n);
while(n<1||n>100000)
{
printf("Please Enter Proper Value(>1&&<100000):");
scanf("%d",&n);
}
dice(arr,n);
total(arr,n);
calc(arr,n);
avg(arr,n);
stddaviation(arr,n);
return 1;
}//end of main
/*******OUTPUT****************
How Many times do u want To roll the Dice:-1
Please Enter Proper Value(>1&&<100000):200000
Please Enter Proper Value(>1&&<100000):1000
Total Number Of Times Rolls Simulated:1000
OCCURENCES OF:
1:156
2:162
3:180
4:168
5:157
6:177
PERCENTAGE OF:
1:15.600000
2:16.200001
3:18.000000
4:16.799999
5:15.700000
6:17.700001
AVERAGE OF ALL DICES ROLLED:3.539000
STANDARD DAVIATION:2.884477
********************************/
//PLZ LET ME KNOW WAS IT THE SAME U REQUIRED.??
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.