Array sorting code help!!

I need help with writing a function called swap that sorts the elements of an array in order from highest to lowest values where they descend and ascend. A particular thing about this function is that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for the elements in the original array and then swap sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. You can declare and initialize the original array without any user input. For example, int arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };
Header of the function swap must be as shown below: void swap(int array[],int swapedIndexes [], int size, char mode) When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order. int swappedIndexes[5];swap(array,swappedInde… 5, 'a');


So far I have this code that randomly generates arrays of 10 elements and I found the max and min, but I am confused on what to do about the swapping part? Also I cant figure out how to make the numbers ascend or descend in numerical order?? Can somebody please please help me to finish with writing this code?
Here is what i have done so far:

#include<ctime>
#include <iostream>
using namespace std;
int min(int [],int);
void max(int [],int , int &);
void main()
{ srand(time(0));
//1-declare
int g[10];
int res=0;
//2-init
for(int i=0;i<10;i++)
{ g[i]=rand()%250;
cout <<g[i] << "\t";
}

//3- processing
res=min(g,10);
cout << "\nthe minimum is " << res<<endl;
max(g,10,res);
cout << "the maximum is " << res<<endl;
//printing


system("pause");
}
int min(int a[],int n)
{ int m=a[0];

for(int i=1;i<n;i++)
if( a[i] <m)
m=a[i];
return m;
}
void max(int a[],int n, int &m)
{ m=a[0];

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

}
dear friend !
if you want to write it by yourself i can help you by :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main() {
	int a[10], swap;
	for ( int i = 0; i < 10; ++i )
		cin >> a[i];
	for ( int i = 0; i < 10; ++i ) {
		for ( int j = i; j < 10; ++j ){
			if( a[j] > a[i] ){
				swap = a[i];
				a[i] = a[j];
				a[j] = swap;
			}
		}
	}

	for ( int i = 0; i < 10; ++i )
		cout << a[i] << " ";	
	return 0;
}


===========================================

but i suggest you to use sort function in <algorithm> & <vector> header files

1
2
3
vector<double> a;
//filling vector by a.push_back(/*something from STDIN or a file*/);
sort(a.begin(), a.end(), /*a function class like greater_than or less_than*/);
Topic archived. No new replies allowed.