Need Help with void prototype (sorting arrays) increasing/decreasing

Hi, I was struggling with the following question from an assignment I have. The code below works but the question asks to the write just the definition for the void sort(int a[], int size, bool order); The problem is that what I want is done in the int main() function of the following example code. So, I'm not sure how to write the definition for just the prototype with the bool. Thanks.

Question:

Consider the function prototype below. This function sorts the elements of
array a in increasing order when order is true and in decreasing order when order is false. Parameter size is simply the size of array a.

I basically need help writing the definition of the following prototype that follows the above question. Thanks.

void sort(int a[], int size, bool order);

Write the prototype’s definition. Submit only the function definition, nothing else.

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
 #include <iostream>
using namespace std;
void fillArray(int a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through a[numberUsed - 1] have been filled with
//nonnegative integers read from the keyboard.
void sort(int a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
void swapValues(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Reference array elements
//have values. Returns the index i such that a[i] is the smallest of the//values a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].
int main()
{
	cout << "This program sorts numbers in increasing order and then decreasing order.\n";
	int sampleArray[10], numberUsed;
	fillArray(sampleArray, 10, numberUsed);
	sort(sampleArray, numberUsed);
	cout << "In sorted increasing order, the numbers are:\n";
	for (int index = 0; index < numberUsed; index++)
	cout << sampleArray[index] << " ";
	cout << endl;

	cout << "In sorted decreasing order, the numbers are:\n";
	for (int index = numberUsed - 1; index >= 0; index--)
		cout << sampleArray[index] << " ";
	cout << endl;
	return 0;
	}
void fillArray(int a[], int size, int& numberUsed)
{
	cout << "Enter up to " << size << " nonnegative whole numbers.\n"
		<< "Mark the end of the list with a negative number.\n";
	int next, index = 0;
	cin >> next;
	while ((next >= 0) && (index < size))
	{
		a[index] = next;
		index++;
		cin >> next;
	}
	numberUsed = index;
}
void sort(int a[], int numberUsed)
{
	int indexOfNextSmallest;
	for (int index = 0; index < numberUsed - 1; index++)
		{//Place the correct value in a[index]:
		indexOfNextSmallest =
			indexOfSmallest(a, index, numberUsed);
		swapValues(a[index], a[indexOfNextSmallest]);
		//a[0] <= a[1] <=...<= a[index] are the smallest of the original array
			//elements. The rest of the elements are in the remaining positions.
			}
	}
void swapValues(int& v1, int& v2)
{
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;

}

int indexOfSmallest(const int a[], int startIndex, int numberUsed)
{
	int min = a[startIndex],
		indexOfMin = startIndex;
	for (int index = startIndex + 1; index < numberUsed; index++)
		if (a[index] < min)
		{
		min = a[index];
		indexOfMin = index;
		//min is the smallest of a[startIndex] through a[index]
			}
	return indexOfMin;
	}
Last edited on
void sort(int a[], int size, bool order);

int compareinc(const void *a, const void *b){return *(int*)a-*(int*)b;}
int comparedes(const void *a, const void *b){return *(int*)b-*(int*)a;}
void sort(int a[], int size, bool order)
{
if(order)
qsort(a,size,sizeof(int),compareinc);
else qsort(a,size,sizeof(int),comparedes);
}

int main()

int sampleArray[10], n=10;
for (int i=0;i<n;i++)
{
sampleArray[i]=rand()%100;
}
sort(sampleArray,10,false);
sort(sampleArray,10,true);
return 0;
}
Thanks, but could u maybe explain the code especially the first two lines under the void sort. Why r there voids in the parameters along with asteriks. Is there any simpler way of writing it. Also, when I'm asked for just the definition for the void prototype, what is meant by that, would int main be part of the void functions's definition or is the main one a separate definition. Thanks.
Topic archived. No new replies allowed.