Thread: Linked List Die Roll

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    Linked List Die Roll

    The assignment is to create a comman line program that accepts the user input to roll two die. Then the program should take the amount roll randomly and display the sum of how many times each number was rolled (i.e. 1 = 0 2 =1... 12 =2) and then the value of each roll (i.I e. 1= 8 2 = 12 etc...).

    I am a little stuck as my program stands it should randomnly roll the dice and then go into a linked list. Then print the values that were rolled as coordinate points.


    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    struct roll {
           int x;
           int y;
           struct roll * next;
           };
           
    struct roll * roll_die(char[], int, int);
    void print_list (struct roll *);
    int main(int argc, char * argv []) {
        
        int x, y;
        
        srand( time (NULL) );
        
        /* If no values are included in parameters, remind user to input */
        if ( argc == 1) {
             printf ("Please enter an amount to roll the die");
        }
        struct roll * roll_die (argv, x, y); /*error says this is treated as compound expression and it is a invalid conversion from int to roll*/
        print_list (roll_die);
                        
                    
        getchar();
        return 0;
    }
    void print_list (struct roll * list){
         while( list != NULL) {
                printf(" The die rolls: %d, %d\n", list ->x, list ->y);
                list = list -> next;
                }
    }
    struct roll * roll_die(char * argv[], int x, int y){
          struct roll * rand_roll = (struct roll *)malloc( sizeof(struct roll) );
          
          int i = 0;
          int z;
          
           for (i = 0; i < z; i++){
               z = atoi(argv[1]);
               x = (rand () % 6) + 1;
               y = (rand () % 6) + 1;
               
               rand_roll -> x = x;
               rand_roll -> y = y;
                
               }
               
               return rand_roll;
    }
    I had it compiling with out errors at a point, but it didn't have an output.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Where you signalled the error (line 22) you do not have a function call; you have a mix of function prototype (like in line 10) and function call.

    Function prototypes specify the return type of the function, the function name, and parameter types.
    Function calls specifiy the function name and parameter values (the compiler knows the type from a previous prototype).

    Also the variables you use as parameters have not been initializaed or were ever assigned a value. You cannot use them safely.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by carpeltunnel View Post
    The assignment is to create a comman line program that accepts the user input to roll two die. Then the program should take the amount roll randomly and display the sum of how many times each number was rolled (i.e. 1 = 0 2 =1... 12 =2) and then the value of each roll (i.I e. 1= 8 2 = 12 etc...).
    That's easily done by starting with a zeroed array and for each roll, using the total as an index to increment that value in the array

    Quote Originally Posted by carpeltunnel View Post
    I am a little stuck as my program stands it should randomnly roll the dice and then go into a linked list.
    Is this an assignment requirement or your own? IMO, a linked list is major overkill for this task.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    The linked list is within the guidelines of the assignment.

    I switched 22 and the errors are found at the function calls:

    22 cannot convert `roll' to `roll*' in assignment
    23 cannot convert `roll' to `roll*' in assignment

    Code:
    int main(int argc, char * argv []) {
        
        int x = 0;
        int y = 0;
        
        srand( time (NULL) );
        
        struct roll * list = NULL;
        
        /* If no values are included in parameters, remind user to input */
        if ( argc == 1) {
             printf ("Please enter an amount to roll the die");
        }
        list = * roll_die (x, y, argv);
        print_list (roll_die);
                        
                    
        getchar();
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    In line 14 of your previous post you dereference the return value from roll_die().

    roll_die(x, y, arg) is a pointer; *roll_die(x, y, arg) is a struct roll.
    The variable list is a pointer.
    You cannot assign a struct roll to a pointer.

    Either make the right-hand side a pointer
    Code:
        list = * roll_die (x, y, argv);
    or make the left-hand side an object of type struct roll
    Code:
    /* line 8*/
    struct roll list = {0};
    --------

    As for line 15, you call a function that, according to the prototype, expects a value of type pointer to struct roll; however you are passing a value of type pointer to function (the name of a function by itself is the address of the function).

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Thank you, dereferncing gives me trouble most of the time but now I'm error free.

    The output of the program display one die roll (x,y), I think my problem is between 43-47 in my initial post, but most things I have tried have led to crashed programs. Am I forgetting something that allows to past more than one value to the struct?

    And to WALTP, storing the values of the die rolls in a linked list is part of the program requirements, then the program just has to process the linked list to display the results.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The roll die function is not correctly written because it does not insert new struct roll nodes into a list.

    You should search the web or the forum for in-depth details on how to make it happen, but the short list is:

    1) you need a pointer to the list
    2) you need the newest roll

    Steps -
    1) with a loop, use the next pointer to find the node before the newest roll's place in the list. This node is the prev node
    2) change the next pointer of the newest roll to point to prev->next
    3) change the prev->next pointer to point to the newest node
    4) return the new list

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Thanks, white flag. I've got most my program down now.

    My Actual Result function works fine and its formatting is very similar to my Number of Occurrences function, but I am not getting valid results for my amounts.

    Code:
    void total_times (struct roll * list, int j){
         struct roll * number_total = (struct roll *)malloc( sizeof( struct roll ) );
         int i = 1;
         int z = 0;
         int num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_10, num_11, num_12;
         for (i = 1; i <= j; i++){/*j is the passed value for agrv[1], actual result uses same for loop and it works fine*/
             z = list ->x + list ->y;
             list = list -> next;
                  if (z ==1){
                        num_1++;
                        }
                  else if (z ==2){
                        num_2++;
                        }
                  else if (z ==3){
                        num_3++;
                        }
                  else if (z ==4){
                        num_4++;
                        }
                  else if (z ==5){
                        num_5++;
                        }
                  else if (z ==6){
                        num_6++;
                        }
                  else if (z ==7){
                        num_7++;
                        }
                  else if (z ==8){
                        num_8++;
                        }
                  else if (z ==9){
                        num_9++;
                        }
                  else if (z ==10){
                        num_10++;
                        }
                  else if (z ==11){
                        num_11++;
                        }
                  else if (z ==12){
                        num_12++;
                        }
                  }  
                  printf("\nNumber of Occurrences:\n\n1 - %d\n2 - %d\n3 - %d\n4 - %d\n5 - %d\n6 - %d\n7 - %d\n8 - %d\n9 - %d\n10 - %d\n11 - %d\n12 - %d",num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_10, num_11, num_12);
                  
             }
    Any pointers would be appreciated

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well...
    Code:
    int num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_10, num_11, num_12;
    None of these are initialized, so adding 1 to these will be unpredictable. Starting everything off at 0 is required.

  10. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    And wouldn't you be better off replacing them anyway with, say,

    int num[13]; <-- arrays start a zero, and I'm trying to retrofit this to your own code, so please excuse this horrible bodge that leaves z[0] unused.

    and using:

    num[z]++; <-- increment the zth box by one.

    instead of that horrendous if-mess and then printing them from the array using similar syntax (num[z]).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Roll on 18 wheels roll on
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-13-2009, 09:12 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM