ascending&descending order

hey lads, I am stuck cause of my lack of attention I guess,

the code below works fine for displaying ascending order of a vector using pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    while (!*px)
  {
  	*px=true;
  	for(int i=0, t; i<*pn-1; i++) {
  		if (*(pv+i) > *(pv+i+1)){	  
  	* px     = false;
  	*(pv+i)  = t;
  	*(pv+i)  = *(pv+i+1);
  	*(pv+i+1)= t;
  	
}
}
}
//display    

for(int i=0; i<*pn; i++)
   {
     cout<< setw(3) << *(pv+i);
   }


Now using the code above I was trying to write a local function which orders ascending and descending the main and secondary diagonals of a matrix.And the algorithm should after be used as a local function
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
//creating the matrix
 template<typename X>
void insertM(X M[][100], int n, int m) 
 {
 for(int i=0; i<n; i++)
 {
 for(int j=0; j<m; j++)
 {
 M[i][j]=rand()%10;
 }
 }
 }

//display
template<typename X>
 void showM(X M[][100], int n, int m)
 {
 for(int i=0; i<n; i++)
 {
 for(int j=0; j<m; j++)
 {
 cout<<setw(3)<<M[i][j];
 }
 cout<<endl;
 }
 }

// function that finds the main and secondary diagonals, which later will be 
// called in the main function :

void dps(int M[][100], int n, bool x)
 {
 if(x)
 {
 for(int i=0; i<n; i++)
 {
 cout<<setw(3)<<M[i][i];
 }
 }
 else
 {
 for(int i=0; i<n; i++)
 {
 cout<<setw(3)<<M[ (n-1)-i][i];
 }
 }
 }
 
 // display 
  void dps1(int M[][100], int n, bool x)
 {
 for(int i=0; i<n; i++)
 {
 if(x) cout<<setw(3)<<M[i][i];

 else cout<<setw(3)<<M[ (n-1)-i][i];
 }
 }
 
 // ascending & descending order of the diagonal ? but it won't work 
 void ad(int M[][100],int n, bool q) 
{
javascript:PostPreview()
bool x=true;
while(x)
{  
  x=false;  
  if(q){
    for( int i=0,temp; i < n-1; i++)
    {
	
     if(M[i] < (M[i+1]) )
       {
            x=true;
            temp        = M[i];
            M[i]        = (M[i+1]) ;
            ((M[i+1]))  = temp; }
		  
		   else {
		   
          if(M[i] > (M[i+1])  )
         {
            x=true;
          temp      = M[i];
          M[i]      =(M[i+1]) ;
          M[i+1])   = temp; }}}}} } 
       

If there is any need of my entire code I will be pleased to add it.
Last edited on
Some general suggestions.
1. Indent your code.
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
//creating the matrix
template < typename X > void insertM(X M[][100], int n, int m)
{
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      M[i][j] = rand() % 10;
    }
  }
}

//display
template < typename X > void showM(X M[][100], int n, int m)
{
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << setw(3) << M[i][j];
    }
    cout << endl;
  }
}

// function that finds the main and secondary diagonals, which later will be
// called in the main function :
void dps(int M[][100], int n, bool x)
{
  if (x) {
    for (int i = 0; i < n; i++) {
      cout << setw(3) << M[i][i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      cout << setw(3) << M[(n - 1) - i][i];
    }
  }
}

 // display
void dps1(int M[][100], int n, bool x)
{
  for (int i = 0; i < n; i++) {
    if (x)
      cout << setw(3) << M[i][i];
    else
      cout << setw(3) << M[(n - 1) - i][i];
  }
}

 // ascending & descending order of the diagonal ? but it won't work
void ad(int M[][100], int n, bool q)
{
  bool x = true;
  while (x) {
    x = false;
    if (q) {
      for (int i = 0, temp; i < n - 1; i++) {
        if (M[i] < (M[i + 1])) {
          x = true;
          temp = M[i];
          M[i] = (M[i + 1]);
          ((M[i + 1])) = temp;
        }
        else {
          if (M[i] > (M[i + 1])) {
            x = true;
            temp = M[i];
            M[i] = (M[i + 1]);
            M[i + 1]) = temp;
          }
        }
      }
    }
  }
}


2. Pick meaningful variable names.
This isn't the 1970's where storing every character cost you 1ยข (literally), and you had to type every character manually.

Storage is cheap, and auto-completion tools in any competent source code editor cut down on the keystrokes massively.

i,j are fine for loop indices and subscripts, but pretty much every other symbol should be readable (that means not cryptic dps or ad).

What you gain is instant comprehension rather than having to figure out every time what 'bool x' actually means.

3. Having a staircase of more than 2 or 3 closing braces means you should be refactoring that bit of code.
Why not:

1
2
3
4
5
6
7
8
9
10
11
12
void ad(int M[][100], int n, bool q)
{
	for (bool x = true; x; ) {
		x = false;
		for (int i = 0; i < n - 1; ++i) {
			if (q ? M[i] < (M[i + 1]) : M[i] > (M[i + 1])) {
				x = true;
				std::swap(M[i], M[i + 1]);
			}
		}
	}
}


but this is for a 1-dim array, but M is 2-dimen?? What are you sorting?


1
2
3
4
5
6
void dpsl1int M[][100], int n, bool x)
{
  for (int i = 0; i < n; ++i)
      cout << setw(3) << M[x ? i : (n - 1) - i][i];

}


Last edited on
@salem c - thanks for your advice, I will keep in mind.

@seeplus I am trying to sort in ascending and descending order the Main and Secondary diagonals of a matrix( 2d), but it won't work no matter what I change , I am only at the beginning of learning c++ , and I have to manage the orders wihtout using built in functions like "std::swap", many thanks
Last edited on
Topic archived. No new replies allowed.