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

Instructions: Create a simple command line program that simulates the rolling of

ID: 3550959 • Letter: I

Question

Instructions: 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 100

Explanation / Answer

// I hope the code is mostly self understandable, if u have any queries ask me in the comments or gauravnarra@gmail.com


#include <stdio.h>
#include <stdlib.h>

struct node{
    int dice1,dice2;
    struct node *next;
};
typedef struct node node;

int order_list(node *head,node *ptr){
    node *itr = head->next,*prev=head;
    int temp1,temp2;
    int sum;
    while(itr != NULL){
        sum = itr->dice1 + itr->dice2;
        if(sum <= (ptr->dice1 + ptr->dice2)){
            ptr->next = itr;
            prev->next = ptr;
            return 0;
        }
        prev = itr;
        itr = itr->next;
    }
    // if insertion at last
    ptr->next = NULL;
    prev->next = ptr;
}
void print_roll_count(node *head){
    node *ptr=head->next;
    int count, prev_count, prev;
    printf("===== Roll Count ===== ");
    if(ptr != NULL){
        count = ptr->dice1 + ptr->dice2;
        prev = count;
        prev_count++;
        ptr = ptr->next;
    }
    while(ptr != NULL){
        count = ptr->dice1 + ptr->dice2;
        if(count != prev){
            printf("count of %d == %d ", prev,prev_count);
            prev_count = 1;
        }
        else
            prev_count++;
        prev = count;
        ptr = ptr->next;
    }
    printf("count of prev == %d ", prev_count);
    printf("All other valid counts are Zero ");
}

void print_list(node *head){
    node *ptr=head->next;
    int count=1;
    printf("==== Roll details ==== ");
    while(ptr != NULL){
        printf("For Roll %d .. dice1 = %d dice2 = %d sum = %d ",count,ptr->dice1,ptr->dice2,ptr->dice1+ptr->dice2);
        count++;
        ptr = ptr->next;
    }
}
int main ( int argc, char *argv[] ){
    /* argc should be 2 for correct execution */
    if ( argc != 2 ){
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s <count>", argv[0] );
        return 0;
    }
    node *ptr, *head;
    head = (node *)malloc(sizeof(node));
    head->next = NULL;
    int i;
    int random;
    for (i=0;i<atoi(argv[1]);i++){
        ptr = (node *)malloc(sizeof(node));
        ptr->dice1    = (rand()%6 + 1);
        ptr->dice2    = (rand()%6 + 1);
        order_list(head,ptr);
    }   
    print_roll_count(head);
    print_list(head);

}