If statements not working

Hello. I wrote a code that will put either a 1 or -1 into a "Box". I am having trouble getting 1's to pop up. I switched the probabilities around so that they should be somewhat probable however not one has come up. Also with the same code if I change the grid to a 100x100 or even 20x20 instead of 10x10 it'll just keep running. Any help would be appreciated.

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
 int main(){

double r, probred,e,z,c,q;
int red,blue, box[10][10];
red= 1;
blue= -1;
probred = 1;




		for (int a=0; a<10; a++){
			for (int b=0; b<10; b++){
			
		
			box[a][b] = c;
	
		
			r = ((double) rand()/(RAND_MAX+1));
			e =  ((double) rand()/(RAND_MAX+1));
			q = exp(-e);
				if (r < probred ) {
					if ( r < q ){
						z=red;
					}//closes if statement
				}//closes if statement
				if ( r < q){
					z=blue;
				}//closes if statement
					
					
			
			
		cout << a << "," << b  << "   ,   " << r << " , " << z << "    ,     " << e << "  ,  " << q << endl;

	}//closes for loop
	}//closes for loop
	
	}//closes main

You have a lot of things wrong with your program.

You need to get your alignment of columns and line spacing better so the program is easier to read. That is just good manners if you are going to ask for help.

There are so many reasons that your if is not working, but the biggest reason is that uninitialized variable. See if you can fix some of these problems and then post back your code.

You need to use descriptive variable names and explain better what it is you are trying to accomplish.

You never initialize the value of variable c before you assign that value to the array location box[a][b].

I don't see why you are declaring an array, other than setting all the array elements equal to the uninitialized variable c, you never use the array. You were probably supposed to assign the array element based upon the result of your rand and if arrangement.

Perhaps you should post the assignment so someone can explain to you what you are supposed to do because this doesn't make sense.
Last edited on
Here, I helped you out and straightened up the formatting.

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
#include <iostream>
#include <cmath>
#include <stdlib.h>     /* srand, rand */

using namespace std;

int main()
{

	double r, probred,e,z,c,q;
	int red,blue, box[10][10];
	red= 1;
	blue= -1;
	probred = 1;

	for (int a=0; a<10; a++)
	{
		for (int b=0; b<10; b++)
		{
		
		box[a][b] = c;

		r = ((double) rand()/(RAND_MAX));
			if (r < 0.5 ) 
			{
				z=red;
			}//closes if statement
			if ( r > 0.5 )
			{
				z=blue;
			}//closes if statement

		cout << a << "," << b  << "   ,   " << r << " , " << z 
			<< "    ,     " << e << "  ,  " << q << endl;

		}//closes for loop
		
	}//closes for loop
	
}//closes main 
Last edited on
Line 16: What do you think you're storing in box[a][b]?
Hint: c is uninitialized (garbage).
Topic archived. No new replies allowed.