Sorting numbers in an array

I need to sort numbers a user entered in an array from largest to smallest but from the upper and lower bound that they give, the rest of the numbers just follow unsorted. I have all of the code but it is not working quite right. Any help please?

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
  #include <iostream>
using namespace std;
bool isSorted(int const numbers[], int low, int high);
void swap(int numbers[], int i, int j);
void sort(int numbers[], int low, int high);
void display(const int numbers[], int low, int high);
int getindexoflargest(int const numbers[], int low, int high);
int main()
{
	int low;
	int high;
	int i;
	int numbers[10];
	cout<<"Enter 10 numbers:\n";
	for(i=0; i<10; i++) {
		cin>>numbers[i];
	}
	cout<<"Enter lower and upper bound:\n";
	cin>>low;
	cin>>high;
	
	if (isSorted(numbers,low,high)) {
		cout<<"Range is sorted.\n";
	}
	else 
	{
		cout<<"Range is not sorted.\n";
	
	}
	cout<<"After sorting:\n";
	sort(numbers,low, high);
	display(numbers, 0, 9);
	
	return 0;
}

bool isSorted(int const numbers[], int low, int high)
{
    int yes=0;
    int range;
    range=high-low;
    for (int i=low; i<high; i++) {
        if (numbers[i]<=numbers[i+1])
        {
            yes=yes+1;
        }
    }
    if (yes==range) {
        return true;
    }
    
    else
        return false;
    
}
int getindexoflargest(int const numbers[], int low, int high) {
	int index=high;
	int i;
	for (i=low + 1; i <= high; i = i+1) {
		if (numbers[i] > numbers[index]) {
			index=i;
		}
	}
	return index;
}
void swap(int numbers[], int i, int j) {
	int temp=numbers[i];
	numbers[i]=numbers[j];
	numbers[j]=temp;
	return;
}
void display(const int numbers[], int low, int high) {
	int i;
	for (i=low; i<=high; i++) 
	{
		cout<<numbers[i]<<"\n";
	}
	return;
}

void sort(int numbers[], int low, int high) {
	int i;
	int index;
	for (i=low; i <= high; i++) {
		index=getindexoflargest(numbers, low, high);
		swap(numbers, i, index);
		
	}
}
closed account (jvqpDjzh)
You don't specify what the compiler says, but I think the function getIndexOfLargest() has at least 2 problems:
//

int getIndexOfLargest(const int numbers[], int low, int high) {//I advise you to use the "camelCase"
int index = high;
int i;
for (i = low; i < high; i++)//we don't need to control the last element of the array!
//why do you want to begin from low+1 ?
if (numbers[i] > numbers[index])
index = i;
return index;
}

//
//line 70 && 78: Why did you return nothing? (we don't need the "return" statement anyway!)
//
//line 84: you don't have to go til the end of range (til "high" included!)... does so:for (i=low; i < high; i++) ....
//
//line 85: the second parameter of the getIndexOfLargest() has to be i and not low, because low is constant and when you call getIndexOfLargest(), this last one does always the same thing!!! :):
getIndexOfLargest(numbers, i, high);
Last edited on
Topic archived. No new replies allowed.