#include <iostream>
#include <algorithm>
bool cmp_fn(int a,int b)
{
return a>b;
}
int main() {
int array[] = { 1,10,21,55,665,556 };
int elements = sizeof(array) / sizeof(array[0]); // Get number of elements in array
std::sort(array, array + elements,cmp_fn);
for (int i = 0; i < elements; ++i) // print the results
std::cout << array[i] << ' ';
}
I would use some kind of sort function. Maybe something like bubblesort (this is a simple way to sort but not the fastest) is what you're seeking. I forget how the code goes but i'm sure you can find it by searching for the function on google.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm>
usingnamespace std;
bool cmp_fn(int a,int b)
{
return a>b;
}
int main(int argc, char *argv[])
{
ifstream in("input.txt");
int num;
in>>num;
int a[100];
for (int i = 0;i <= num; i++){
in>>a[i];
}
// Get number of elements in array
sort(a, a + num,cmp_fn);
for (int i = 0; i < num; ++i) // print the results
cout << a[i] << ' ';
system("pause");
}
now it prints the integers in the array sorted from highest to lowest.
How to change the code so it would print the locations of the numbers in the array instead of the numbers itselfs?
for example, if i type in
4 //the number of integers in the array
11 //the integers
31
26
48