Using Microsoft Visual Studios write a program in C with three functions. The ma
ID: 3687211 • Letter: U
Question
Using Microsoft Visual Studios write a program in C with three functions. The main function will just define and initialize the variables and call the user functions. All the work will be done by the user functions. In the main function, define an unsigned integer array of 7 elements – 1 to 6 are the faces of the die – element zero is the number of times to throw the die. Prompt the user for how many times to throw the die. Read the unsigned int and store in element zero. Call function #1. Call function #2.
Function #1: This function will be passed the integer array and its size and return nothing. Using the value stored in element zero, loop through the die rolls. Increment the array element number equal to the die face value – i.e. array[1] = count of ones rolled, array[2] = count of twos rolled, etc.
Function #2: This function will be passed the unsigned integer array and its size and return nothing. Using a Sentinel loop prompt the user to enter two numbers. The first is the die face value – error check to make sure it is 1 to 6 – if not error message. If a valid die face value, then prompt for and read the second number. The second number is a command number. If a negative number exit the Sentinel loop. If zero do nothing – i.e. go to top of Sentinel loop. If one print the number of times the die face number (first number entered) entered occurred – i.e. print the value in the element number entered. If two call function #3 and print the returned value.
Function #3: This function will be passed the unsigned integer array, its size, the die face value asked for ( first number entered in Function #2 ) and return a double – the returned double is the percentage that face value appeared – i.e. (die count) / (element zero value).
Explanation / Answer
C program with implementation of function one is given :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void function1(unsigned int*, int);
void main()
{
unsigned int array[7];
int i = 0;
printf("Enter no's of time you want to throw the die, value should be between 1-6 ");
scanf("%u", array[0]);
function1(array, 7);
}
void function1(unsigned int* arr, int size)
{
int i = 0;
srand (time(NULL));
for(i; i<arr[0]; i++)
{
arr[i+1] = rand() %6 + 1;
}
}