Good Afternoon,

I am working on a project in which I would need to create a matrix of nxm dimensions, in which the top, bottom, left and right edges should have the value 1 and the interior of the matrix should be a random between 0 and 1 with a give probability. I`ve tried to write the following code but when I try to create the interior it gives an error.

Here is the code:

void create_edges(){
	matrix_save = new int*[dim1_save];//allocate the space for an array of pointers
	for(int i=0; i<dim1_save; i++){//for each of those pointers, allocate the
		matrix_save[i] = new int[dim2_save];//amount of space we need
	}

	 
	//matrix_save=(bool)matrix_save;
	for(int i=0;i<dim1_save;i++){
		for (int j=0;j<dim2_save;j++){
			if ((j==0) && (i<dim1_save))

				matrix_save[i][j]=1;
			else if ((i==0) && (j<dim2_save))
				matrix_save[i][j]=1;
			else if ((i==dim1_save-1)&&(j<dim2_save))
				matrix_save[i][j]=1;
			else if ((i<dim1_save-1)&&(j==dim2_save-1))
				matrix_save[i][j]=1;
			else matrix_save[i][j]=0;
				}
	}

}
void init_matrix(){
	int aux;
	int count=0;

	srand((unsigned)time(NULL));
	int lowest=0, highest=100; 
	int range=(highest-lowest)+1;
     
	aux=lowest+int(range*rand()/(RAND_MAX + 1.0));

	for(int i=1;i<=dim1_save;i++){
		for (int j=1;j<=dim2_save;j++){
			if(aux<=100-complexity)
			  matrix_save[i][j]= 0;

			else {
			 matrix_save[i][j]= 1;
			count=count+1;
		}

		}
	}
	cout<<count;
	system("PAUSE");
}

Please Help me

what error? What exactly is the problem you are having?

When I create the matrix and assign to the elements from the edge the value of 1, in the interior of the matrix are stored the value of 0, but after that when I use the init_matrix() function to assign the random values to the interior it gives an error at line 38. from the above code. The error is :

Unhandled exception at 0x010be3f5 in name.exe: 0xC0000005: Access violation writing location 0xfdfdfe01.

Your for loop condition is wrong:

for(int i=1;i<=dim1_save;i++){
for (int j=1;j<=dim2_save;j++){

remove that '='

Thank you, that solved the error, but now I am facing another problem. So first I call the create_edge() function and afterwards the init_matrix() function. The problem is that it creates the edges but it does not do anything about the interior, it does not enter the else statement from line 40-43. I don`t understand why the statement is never satisfied.

After I rebuilt the program, the else statement is always executed, while the if statement from line 37 is never executed. I do not understand why is this happening. Please help me!

Multiple things: srand((unsigned)time(NULL)); should be at the beginning of the program, in main()

Desk-check your program to see what value (aux<=100-complexity) is going to be at various times through the loops.

int lowest=0, highest=100;
int range=(highest-lowest)+1;
aux=lowest+int(range*rand()/(RAND_MAX + 1.0));

Why this convoluted equation when all you want is a 1 or 0 in each cell?

for(int i=1;i<=dim1_save;i++){
  for (int j=1;j<=dim2_save;j++){
    if(aux<=100-complexity)
      matrix_save[i][j]= 0;
    else {
      matrix_save[i][j]= 1;
      count=count+1;
    }
 }

Where do aux and complexity change to make the IF do something different while looping?

Thank you for your answer, I`ve managed to solve the problem. I forgot to put aux inside the for loops. Now it works fine. About the complexity, I give it as an input to the program.

Thank you again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.