Rearranging elements of a matrix

Hello.
I need to rearrange the diagonal elements of a matrix myArray[4][4] in an increasing order.Here it is what i have done so far. I have got an error while running "Acess Violation".Could Somebody explain whats wrong with the code.Iam currently using Visual Studio 2010 Profeional


#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
#define WIDTH 4
#define HEIGHT 4
using namespace std;
int jimmy [HEIGHT][WIDTH];
int n,m,temp;

int main ()
{
srand ( static_cast<unsigned int>(time(NULL)) ); //initialize time
for (n=0;n<HEIGHT;n++) //set HEIGHT
for (m=0;m<WIDTH;m++) //set WIDTH
{
jimmy[n][m]=(rand( )%(99)); //Initilaize random Matrix
}
//Print Out Random Matrix
cout <<"|"<< jimmy[0][0]<<","<< jimmy[0][1]<<","<< jimmy[0][2]<<","<< jimmy[0][3]<<"|"<<endl;
cout <<"|"<< jimmy[1][0]<<","<< jimmy[1][1]<<","<< jimmy[1][2]<<","<< jimmy[1][3]<<"|"<<endl;
cout <<"|"<< jimmy[2][0]<<","<< jimmy[2][1]<<","<< jimmy[2][2]<<","<< jimmy[2][3]<<"|"<<endl;
cout <<"|"<< jimmy[3][0]<<","<< jimmy[3][1]<<","<< jimmy[3][2]<<","<< jimmy[3][3]<<"|"<<endl;


cout <<"The new martix rearranged to a raising diagonal\n";
while (m=n){
if (jimmy[m][n]>jimmy[m+1][n+1]){
jimmy[m][n]=temp;
jimmy[m][n]=jimmy[m+1][n+1];
jimmy[m+1][n+1]=temp;
}
m++,n++;
}
cout <<"|"<< jimmy[0][0]<<","<< jimmy[0][1]<<","<< jimmy[0][2]<<","<< jimmy[0][3]<<"|"<<endl;
cout <<"|"<< jimmy[1][0]<<","<< jimmy[1][1]<<","<< jimmy[1][2]<<","<< jimmy[1][3]<<"|"<<endl;
cout <<"|"<< jimmy[2][0]<<","<< jimmy[2][1]<<","<< jimmy[2][2]<<","<< jimmy[2][3]<<"|"<<endl;
cout <<"|"<< jimmy[3][0]<<","<< jimmy[3][1]<<","<< jimmy[3][2]<<","<< jimmy[3][3]<<"|"<<endl;

system ("pause");
return 0;
}
I've only given this a quick glance, but what happens when m and n = 4 in your while loop?
Last edited on
Before rearranging, you never reset m and n to 0. They are equal to HEIGHT and WIDTH after the for loops. Also you never check that m and n are under 4. In general that sorting algorithm is bad, but I assume you're not done yet..

Other issues are that you really should do the printing with for loops, there is no point in having two variables if they are going to be equal (in the sorting part) and the srand() cast would work implicitly, so there is no reason to put it there.
@JMJAtalanta When m=n it will compare only the only the diagonal elements of the matrix, jimmy[0][0],jimmy[1][1],jimmy[2][2],jimmy[3][3] so can rearrange then in an increasing order.
@Hamsterman I reset m and n before rearranging the matrix i have set them to 0 so i can initialize the matrix with random numbers for the sake of not entering them manually every time. Can u sugest a better algoritm for sorting them please?
Ok i have found out that i had a logical error on my sorting algorithm so i decided to use the bubblesort algorithm instead but i get an "Acess Violation" error and cant understand why.here it is what i have so far. any clue on this one guys?
Unhandled exception at 0x011bbfed in Laba6.exe: 0xC0000005: Access violation reading location 0x00000061.
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

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
#define WIDTH 4
#define HEIGHT 4
using namespace std;
int jimmy [HEIGHT][WIDTH];
int n,m;
void bubblesort (int jimmy[HEIGHT][WIDTH], int length);
int main ()
{
     srand ( static_cast<unsigned int>(time(NULL)) );               //initialize time
  for (n=0;n<HEIGHT;n++)                 //set HEIGHT
    for (m=0;m<WIDTH;m++)                //set WIDTH
    {
      jimmy[n][m]=(rand( )%(99));        //Initilaize random Matrix
    }
    //Print Out Random Matrix
    cout <<"|"<< jimmy[0][0]<<","<< jimmy[0][1]<<","<< jimmy[0][2]<<","<< jimmy[0][3]<<"|"<<endl;
    cout <<"|"<< jimmy[1][0]<<","<< jimmy[1][1]<<","<< jimmy[1][2]<<","<< jimmy[1][3]<<"|"<<endl;
    cout <<"|"<< jimmy[2][0]<<","<< jimmy[2][1]<<","<< jimmy[2][2]<<","<< jimmy[2][3]<<"|"<<endl;
    cout <<"|"<< jimmy[3][0]<<","<< jimmy[3][1]<<","<< jimmy[3][2]<<","<< jimmy[3][3]<<"|"<<endl;
    
    bubblesort(jimmy, 10);
    
    cout <<"|"<< jimmy[0][0]<<" , "<< jimmy[0][1]<<" , "<< jimmy[0][2]<<" , "<< jimmy[0][3]<<"|"<<endl;
    cout <<"|"<< jimmy[1][0]<<" , "<< jimmy[1][1]<<" , "<< jimmy[1][2]<<" , "<< jimmy[1][3]<<"|"<<endl;
    cout <<"|"<< jimmy[2][0]<<" , "<< jimmy[2][1]<<" , "<< jimmy[2][2]<<" , "<< jimmy[2][3]<<"|"<<endl;
    cout <<"|"<< jimmy[3][0]<<" , "<< jimmy[3][1]<<" , "<< jimmy[3][2]<<" , "<< jimmy[3][3]<<"|"<<endl;
    
    system ("pause");
  return 0;
}
void bubblesort (int jimmy[HEIGHT][WIDTH], int length)
{
	int index;
	int iteration;
	int temp;
		for(iteration = 1; iteration < length; iteration++)
		{
			for(index = 0; index < length - iteration;index++)
				if(jimmy[index][index] > jimmy[index + 1][index + 1])
				{
				temp = jimmy[index][index];
				jimmy[index][index] = jimmy[index + 1][index + 1];
				jimmy[index + 1][index + 1] = temp;
				}
		
		
		}
}
Why did you pass 10 for length?
@hamsterman That was the question that solved the problem )))Thats why i had the Acess Violation error. I used the BubbleSort Algorithm from a previous project and forgot that my array in this case has a length of only 4. just modified it from 10 to 4 and now runs just fine.Thank you so much for the help guys. here is the solved variant
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
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
#define WIDTH 4
#define HEIGHT 4

using namespace std;

int jimmy [HEIGHT][WIDTH];

int n,m;

void bubblesort (int jimmy[HEIGHT][WIDTH], int length);

int main ()
{
     srand ( static_cast<unsigned int>(time(NULL)) );               //initialize time
  for (n=0;n<HEIGHT;n++)                 //set HEIGHT
    for (m=0;m<WIDTH;m++)                //set WIDTH
    {
      jimmy[n][m]=(rand( )%(99));        //Initilaize random Matrix
    }
    //Print Out Random Matrix

	cout <<"The Random Generated Matrix \n";
    cout <<"|"<< jimmy[0][0]<<" , "<< jimmy[0][1]<<" , "<< jimmy[0][2]<<" , "<< jimmy[0][3]<<"|"<<endl;
    cout <<"|"<< jimmy[1][0]<<" , "<< jimmy[1][1]<<" , "<< jimmy[1][2]<<" , "<< jimmy[1][3]<<"|"<<endl;
    cout <<"|"<< jimmy[2][0]<<" , "<< jimmy[2][1]<<" , "<< jimmy[2][2]<<" , "<< jimmy[2][3]<<"|"<<endl;
    cout <<"|"<< jimmy[3][0]<<" , "<< jimmy[3][1]<<" , "<< jimmy[3][2]<<" , "<< jimmy[3][3]<<"|"<<endl;
    
    bubblesort(jimmy, 4);
    cout <<"The Sorted Matrix\n ";
    cout <<"|"<< jimmy[0][0]<<" , "<< jimmy[0][1]<<" , "<< jimmy[0][2]<<" , "<< jimmy[0][3]<<"|"<<endl;
    cout <<"|"<< jimmy[1][0]<<" , "<< jimmy[1][1]<<" , "<< jimmy[1][2]<<" , "<< jimmy[1][3]<<"|"<<endl;
    cout <<"|"<< jimmy[2][0]<<" , "<< jimmy[2][1]<<" , "<< jimmy[2][2]<<" , "<< jimmy[2][3]<<"|"<<endl;
    cout <<"|"<< jimmy[3][0]<<" , "<< jimmy[3][1]<<" , "<< jimmy[3][2]<<" , "<< jimmy[3][3]<<"|"<<endl;
    
    system ("pause");
  return 0;
}
void bubblesort (int jimmy[HEIGHT][WIDTH], int length)
{
	int index;
	int iteration;
	int temp;
		for(iteration = 1; iteration < length; iteration++)
		{
			for(index = 0; index < length - iteration;index++)
				if(jimmy[index][index] > jimmy[index + 1][index + 1])
				{
				temp = jimmy[index][index];
				jimmy[index][index] = jimmy[index + 1][index + 1];
				jimmy[index + 1][index + 1] = temp;
				}
			
		
		}
}
Topic archived. No new replies allowed.