functions

Lads, need your help over here, beside all other tasks that works fine, it was given to write a code using a single function which has to sort in ascending order and descending order the elements of an array.

I've done something but my boolean variable won't work properly, it works just for ascending or descending order, but not both at the same time, please enlighten me how to correctly use both statements of bool in order to display ascending and descending order.s (line 82-107 and line 139-140 )
Many thanks!
btw I use IDE, DEV-C++ for now...

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
 #include <cstdlib>
 #include <ctime>
 #include <cstring>
 #include <conio.h>
 #include <iomanip>
 #include <windows.h>

 using namespace std;

int main()


void insertV(int z[], int n) //void insertV(int v[100], int n)

 {
 for(int i=0; i<n; i++)
 {
 z[i]=rand()%10;
 }
 }


 void showV(int z[], int n) //display (int v[100], int n)

 {
 for(int i=0; i<n; i++)
 {
 cout<<setw(3)<<z[i];
 }
 }


 int sumaV(int z[], int n) //array sum

 {
 int s=0;
 for(int i=0; i<n; i++)
 {
 s+=z[i];
 }
 return s;
 }
 
 

 int  maxV(int z[], int n) // maximum element in array 

 {
 int m=z[0];
 for(int i=0; i<n; i++)
 {
 if (m<z[i]) m=z[i];
 }
return m;
 }


int maxp(int z[],int n) //maximum odd value;
{
	int mp=z[0];
	for(int i=0; i<n; i++)
	{
		 if (z[i] %2==0 && mp < z[i]) mp=z[i];
	}
	return mp;
}


int mini(int z[],int n) //minimum even value;
{
	int mi=z[0];
	for(int i=0; i<n; i++)
	{
		 if (z[i] %2 !=0 && mi > z[i]) mi=z[i];
	}
	return mi;
}
	
	
	
void ad(int z[],int n, bool q)// ascending & descending order
{

bool x=true;
 while(x)
{  
  x=false;  
  
  
    for( int i=0,temp; i < n-1; i++)
    {
    	if(q){
		
     if(z[i] < z[i+1] )
       {
            x=true;
          temp = z[i];
          z[i] = z[i+1];
          z[i+1] = temp; }}
		  
		   if(z[i] > z[i+1] )
       {
            x=true;
          temp = z[i];
          z[i] = z[i+1];
          z[i+1] = temp;       
} 
}       
}
}

 {

 srand(time(0));


 int v[100],n=insertINT("\n Enter n=");


 insertV(v,n);

 showV(v,n);


 int sv=sumaV(v,n);

 cout<<" sv="<<sv<<endl;

 int m=maxV(v,n);
 cout<<" max="<<m<<endl;
 
 int pi=maxp(v,n);
 cout<<" maxeven="<<pi<<endl;
 
 int ci=mini(v,n);
 cout<<" minodd="<<ci<<endl;
 
  ad(v,n,true);      //I am trying to somehow force the app to check both true 
  ad(v,n,false);     //and false conditions 
  showV(v,n);

   return 0;

 }
Last edited on
a single function which has to sort in ascending order and descending order the elements of an array.


What does this mean?

If the input is, for example, { 3 , 8, 2, 54 , -3}

and your function sorts this into ascending and descending order, what's the correct output?

{-3, 2, 3, 8, 54} is ascending.
{54, 8, 3, 2, -3} is descending.

What is "ascending and descending order"?
@Reapeater.

It should display both orders using the same function, both algorithms should be included in 1 function.
C++ doesn't allow local functions. So main() should be after all the other functions have been defined.

What is "ascending and descending order"?


ascending/descending order depending upon value of bool q in ad()

- for maxV(), i should start at 1 not 0 as m is already z[0]

- for maxp(), mini() your starting value is incorrect. for maxp() (max odd), if the first number is even and less than all the other odd numbers, then it will incorrectly return this even number. Similar for mini (minimum even)

Also z %2==0 is [i]true if z is [i]even, not odd! For maxp(), mini() the condition tests need to be reversed.

Last edited on
thanks guys, I guess my English is too bad in order to briefly explain what I was meaning to do, the problem was that my function " void ad(int z[],int n, bool q)" line 82-107 had some code mistakes, now I've handled it, using it right now it will display ascending and descending order of array.

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

void ad(int z[],int n, bool q)
{

bool x=true;
 while(x)
{  
  x=false;  
  
  if(q){
    for( int i=0,temp; i < n-1; i++)
    {
	
     if(z[i] < z[i+1] )
       {
            x=true;
          temp = z[i];
          z[i] = z[i+1];
          z[i+1] = temp; }}}
		  
		   else {
		   
		   for( int i=0,temp; i < n-1; i++)
           {
		   if (z[i] > z[i+1] ){
            x=true;
          temp = z[i];
          z[i] = z[i+1];
          z[i+1] = temp;       
}       
}
}
}
}
.......

  ad(v,n,true);
  showV(v,n);
  cout<<endl;
  ad(v,n,false);
  showV(v,n);
ad() can be simplified. Consider:

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
109
110
111
112
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <limits>

using namespace std;

int insertINT(const char* mes)
{
	int value;

	cout << mes;
	cin >> value;

	return value;
}

void insertV(int z[], int n)
{
	for (int i = 0; i < n; ++i)
		z[i] = rand() % 100;
}

void showV(int z[], int n)
{
	for (int i = 0; i < n; ++i)
		cout << setw(3) << z[i];

	cout << '\n';
}

int sumaV(int z[], int n) //array sum
{
	int s = 0;

	for (int i = 0; i < n; ++i)
		s += z[i];

	return s;
}

int maxV(int z[], int n) // maximum element in array
{
	int m = z[0];

	for (int i = 1; i < n; ++i)
		if (m < z[i])
			m = z[i];

	return m;
}

int maxp(int z[], int n) //maximum odd value;
{
	int mp = numeric_limits<int>::min();

	for (int i = 0; i < n; i++)
		if (z[i] % 2 != 0 && mp < z[i])
			mp = z[i];

	return mp;
}

int mini(int z[], int n) //minimum even value;
{
	int mi = numeric_limits<int>::max();

	for (int i = 0; i < n; i++)
		if (z[i] % 2 == 0 && mi > z[i])
			mi = z[i];

	return mi;
}

void ad(int z[], int n, bool q)
{
	bool x = true;

	while (x) {
		x = false;

		for (int i = 0, temp; i < n - 1; ++i)
			if (q ? z[i] < z[i + 1] : z[i] > z[i + 1]) {
				x = true;
				temp = z[i];
				z[i] = z[i + 1];
				z[i + 1] = temp;
			}
	}
}

int main()
{
	srand(time(0));

	int v[100], n = insertINT("\n Enter n=");

	insertV(v, n);
	showV(v, n);

	cout << " sv=" << sumaV(v, n) << endl;
	cout << " max=" << maxV(v, n) << endl;
	cout << " maxodd=" << maxp(v, n) << endl;
	cout << " mineven=" << mini(v, n) << endl;

	ad(v, n, true);
	showV(v, n);

	ad(v, n, false);
	showV(v, n);
}

Topic archived. No new replies allowed.