C++ put numbers in ascending descending order

  • Context: Comp Sci 
  • Thread starter Thread starter cstuh
  • Start date Start date
  • Tags Tags
    Arrays C++ Numbers
Click For Summary
SUMMARY

The discussion centers on implementing a C++ function named sortMe that sorts an array of integers in either ascending or descending order without rearranging the original array. Instead, it utilizes a secondary array of indexes to achieve the sorting. The function takes an array, an array of indexes, the size of the array, and a character indicating the sorting mode ('a' for ascending and 'd' for descending). Participants highlight issues with variable scope and initialization, particularly the global variables a and d, and suggest improvements for better code structure.

PREREQUISITES
  • Understanding of C++ arrays and functions
  • Familiarity with sorting algorithms
  • Knowledge of variable scope and initialization in C++
  • Basic input/output operations in C++ using cin and cout
NEXT STEPS
  • Refactor the sortMe function to eliminate global variables
  • Implement error handling for user input in C++
  • Explore different sorting algorithms like QuickSort or MergeSort
  • Learn about the use of standard library functions such as std::sort
USEFUL FOR

C++ developers, computer science students, and anyone interested in algorithm design and implementation in C++.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K