C++ put numbers in ascending descending order

  • Context: Comp Sci 
  • Thread starter Thread starter cstuh
  • Start date Start date
  • Tags Tags
    Arrays C++ Numbers
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 6K views
cstuh
Messages
1
Reaction score
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.

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;

count << "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];
}
}
}
}
}
 
Physics news on Phys.org
Please use code tags when you post code here. This means a [ code ] tag at the top and a [ /code ] tag at the bottom, but without spaces.
cstuh said:
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.

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


C:
#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;

    count << "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];
                }
            }
        }
    }
}

In the sortMe function what are the values of your a and d variables? Also, it's a bad idea to make these global variables.