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

Create a simple command line program that simulates the rolling of a pair of six

ID: 3587855 • Letter: C

Question

Create a simple command line program that simulates the rolling of a pair of six-sided dice a user given number of times. The number of times to roll the pair of dice should be read as input from the argv array on the command line. The result of each roll should be stored in a struct with at least two int fields: dice01 and dice02. As the structs are created they need to be stored in an ordered linked list (ordered by the total). After the last roll, process the list of rolls and count how many times each result occurred. The output should cover the two impossible (for 6 sided dice) results of 1 and 13. After the calculated results are displayed, print out the entire list of rolls showing the roll count, the value of each dice, and finally the total An example of running the program from the command line might be: rolldice2.exe 10e The resulting output should be similar to: Dice Result: Number of occurrences 1: (should always be zero) 3: 23 (omitting results for 4 thru 11 to save space your program should print them!) 12: 8 13: (should always be zero) Actual Dice Rolls: Roll 1: Dice A: 1, Dice B:, Total: 2 Roll 2: Dice A: 1, Dice B: 1, Total: 2 Roll 3: Dice A: 1, Dice B: 2, Total: 3 (omitting results for 4 thru 98 to save space your program should print them!) Rol1 99: Dice A: 5, Dice B: 6, Total: 11 Roll 100: Dice A: 6, Dice B 6, Total: 12

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

/******************************************************

* Structure declaration

******************************************************/

struct node {

int dice1,dice2,total;

struct node *next;

};

/******************************************************

* Function prototypes and macro

*******************************************************/

typedef struct node NODE;

void insertOrderedDice(NODE *,int,int,int);

void showResult(NODE *);

void showOccurances(int []);

#define ARRAY_SIZE 14

static int occurances[ARRAY_SIZE];//static array declarartion

/****************************************************************************

* Name :

main()

* Purpose :

* Simulating the rolling of pair of six-sided dice a user given

number of times.

* Input :

* Number of time to roll the pair of dice.

*

*****************************************************************************/

int main(int argc,char *argv[])

{

int i,numOfRolls,d1,d2,tot;

if (argc != 2 )

{

printf(" Number of command line argument is invalid. ");

return 0;

}

numOfRolls = atoi(argv[1]);

NODE start;

start.next = NULL;

start.dice1 = 0;

start.dice2 = 0;

start.total = 0;

for(i=0;i<numOfRolls;i++) {

d1 = rand()%6 + 1;

d2 = rand()%6 + 1;

tot = d1 + d2;

occurances[tot] += 1;

insertOrderedDice(&start,d1,d2,tot);

}

printf("Dice Results : ");

showOccurances(occurances);

printf("Actual Dice Results : ");

showResult(&start);

return 0;

}

/****************************************************************************

* Name :

* insertOrderedDice()

* Purpose :

* To insert the reading of pair of dice and total of the readings in a ordered link list.

* Input :

* start- pointer to the head of the link list

* data1- reading of first dice

* data2- reading of second dice

* total- sum of readings of both dice

*

*****************************************************************************/

void insertOrderedDice(NODE *start,int data1,int data2,int total)

{

NODE *newnode;

newnode = (NODE *)malloc(sizeof(NODE));

newnode->dice1 = data1;

newnode->dice2 = data2;

newnode->total = data1+data2;

NODE *prev = start;

NODE *curr = start->next;

while (curr !=NULL && total < curr->total)

{

prev = curr;

curr = curr->next;

}

prev->next = newnode;

newnode->next = curr;

}

/****************************************************************************

* Name :

* showOccurances()

* Purpose :

* To show the occurances of sum of readings of both dices i.e occurances of total.

* Input :

* occurances[]-static array which has stored the number of occurances.

*

*****************************************************************************/

void showOccurances(int arr[])

{

int i;

printf(" Number of occurances ");

for(i=1;i<ARRAY_SIZE;i++)

{

printf(" %d : %d ",i,occurances[i]);

}

}

/****************************************************************************

* Name :

* showResult()

* Purpose :

* To show the readings of each dice and their sum.

* Input :

* start- pointer to the haed of the link list.

*

*****************************************************************************/

void showResult(NODE *start) {

int i;

NODE * curr = start->next;

while (curr != NULL)

{

printf(" Roll %d : Dice A %d, Dice B %d, Total %d ",i,curr->dice1,curr->dice2,curr->total);

curr = curr->next;

i++;

}

printf(" ");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote