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