1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ put numbers in ascending descending order

  1. Nov 9, 2014 #1
    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.

    2. Relevant equations
    I'm not sure why my function isnt running even though I called it in main.
    Any help would be appreciated! Thank you

    3. 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];
    }
    }
    }
    }
    }
     
  2. jcsd
  3. Nov 9, 2014 #2

    Mark44

    Staff: Mentor

    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.
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ put numbers in ascending descending order
Loading...