# How To Calculate The Sum Of 2 Sets Created By Two Group Datas From A Random Number Generator?

## 12 August 2020 - 1 answer

I am trying to make a programming assignment in my school that requires me to create a dice game in C where the winner is determined based on which sum of 10 dice rolls (between the computer and the player) is higher. However I seem to have encountered a problem where my program would add the sum of the computer dice rolls to that of the user causing a guaranteed win to the user. The program must make use of the srand and rand function and I currently have no idea how to fix this due to this being my entry into programming. Below is the code I have made as I do not how to properly explain this and I have no clue the error could be. Thank you very much for any possible help as I cannot contact my professor at the moment

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//Computer Dice Roll
Comp_Dice(int CP_Value, int CP_Sum){
srand(time(0));
for(int i = 0; i <10; i++){
CP_Value = rand()%6+1;
printf("%d ", CP_Value);
CP_Sum += CP_Value;

}
printf("\nPlayer 1 Total: %d", CP_Sum);
}
//End of Computer Dice Roll

//Player Data
Plyr_Dice(int Plyr_Value, int Plyr_Sum, int x){
srand(x);
for(int e = 0; e <10; e++){
Plyr_Value = rand()%6+1;
printf("%d ", Plyr_Value);
Plyr_Sum +=Plyr_Value;

}
printf("\nPlayer 2 Total: %d", Plyr_Sum);
}
//End of Player Data

int main(){
int x, CP_Value, CP_Sum, Plyr_Value, Plyr_Sum, sel;

system("cls");

do{
system("cls");
printf("\n\t\tDice Roll Game");
printf("\n\t1.Play");
printf("\n\t2.Exit");
printf("\nSelect>>: ");
fflush(stdin);
scanf("%d" ,&sel);
printf("\n\n\n");

switch(sel){
case 1: printf("Computer dice generated: ");
Comp_Dice(CP_Value, CP_Sum);

printf("\nPlayer dice has been generated please Input integer seed: ");
scanf("%d", &x);

Plyr_Dice(Plyr_Value, Plyr_Sum, x);
if (Plyr_Sum>CP_Sum){
printf("\nPlayer 2 Wins!\n");
}else printf("\nPlayer 1 Wins!\n");
break;
case 2: printf("Thank you for playing");
break;
default: printf("invalid input please choose 1 or 2\n");
}
system("pause");
}while(sel!=2);
return 0;
}
``````

If you address the things the compiler hints at when warnings are turned on, your code will be closer to running correctly.

Here is the list my compiler shows:

``````Build Status (prog.prj - Debug)
main29.c - 6 warnings
16, 1    warning: function does not return a value
29, 1    warning: function does not return a value
49, 27    warning: variable 'CP_Value' may be uninitialized when used here
33, 20    note: initialize the variable 'CP_Value' to silence this warning
49, 37    warning: variable 'CP_Sum' may be uninitialized when used here
33, 28    note: initialize the variable 'CP_Sum' to silence this warning
54, 27    warning: variable 'Plyr_Value' may be uninitialized when used here
33, 40    note: initialize the variable 'Plyr_Value' to silence this warning
54, 39    warning: variable 'Plyr_Sum' may be uninitialized when used here
33, 50    note: initialize the variable 'Plyr_Sum' to silence this warning
"c:\play\prog.exe" successfully created.
Build succeeded.
``````

Some other suggestions:

As per C standard, it is undefined behavior to use fflush(stdin).

Place `srand()` in the beginning of `main(){`, and call only once during execution. More on proper use of `srand()` here` here

To change the value of a variable passed as an argument in any function, it is not the variable itself that is passed, it is the address of that variable.

Change the prototype of `Comp_Dice` to:

``````Comp_Dice(int CP_Value, int *CP_Sum){
``````

Then call it as shown:

``````Comp_Dice(CP_Value, &CP_Sum);//& is the `address of` operator
``````

Change prototype of `Plyr_Dice` to:

``````void Plyr_Dice(int Plyr_Value, int Plyr_Sum, int x);
^^^^
``````

Same with `Plyr_Dice`

In your first call to `Comp_Dice`, `CP_Value` is not initialized. True for several other variables as well. `Plyr_Value`, `Plyr_Sum`,... Initialize all of them, even if you believe they will be assigned values in first use. Its is just good practice to initialize.

Following is your code modified to run. I have commented out a few things that do not work on my system, and things that I have identified are wrong. It may need some debugging, but it should run.

``````//Computer Dice Roll
void Comp_Dice(int CP_Value, int *CP_Sum){
srand(time(0));
for(int i = 0; i <10; i++){
CP_Value = rand()%6+1;
printf("%d ", CP_Value);
*CP_Sum += CP_Value;

}
printf("\nPlayer 1 Total: %d", *CP_Sum);
}
//End of Computer Dice Roll

//Player Data
void Plyr_Dice(int Plyr_Value, int *Plyr_Sum, int x){
srand(x);
for(int e = 0; e <10; e++){
Plyr_Value = rand()%6+1;
printf("%d ", Plyr_Value);
*Plyr_Sum +=Plyr_Value;

}
printf("\nPlayer 2 Total: %d", *Plyr_Sum);
}
//End of Player Data

int main(void){
int x, CP_Value=0, CP_Sum, Plyr_Value=0, Plyr_Sum, sel;

//system("cls");//okay, but does not work on my system.

do{
//system("cls");
printf("\n\t\tDice Roll Game");
printf("\n\t1.Play");
printf("\n\t2.Exit");
printf("\nSelect>>: ");
//fflush(stdin);// undefined behavior
scanf("%d" ,&sel);
printf("\n\n\n");

switch(sel){
case 1: printf("Computer dice generated: ");
Comp_Dice(CP_Value, &CP_Sum);

printf("\nPlayer dice has been generated please Input integer seed: ");
scanf("%d", &x);

Plyr_Dice(Plyr_Value, &Plyr_Sum, x);
if (Plyr_Sum>CP_Sum){
printf("\nPlayer 2 Wins!\n");
}else printf("\nPlayer 1 Wins!\n");
break;
case 2: printf("Thank you for playing");
break;
default: printf("invalid input please choose 1 or 2\n");
}

//system("pause");//okay, but does not work on my system.
}while(sel!=2);
return 0;
}
``````