- #1
cstuh
- 1
- 0
In this assignment, you are going to write a function called sortMe that sorts the elements of an array in numerical order from
highest to lowest values (descending order) or vice versa (ascending order).
The interesting point of the function is that sortMe does NOT re-arrange elements in the array; instead, it uses a second array, an
array of indexes for the elements in the original array and then sortMe 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.
Please note that you can declare and initialize the original array without any user input.
I'm not sure why my function isn't running even though I called it in main.
Any help would be appreciated! Thank you
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sortMe(int array[], int sortedIndexes[], int size, char mode);
char option;
const int SIZE = 5;
char a, d;
int smallestIndex, smallestValue, currentIndex, currentOriginalArray;
void main()
{
int array[SIZE] = { 45, 72, 4, 89, 11 };
int sortedIndexes[SIZE] = {};
int size = 5;
cout << "Enter a for ascending or d for descending order: ";
cin >> option;
sortMe( array, sortedIndexes, 5, option);
system("pause");
}
void sortMe(int array[], int sortedIndexes[], int size, char mode)
{
for (int sortedIndexes = 0; sortedIndexes < SIZE - 1; sortedIndexes++)
{
for (int smallestValue = 0; smallestValue < SIZE - 1; smallestValue++)
{
if (option == a)
{
if (smallestValue <= sortedIndexes)
{
smallestIndex = currentIndex;
smallestValue = array[smallestIndex];
}
}
else if (option == d)
{
if (smallestValue >= currentOriginalArray)
{
smallestIndex = currentIndex;
smallestValue = array[smallestIndex];
}
}
}
}
}
highest to lowest values (descending order) or vice versa (ascending order).
The interesting point of the function is that sortMe does NOT re-arrange elements in the array; instead, it uses a second array, an
array of indexes for the elements in the original array and then sortMe 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.
Please note that you can declare and initialize the original array without any user input.
Homework Equations
I'm not sure why my function isn't running even though I called it in main.
Any help would be appreciated! Thank you
The Attempt at a Solution
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sortMe(int array[], int sortedIndexes[], int size, char mode);
char option;
const int SIZE = 5;
char a, d;
int smallestIndex, smallestValue, currentIndex, currentOriginalArray;
void main()
{
int array[SIZE] = { 45, 72, 4, 89, 11 };
int sortedIndexes[SIZE] = {};
int size = 5;
cout << "Enter a for ascending or d for descending order: ";
cin >> option;
sortMe( array, sortedIndexes, 5, option);
system("pause");
}
void sortMe(int array[], int sortedIndexes[], int size, char mode)
{
for (int sortedIndexes = 0; sortedIndexes < SIZE - 1; sortedIndexes++)
{
for (int smallestValue = 0; smallestValue < SIZE - 1; smallestValue++)
{
if (option == a)
{
if (smallestValue <= sortedIndexes)
{
smallestIndex = currentIndex;
smallestValue = array[smallestIndex];
}
}
else if (option == d)
{
if (smallestValue >= currentOriginalArray)
{
smallestIndex = currentIndex;
smallestValue = array[smallestIndex];
}
}
}
}
}