Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

C Programming Coursework 1

TOPIC: Dice rolling program


Name: Usaamah Shakeel
MISIS No: M00933575
Module Name: Computer Systems Architecture and
Operating Systems
Module No: CST1500
Program code:
Output:

Explanation of the written code:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
These are preprocessor directives. #include is used to include header files in the
program. <stdio.h> provides functions for input and output, <stdlib.h> provides
functions for memory allocation and random number generation, and <time.h>
provides functions related to time.
void generate_throws (int n, int throws[n]);
void print_occurrences (int n, int throws[n]);
These are function prototypes. They declare the functions generate_throws and
print_occurrences before they are defined. This allows the compiler to recognize the
functions when they are called in main ().

int main () {
int n, throws [6];
This is the main function where the program execution starts. It declares two variables
n and throws. n will store the number of faces on the dice, and throws is an array to
store the generated throws

printf (" Enter Number of faces between 1 and 25: ");


scanf ("%d", &n);
while (faces[0] < 1 || faces [0] > 25);
This prompts the user to enter the number of faces on the dice and reads the input from
the user, storing it in the variable n. It will also range check that the inputted number is
between 1 and 25.

printf(" Enter Number of throws between 1 and 500: ");


scanf("%d", &throws[0]);
while (throws[0] < 1 || throws[0] > 500);
This prompts the user to enter the number of throws and reads the input, storing it in
the first element of the throws array (throws[0]). It will also range check that the
inputted number is between 1 and 500.

srand(time(0));
This seeds the random number generator. The srand function is given the current time
(in seconds since the epoch) as a seed, ensuring a different sequence of random numbers
on each program run.

printf(" Generating throws: \n");


generate_throws(n, throws);
This prints a message indicating that throws are being generated and calls the
generate_throws function to generate random throws and store them in the throws
array.
print_occurrences(throws[0], throws);
This calls the print_occurrences function to print the occurrences of each face in the
generated throws.

return 0;
}
void generate_throws(int n, int throws[n]) {
for (int i = 1; i <= throws[0]; i++) {
This statement indicates the successful execution of the program. The value 0 is conventionally
returned to the operating system to indicate that the program terminated without errors.
This is the beginning of the generate_throws function. It takes the number of faces (n)
and the array to store throws (throws). The function uses a loop to generate random
throws and store them in the throws array.

int face = rand() % n + 1;

This generates a random face value between 1 and n using the rand function.

printf(" %d\n", face);


throws[i] = face;
}
}
This prints the generated face value and stores it in the throws array at the
corresponding index

void print_occurrences(int n, int throws[n]) {


int occurrence[6] = {0};
This is the beginning of the print_occurrences function. It takes the number of throws
(n) and the array containing throws (throws). It initializes an array occurrence to store
the occurrences of each face

for (int i = 1; i <= n; i++) {


occurrence[throws[i]]++;
}
This loop iterates through the throws array and increments the occurrences of each face in the
occurrence array.

for (int i = 1; i <= n; i++) {


printf("Occurrences of %d: %.2f%%\n", i, (float)occurrence[i] / n * 100);
}
}
This loop prints the occurrences of each face as a percentage of the total throws. It
calculates the percentage by dividing the number of occurrences by the total number of
throws and multiplying by 100. The output is formatted with two decimal places

You might also like