Comp Sci C++ put numbers in ascending descending order

AI Thread Summary
The discussion focuses on creating a C++ function called sortMe that sorts an array's elements in ascending or descending order using a secondary array of indexes. The function is designed to avoid rearranging the original array, instead sorting the indexes based on the values. Users are prompted to choose the sorting order, but there are issues with the function's implementation, particularly with variable scope and logic errors in the sorting loops. Participants highlight that the global variables for sorting options (a and d) are poorly designed and suggest keeping them local to improve code clarity. The thread emphasizes the need for debugging to ensure the function operates as intended.
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;

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

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

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.
 

Similar threads

Replies
3
Views
1K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
4
Views
2K
Replies
3
Views
8K
Replies
21
Views
3K
Replies
7
Views
2K
Back
Top