0% found this document useful (0 votes)
22 views10 pages

Simple Dice Rolling Simulator

Uploaded by

Jkyle Lapig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Simple Dice Rolling Simulator

Uploaded by

Jkyle Lapig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Dice Simulator

Flowchart
start
Declarations
num numDice
num numRolls
num roll
num dice
num diceValue
array diceValues
num i
num sum
num DICE_SIDES = 6
housekeeping()
roll = 1
while roll <= numRolls
displayRollHeader(roll)
dice = 1
while dice <= numDice
diceValue = rollDice()
diceValues[dice] = diceValue
output "Dice ", dice, ": ", diceValue
dice = dice + 1
endwhile
displayStats(diceValues, numDice)
roll = roll + 1
endwhile
finish()
stop
rollDice()
diceValue = random(1, DICE_SIDES)
return diceValue

housekeeping()
output "SIMPLE DICE SIMULATOR"
input "Enter the number of dice to roll: ", numDice
input "Enter the number of rolls: ", numRolls
return

displayRollHeader(roll)
output "--------------------------"
output "Roll #", roll, ":"
return

displayStats(diceValues, count)
sum = 0
i=1
while i <= count
sum = sum + diceValues[i]
i=i+1
endwhile
output "Statistics:"
output "Sum: ", sum
output "Average: ", sum/count
return

finish()
output "END OF SIMULATION"
return
Pseudocode
Declarations
num numDice
num numRolls
num roll
num dice
num diceValue
array diceValues
num i
num sum
num DICE_SIDES = 6

housekeeping()
roll = 1
while roll <= numRolls
displayRollHeader(roll)
dice = 1
while dice <= numDice
diceValue = rollDice()
diceValues[dice] = diceValue
output "Dice ", dice, ": ", diceValue
dice = dice + 1
endwhile
displayStats(diceValues, numDice)
roll = roll + 1
endwhile
finish()
stop

rollDice()
diceValue = random(1, DICE_SIDES)
return diceValue

housekeeping()
output "SIMPLE DICE SIMULATOR"
input "Enter the number of dice to roll: ", numDice
input "Enter the number of rolls: ", numRolls
return

displayRollHeader(roll)
output "--------------------------"
output "Roll #", roll, ":"
return

displayStats(diceValues, count)
sum = 0
i=1
while i <= count
sum = sum + diceValues[i]
i=i+1
endwhile
output "Statistics:"
output "Sum: ", sum
output "Average: ", sum/count
return

finish()
output "END OF SIMULATION"
return
C Program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function prototypes
void housekeeping(int *numDice, int *numRolls);
int rollDice();
void displayRollHeader(int roll);
void displayStats(int diceValues[], int count);
void finish();

int main() {
// Declarations
int numDice;
int numRolls;
int roll;
int dice;
int diceValue;
int *diceValues;
const int DICE_SIDES = 6;

// Seed random number generator


srand(time(NULL));

// Call housekeeping function


housekeeping(&numDice, &numRolls);

roll = 1;
while (roll <= numRolls) {
displayRollHeader(roll);
// Allocate memory for dice values (using 1-based indexing for consistency with
pseudocode)
diceValues = (int*)malloc((numDice + 1) * sizeof(int));

dice = 1;
while (dice <= numDice) {
diceValue = rollDice();
diceValues[dice] = diceValue;
printf("Dice %d: %d\n", dice, diceValue);
dice = dice + 1;
}

displayStats(diceValues, numDice);

// Free allocated memory


free(diceValues);

roll = roll + 1;
}

finish();

return 0;
}

// Function to get user input


void housekeeping(int *numDice, int *numRolls) {
printf("==========================\n");
printf(" SIMPLE DICE SIMULATOR \n");
printf("==========================\n\n");

printf("Enter the number of dice to roll: ");


scanf("%d", numDice);

printf("Enter the number of rolls: ");


scanf("%d", numRolls);

// Validate input
if (*numDice <= 0 || *numRolls <= 0) {
printf("Error: Please enter positive numbers only.\n");
exit(1);
}
}

// Function to simulate rolling a dice


int rollDice() {
const int DICE_SIDES = 6;
return (rand() % DICE_SIDES) + 1;
}

// Function to display header for each roll


void displayRollHeader(int roll) {
printf("\n--------------------------\n");
printf("Roll #%d:\n", roll);
}

// Function to display statistics for dice rolls


void displayStats(int diceValues[], int count) {
int sum = 0;
int i = 1;

while (i <= count) {


sum = sum + diceValues[i];
i = i + 1;
}

printf("\nStatistics:\n");
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", (float)sum / count);
}

// Function to display ending message


void finish() {
printf("\n==========================\n");
printf(" END OF SIMULATION \n");
printf("==========================\n");
}
Explanation
1. Program Structure:
o The program follows a modular design with separate functions for each
logical component
o The main program contains the core logic while delegating specific
tasks to functions
2. Key Components:
o Declarations: All variables are declared at the beginning

o housekeeping(): Handles program introduction and user input

o rollDice(): Generates a random dice value

o displayRollHeader(): Outputs formatting for each roll

o displayStats(): Calculates and displays statistics for a set of dice


values
o finish(): Displays program conclusion message

3. Program Flow:
o Begin with variable declarations and housekeeping

o Use nested while loops to handle multiple rolls of multiple dice

o For each roll, display the roll number, individual dice values, and
statistics
o End with a finishing message

4. Sample Output:
5. ==========================
6. SIMPLE DICE SIMULATOR
7. ==========================
8.
9. Enter the number of dice to roll: 3
[Link] the number of rolls: 2
11.
12.--------------------------
[Link] #1:
[Link] 1: 4
[Link] 2: 6
[Link] 3: 1
17.
[Link]:
[Link]: 11
[Link]: 3.67
21.
22.--------------------------
[Link] #2:
[Link] 1: 3
[Link] 2: 5
[Link] 3: 2
27.
[Link]:
[Link]: 10
[Link]: 3.33
31.
32.==========================
33. END OF SIMULATION
34.==========================
This program successfully incorporates all the required elements of C programming
in a simple dice simulator, with the flowchart and pseudocode formatted according
to the provided example.

You might also like