Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help verifying my code for my C programming class. The assignment is to thr

ID: 3853425 • Letter: N

Question

Need help verifying my code for my C programming class. The assignment is to throw a die 10, 1,000 and 10,000 times and compute the average and the standard deviation.

Here is my code:

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

int chance(int a[]){
float sum=0;int i;
for (i=0;i<sizeof(a);i++){ //rolls the dice
a[i]=rand()%6+1;
sum+= a[i];
}
return sum; //returns the sum of dice roll
}
float standardDeviation(int a[], float *avg){
int i; float var, std;

for (i=0;i<sizeof(a);i++){ var+=pow(a[i]-*avg,2);} //equation for variance
std=sqrt((var)/(sizeof(a)-1.0)); //equation for standard deviation
return std;
}

int main()
{
int a=10, b=1000, c=10000, sum, sum1, sum2; float avg, avg1, avg2;
srand(time(NULL));
sum = chance(&a); sum1 = chance(&b); sum2 = chance (&c);
avg = sum/a; avg1 = sum1/b; avg2 = sum2/c;
printf("Average of 10 rolls=%f, and Standard Deviation of 10 rolls=%f. ", avg, standardDeviation(&a, &avg));
printf("Average of 1000 rolls=%f, and Standard Deviation of 1000 rolls=%f. ", avg1, standardDeviation(&b, &avg1));
printf("Average of 10000 rolls=%f, and Standard Deviation of 10000 rolls=%f. ", avg2, standardDeviation(&c, &avg2));
return 0;
}

Thank you for any help

Explanation / Answer

Hi, Below is your code.. I have modified it , by adding arrays and typecasting wherever needed, and improved its performance by removing the unwanted pointer usage. Let me know if you need anything else: -

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

int chance(int a[]){

float sum=0;int i;

for (i=0;i<sizeof(a);i++){ //rolls the dice

a[i]=rand()%6+1;

sum+= a[i];

}

return sum; //returns the sum of dice roll

}

float standardDeviation(int a[], float avg){

int i; float var, std;

for (i=0;i<sizeof(a);i++){ var+=pow(a[i]-avg,2);} //equation for variance

std=sqrt((var)/(sizeof(a)-1.0)); //equation for standard deviation

return std;

}

int main()

{

int a=10, b=1000, c=10000, sum, sum1, sum2;

float avg, avg1, avg2;

int arrA[a],arrB[b],arrC[c];

srand(time(NULL));

sum = chance(arrA); sum1 = chance(arrB); sum2 = chance (arrC);

avg = sum/(float)a; avg1 = sum1/(float)b; avg2 = sum2/(float)c;

printf("Average of 10 rolls=%f, and Standard Deviation of 10 rolls=%f. ", avg, standardDeviation(arrA, avg));

printf("Average of 1000 rolls=%f, and Standard Deviation of 1000 rolls=%f. ", avg1, standardDeviation(arrB, avg1));

printf("Average of 10000 rolls=%f, and Standard Deviation of 10000 rolls=%f. ", avg2, standardDeviation(arrC, avg2));

return 0;

}

Sample Run: -

Average of 10 rolls=2.600000, and Standard Deviation of 10 rolls=1.807919.
Average of 1000 rolls=0.028000, and Standard Deviation of 1000 rolls=3.972014.
Average of 10000 rolls=0.002500, and Standard Deviation of 10000 rolls=3.603075.


--------------------------------
Process exited after 0.0681 seconds with return value 0
Press any key to continue . . .