C++ put numbers in ascending descending order

In summary: You should pass them as parameters to the function.In summary, the assignment requires the creation of a function called sortMe that sorts an array in either ascending or descending order without rearranging the elements. This is achieved by using a second array of indexes and sorting it based on the values in the original array. The sorted version of the original array can then be produced using these sorted indexes. The original array can be declared and initialized without any user input. The code provided includes a main function and a sortMe function, but the values of the a and d variables are not specified. It is also suggested to pass these variables as parameters instead of using global variables.
  • #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.

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
  • #2
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.
 

1. How do I sort numbers in ascending order in C++?

To sort numbers in ascending order in C++, you can use the sort function from the algorithm library. This function takes in the beginning and end iterators of a container as well as an optional comparison function, and rearranges the elements in ascending order.

2. How do I sort numbers in descending order in C++?

To sort numbers in descending order in C++, you can use the sort function with a custom comparison function. The comparison function should return true if the first element is greater than the second element, and false otherwise.

3. Can I use the sort function to sort numbers in-place?

Yes, the sort function in C++ performs an in-place sort, meaning it rearranges the elements within the container without creating a new container.

4. How do I sort numbers in a specific range in C++?

You can use the sort function with the begin and end iterators of the range you want to sort. This will only sort the elements within that range and leave the rest of the container unchanged.

5. What is the time complexity of sorting numbers in C++ using the sort function?

The sort function in C++ has an average time complexity of O(nlogn), where n is the number of elements in the container. This makes it a very efficient sorting algorithm for most use cases.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
927
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
Back
Top