Nov 18, 2014 at 9:43am UTC
Hi I'd like to modify this code so that there is 1000 numbers instead of 100, and that they're sorted in ascending order instead of descending order. Any chance someone could help me with this? Thank you in advance.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double* sort_array(double* sort) {
for (int a=1; a<100; a++) {
for (int b=99; b>=a; b--) {
if (sort[b-1] < sort[b]) {
double t = sort[b-1];
sort[b-1] = sort[b];
sort[b] = t; }
}
}
return sort;}
int main(){
srand(time(0));
double array[100];
for (int i=0;i<100;i++) {
int random = rand();
array[i] = random % 250 + (((random) % 1001) * 0.001); }
sort_array(array);
for (int x=0;x<100;x++) {
cout << array[x] << endl;
}
return 0; }
Nov 18, 2014 at 11:05am UTC
Change the 100s to 1000.
Change the comparison from < to > (in if (sort[b-1] < sort[b])
)
Nov 18, 2014 at 5:56pm UTC
How could you change this program so that the programmer can choose to sort the numbers in ascending or descending order?