Need help please!!!

This is a discussion on Need help please!!! within the C Programming forums, part of the General Programming Boards category; Hi i have been introduced to passing values by reference and in this program, my task was to convert it ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    Unhappy Need help please!!!

    Hi i have been introduced to passing values by reference and in this program, my task was to convert it from passing by value to passing by reference. I have run out of options because of the fact that i really don't know how to correct the errors to the program when given

    anyway here is what i have so far for my source code...

    Code:
     #include <stdio.h>
    #include <stdlib.h>                 /*rand(), srand() prototypes*/
    #include <time.h>                   /* time() prototype*/
    void setseed();                        /* set seed for random number */
    void rollDice(int* r);                         /* roll dice  */
    void calculation(int* player1, int* player2, int* result);
    void print_result(int* player1, int* player2, int* result);
    void comparison(int*, int*);/*comparing the two players final result*/
    
    int main(void)
    {
      int result = 0;
      int player1 = 0;
      int player2 = 0;
      int roll1, roll2, roll3, roll4, roll5, roll6;
    
    
      setseed();
      /* rolling 6 dice */
      roll1 = rollDice(int* r);
      roll2 = rollDice(int* r);
      roll3 = rollDice(int* r);
      roll4 = rollDice(int* r);
      roll5 = rollDice(int* r);
      roll6 = rollDice(int* r);
    
         player1 =  roll1+roll2+roll3;
         player2 =  roll4+roll5+roll6;
    
         result = player1 + player2;
    
      /* calculate and print the results */
      *result = calculation(&player1, &player2, &result);
    
      print_result(&player1, &player2, &result);
    
     /*compare the scores of both players*/
      comparison(&player1, &player2);
    
      return 0;
    }/*end of main function*/
    
    /* set seed using initial time from time.h */
    void setseed()
    {
      int seed;               /*time(0) returns the current clendar time, often         */
      seed = time(0);   /* implemented as # of seconds since some specified  */
      srand(seed);       /* date. sets different seed each time, making random */
    }                             /* output appear even more random                              */
    /* rolling the dice  */
    
    void rollDice(int* r)
    {
      *r =  return (rand() % 6 + 1) ;
    }/*end of rollDice*/
    
    /* summing up the total of 2 dices */
    
    void calculation(int* player1, int* player2, int* sum)
    {
        *sum = *player1 + *player2;
    }/*end of calculation*/
    
    /* printing the result */
    
    void print_result(int* player1, int* player2, int* result)
    {
      printf("player1 score = %d  player2 score = %d ", *player1, *player2);
      printf("\nThe sum of both players = %d \n", *result);
    }/*end of print_result*/
    
    /*comparing the total scores of both players*/
    
    void comparison(int* player1, int* player2)
    {
      if(*player1 > *player2)
      {
       printf("1\n");
      }
      else if (*player1 < *player2)
      {
       printf("-1\n");
      }
      else
      {
       printf("0");
      }
    }/*end of comparison*/



    any help would be much appreciated

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by dbz4life View Post
    Hi i have been introduced to passing values by reference
    But you never passed any value by reference, you are using pointers (and that too is wrong at some places)
    Last edited by abh!shek; 06-06-2008 at 02:37 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as this is the C programming board and not the C++ programming board, you CAN NOT pass by reference (at least not in the meaning that the word has in C++). C only have two ways (actually, essentially, only one) of passing parameters: by value, or as a pointer (which is a value, so it's really not different from passing by value).

    Code:
    ...
    roll1 = rollDice(int* r);
    ...
    I presume you want "roll1" to be "passed by reference", so you should pass the address of roll1 to rolldice. As it is currently written, it is syntactically incorrect, as the "int *" is incorrect in this context.

    Maybe you should go back and re-read the chapter about passing pointers in your book?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't return anything from a void function (cough rolldice cough). Changing call-by-value to call-by-pointer is meaningless if you don't intend to change the value of the parameter, so your print and compare functions don't need pointers.

    And you managed to completely avoid stating your question.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21