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: 3587998 • 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

//program

#include<iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

struct dice

{

int value1;

int value2;

};

typedef struct dice Dice;

struct list

{

int total;

struct list *next;

};

void add_list(struct list **list,int tot);

int count_total(struct list *list,int n);

int main(int argc , char **argv)

{

//create pointer to dice Data array to hold dice values

struct dice *diceData;

srand(time(0));

struct list *head = NULL;

if (argc < 2)

{

cout << "Please enter number of times to roll ";

return -1;

}

int no_times = atoi(argv[1]);

//int no_times =10;

//create array of struct to hold dice data

diceData = new struct dice[no_times];

for (int i = 0; i < no_times; i++)

{

diceData[i].value1 = (rand() % 6 + 1);

diceData[i].value2 = (rand() % 6 + 1);

//add total of two dice values to lined list in order of their sum

add_list(&head, diceData[i].value1 + diceData[i].value2);

}

//display results

int j = 1;

cout << "Dice results: Number of occurences of 1: 0" << endl;

int totalOccarances[12] = { 0 };

totalOccarances[0] = 0;

for (int i = 1; i <=12; i++)

{

if ( i > 1)

totalOccarances[j] = count_total(head, i);

cout << "Dice results: Number of occurences of " << i << ": "<<totalOccarances[j]<< endl;

j++;

}

cout << "Dice results: Number of occurences of 13: 0" << endl;

for (int i = 0; i < no_times; i++)

{

cout << "Roll " << i << ": Dice A:" << diceData[i].value1 << " 1, Dice B: " << diceData[i].value2 << " , Total: " << diceData[i].value1 + diceData[i].value2 << endl;

}

}

int count_total(struct list *ptr,int n)

{

struct list *cur = ptr;

int count = 0;

while (cur != NULL)

{

if (cur->total == n)

count++;

cur = cur->next;

}

return count;

}

void add_list(struct list **list,int tot)

{

struct list *newList,*cur=*list;

int tmp;

newList = new struct list;

newList->total = tot;

newList->next = NULL;

if (*list == NULL)

{

*list = newList;

*list = newList;

}

else

{

while (cur->next != NULL && cur->next->total < newList->total)

{

cur = cur->next;

}

newList->next = cur->next;

cur->next = newList;

}

}

-----------------------------------------------------------------------------------------------------------------------------

//output

Dice results: Number of occurences of 1: 0
Dice results: Number of occurences of 1: 0
Dice results: Number of occurences of 2: 0
Dice results: Number of occurences of 3: 1
Dice results: Number of occurences of 4: 1
Dice results: Number of occurences of 5: 1
Dice results: Number of occurences of 6: 0
Dice results: Number of occurences of 7: 1
Dice results: Number of occurences of 8: 2
Dice results: Number of occurences of 9: 2
Dice results: Number of occurences of 10: 1
Dice results: Number of occurences of 11: 1
Dice results: Number of occurences of 12: 0
Dice results: Number of occurences of 13: 0
Roll 0: Dice A:5 1, Dice B: 3 , Total: 8
Roll 1: Dice A:6 1, Dice B: 1 , Total: 7
Roll 2: Dice A:6 1, Dice B: 4 , Total: 10
Roll 3: Dice A:6 1, Dice B: 3 , Total: 9
Roll 4: Dice A:1 1, Dice B: 2 , Total: 3
Roll 5: Dice A:5 1, Dice B: 4 , Total: 9
Roll 6: Dice A:1 1, Dice B: 3 , Total: 4
Roll 7: Dice A:3 1, Dice B: 2 , Total: 5
Roll 8: Dice A:6 1, Dice B: 5 , Total: 11
Roll 9: Dice A:6 1, Dice B: 2 , Total: 8

-------------------------------

//one mor output

Dice results: Number of occurences of 1: 0
Dice results: Number of occurences of 1: 0
Dice results: Number of occurences of 2: 1
Dice results: Number of occurences of 3: 1
Dice results: Number of occurences of 4: 1
Dice results: Number of occurences of 5: 1
Dice results: Number of occurences of 6: 4
Dice results: Number of occurences of 7: 1
Dice results: Number of occurences of 8: 4
Dice results: Number of occurences of 9: 3
Dice results: Number of occurences of 10: 0
Dice results: Number of occurences of 11: 2
Dice results: Number of occurences of 12: 2
Dice results: Number of occurences of 13: 0
Roll 0: Dice A:5 1, Dice B: 1 , Total: 6
Roll 1: Dice A:3 1, Dice B: 1 , Total: 4
Roll 2: Dice A:6 1, Dice B: 5 , Total: 11
Roll 3: Dice A:6 1, Dice B: 1 , Total: 7
Roll 4: Dice A:4 1, Dice B: 4 , Total: 8
Roll 5: Dice A:6 1, Dice B: 2 , Total: 8
Roll 6: Dice A:5 1, Dice B: 4 , Total: 9
Roll 7: Dice A:6 1, Dice B: 6 , Total: 12
Roll 8: Dice A:2 1, Dice B: 3 , Total: 5
Roll 9: Dice A:4 1, Dice B: 4 , Total: 8
Roll 10: Dice A:3 1, Dice B: 3 , Total: 6
Roll 11: Dice A:6 1, Dice B: 5 , Total: 11
Roll 12: Dice A:2 1, Dice B: 1 , Total: 3
Roll 13: Dice A:1 1, Dice B: 5 , Total: 6
Roll 14: Dice A:3 1, Dice B: 6 , Total: 9
Roll 15: Dice A:4 1, Dice B: 5 , Total: 9
Roll 16: Dice A:6 1, Dice B: 6 , Total: 12
Roll 17: Dice A:1 1, Dice B: 5 , Total: 6
Roll 18: Dice A:6 1, Dice B: 2 , Total: 8
Roll 19: Dice A:1 1, Dice B: 1 , Total: 2

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