My program keeps crashing on launching!

Hi everyone!
I'm posting there because I really need help for this program I posted.
I know this might be a long post but if you can understand where the problem is without checking every line of that program I would be very grateful.
This program is supposed to give you some combinations to create three non-transitive dices analyzing every possible combination. If you don't know, non-transitive dices are a group of dices with unusual numbers on each one (numbers are always between 1 and 6 but some numbers should be repeated -so some others are not in a cube's face at all-) chosen to create the non-transitive effect. This effect makes dice one win on dice two, dice two win on dice three but dice three wins on dice one, making some kind of 'circle' of winners with no absolute losers.
The problem is that compilation doesn't mark any mistake, but as soon as I run the program on Dev C++ it crashes. I have checked more than once every single step, but since I'm just a beginner I can't see where the mistake is.
Can you help me? Thanks a lot!
*I wrote comments on my own mothertongue and I deleted them before posting, sorry if I forgot some or anything else*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <math.h>
using namespace std;

const shortred=5,
	blue=11,
	green=17;

unsigned long winner_red[108],
	winner_blue[108],
	winner_green[108];

float rate_red[108],
	rate_blue[108],
	rate_green[108];

short dice[18],
	total_wins=0;
float winner (short end1, short end2)
{
	short start1,
	start2;
	floatwins=0;
	start1=end1-5;
	start2=end2-5;
	for (int k_winner=start1; k_winner<=end1; k_winner++)
		{
		for (short j_winner=start2; j_winner<=end2; j_winner++)
		{
			if (dice[k_winner]>dice[j_winner])
			{
				wins++;
			}
		}
	}
	wins=wins/36*100;
	return wins;
}
unsigned long fill (short end)
{
	unsigned long result=0;
	short start,
	exp;
	start=end-5;
	for (short k_fill=start; k_fill<=end; k_fill++)
	{
		exp=k_fill-start;
		result=result+dice[k_fill]*pow(10,exp);
	}
	return result;
}
int main(int argc, char** argv) 
{
	float p_red,
	p_blue,
	p_green,
	progress=0,
	p_progress;
	cout<<"Welcome! All the possible combinations to create a group of three non-transitive dices are going to be calculated.\n\n";
	for (int k=0; k<=17;k++)
	{
		dice[k]=1;
	}
	do
	{
		p_red= winner(red, green);
		if(p_red>50)
		{
			p_blue= winner(blue, red);
			if(p_blue>50)
			{
				p_green= winner(green, blue);
				if(p_green>50)
				{
					winner_red[total_wins]=fill(red);
					rate_red[total_wins]=p_red;
					winner_blue[total_wins]=fill(blue);
					rate_blue[total_wins]=p_blue;
					winner_green[total_wins]=fill(green);
					rate_green[total_wins]=p_green;
					total_wins++;
				}
			}
		}
		dice[17]++;
		for(int k=17;k>0;k++)
		{
			if(dice[k]==7)
			{
				dice[k-1]++;
			}
		}
		progress++;
		p_progress=progress/216*100;
		cout<<"\r\r";
		cout<<p_progress<<"%";
	}while (dice[1]!=7);
	cout<<"Completed. Combinations are:\n";
	for(int k=0; k<=total_wins;k++)
	{
		cout<<"Red: "<<winner_red[k]<<endl;
		cout<<"Blue: "<<winner_blue[k]<<endl;
		cout<<"Green: "<<winner_green[k]<<endl;
		cout<<"Red > Green of "<<rate_red<<"%"<<endl;
		cout<<"Blue > Red of "<<rate_blue<<"%"<<endl;
		cout<<"Green > Blue of "<<rate_green<<"%"<<endl;
	}
}
Last edited on
There were some typos in line 5 and line 23. (missing spaces)

$ gdb a.out
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
in main (argc=1, argv=0x7fffffffdf08) at foo.cpp:88
88                              if(dice[k]==7)
(gdb) print k
$1 = 1184

Look closely at line 86 for(int k=17;k>0;k++)
Last edited on
Oh, what mistakes, thanks!
Unluckily I've just corrected them but the .exe file crashed again... What other problems are there?
upload your updated code.
¿may you also add comments?
Sure! There it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <math.h>
using namespace std;

const short red=5,
	           blue=11,
                   green=17;

unsigned long winner_red[108],
	                winner_blue[108],
	                winner_green[108];

float rate_red[108],
	rate_blue[108],
	rate_green[108];

short dice[18],
	total_wins=0;
float winner (short end1, short end2) //Checks the probability of dice 1 to win on dice 2
{
	short start1,
	         start2;
	float  wins=0;
	start1=end1-5;
	start2=end2-5;
	for (int k_winner=start1; k_winner<=end1; k_winner++)
		{
		for (short j_winner=start2; j_winner<=end2; j_winner++)
		{
			if (dice[k_winner]>dice[j_winner])
			{
				wins++;
			}
		}
	}
	wins=wins/36*100; //percentage
	return wins;
}
unsigned long fill (short end) //converts the 6 elements of a dice to one single long var
{
	unsigned long result=0;
	short start,
	exp;
	start=end-5;
	for (short k_fill=start; k_fill<=end; k_fill++)
	{
		exp=k_fill-start;
		result=result+dice[k_fill]*pow(10,exp); //treated like the polynomial from
	}
	return result;
}
int main(int argc, char** argv) 
{
	float p_red,
	p_blue,
	p_green,
	progress=0,
	p_progress;
	cout<<"Welcome! All the possible combinations to create a group of three non-transitive dices are going to be calculated.\n\n";
	for (int k=0; k<=17;k++) //Minimum value for all dices
	{
		dice[k]=1;
	}
	do
	{
		p_red= winner(red, green); //probability of red winning on green
		if(p_red>50)
		{
			p_blue= winner(blue, red); //prob. of blue winning on red
			if(p_blue>50)
			{
				p_green= winner(green, blue); //prob. of green winning on blue
 				if(p_green>50)
				{
					winner_red[total_wins]=fill(red);//combination saved & converted
					rate_red[total_wins]=p_red;      //percentage saved
					winner_blue[total_wins]=fill(blue);
					rate_blue[total_wins]=p_blue;
					winner_green[total_wins]=fill(green);
					rate_green[total_wins]=p_green;
					total_wins++;
				}
			}
		}
		dice[17]++;
		for(int k=17;k>0;k--) //checks that none of the values gets to 7
		{                
			if(dice[k]==7)
			{
				dice[k-1]++;
			}
		}
		progress++;
		p_progress=progress/216*100;//var to keep the progress of the whole program
		cout<<"\r\r";
		cout<<p_progress<<"%";
	}while (dice[1]!=7);
	cout<<"Completed. Combinations are:\n";
	for(int k=0; k<=total_wins;k++) //output
	{
		cout<<"Red: "<<winner_red[k]<<endl;
		cout<<"Blue: "<<winner_blue[k]<<endl;
		cout<<"Green: "<<winner_green[k]<<endl;
		cout<<"Red > Green of "<<rate_red[k]<<"%"<<endl;
		cout<<"Blue > Red of "<<rate_blue[k]<<"%"<<endl;
		cout<<"Green > Blue of "<<rate_green[k]<<"%"<<endl;
	}
}
Last edited on
I could not reproduce your crash, in my case it enters an infinite loop
85
86
87
88
89
90
91
92
		dice[17]++;
		for(int k=17;k>0;k--) //checks that none of the values gets to 7
		{                
			if(dice[k]==7)
			{
				dice[k-1]++;
			}
		}
You increase the last dice, when it reaches 7 you increase the one in position 16.
Analyze what would happen from there.
You've got your array filled with {1,1,1,1,..., 2, 7}
Then you increase the last one so {1,1,1,1,..., 2, 8}
The condition in line 88 would never be true, and you keep increasing the last dice.

If your intention was to reset the last dice to 1, in order to reach all the possible variations, that would take a long time (6**18 > 1e14)

Given that you do not care about the order of the faces in each dice, or the order of the dices, you may optimize the number of sequences to test.
Still it is a big number.(sorry, cannot count it)
What I wanted to do in that cycle was to reset the the last number to one and add one to the previous one, as it happens passing from nine to ten, but I didn't add the statement.
This is how I wanted it to be:
1
2
3
4
5
6
7
8
9
		dice[17]++;
		for(int k=17;k>0;k--) //checks that none of the values gets to 7
		{                
			if(dice[k]==7)
			{
				dice[k-1]++;
                                dice[k]=1;
			}
		}

About the crash I may try to copy the code to a Mac using xCode just to check if the problem is my compiler on the PC, shouldn't i?
that's going to take a lot of time.


about the crash, run through a debugger, it would indicate the line in which it crashes.
one candidate may be out of bounds in the 'winner' 'rate' arrays.
Topic archived. No new replies allowed.