1. Not finding help here? Sign up for a free 30min 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!

Sorting array into ascending order in C++

  1. Mar 2, 2013 #1
    1. The problem statement, all variables and given/known data
    a.
    Define an array with a maximum of 20 integers values, and fill the array with numbers input from the keyboard or assigned by the program. Then write a function named split() that reads the array and places all zeros or positive numbers in an array named positive and all negative numbers in an array named negative. Finally, have your program call a function that displays the values in both the positive and negative arrays.
    b.
    Extend the program written for exercise a to sort the positive and negative arrays into ascending order before they are displayed.


    2. Relevant equations



    3. The attempt at a solution
    I am having problems with part b sorting the array of negative numbers into ascending order. The way I look at is the array named values calls the splitNegative function and stores the elements in the array titled negativeArray, then the first for statement sorts the elements into only negative elements storing only negative values in the array titled negativeArray. I then used a nested loop to try to sort the array into ascending order. I don't see what the problem is I used on the same technique on the splitPositive function and that works well.
    I am also completely new to programming, so excuse me if I don't use all the right terminology. Since I'm new I would also like for you to respond in the simplest way possible. Thank you!

    #include <iostream>

    using namespace std;

    void splitPositive ( int [] , int );

    void splitNegative ( int [] , int );

    int main()
    {
    const int size = 20;

    int values[ size ] = { 11, 21, 8, 4, 1,
    15, 35, 50, 89, 2,
    -11, -21, -8, -4, -1,
    -15, -35, -50, -89, -2 };

    splitPositive ( values , size );

    splitNegative ( values , size );

    return 0;
    }

    void splitPositive ( int positiveArray[] , int dimension )
    {
    int i, j, k, temporary, minimum, lowIndex;

    cout << "Positive Elements" << endl;

    for ( i = 0 ; i < 20 ; i++)
    {
    if ( positiveArray > 0 )
    {
    //cout << positiveArray <<endl;
    }

    }

    for ( j = 0 ; j < 10 ; j++ )
    {
    minimum = positiveArray[j];

    lowIndex = j;

    for ( k = j + 1; k < 10 ; k++ )
    {
    if ( positiveArray [k] < minimum )
    {
    minimum = positiveArray [k];

    lowIndex = k;
    }
    }

    if ( minimum < positiveArray[j] )
    {
    temporary = positiveArray[j];

    positiveArray [j] = minimum;

    positiveArray [lowIndex] = temporary;
    }

    cout << positiveArray [ j ] << endl;
    }

    return;
    }

    void splitNegative ( int negativeArray[] , int dimension )
    {
    int i, j, k, minimum, temporary, lowIndex;

    cout << "Negative Elements" << endl;

    for ( i = 0; i < dimension ; i++)
    {
    if ( negativeArray < 0 )
    {
    //cout << negativeArray << endl;
    }

    }

    for ( j = 0 ; j < dimension ; j++ )
    {
    minimum = negativeArray[j];

    lowIndex = j;

    for ( k = j + 1; k < dimension ; k++ )
    {
    if ( negativeArray [k] < minimum )
    {
    minimum = negativeArray [k];

    lowIndex = k;
    }
    }

    if ( minimum < negativeArray[j] )
    {
    temporary = negativeArray[j];

    negativeArray [j] = minimum;

    negativeArray [lowIndex] = temporary;
    }

    cout << negativeArray [ j ] << endl;
    }

    return;
    }
    /*Positive Elements
    1
    2
    4
    8
    11
    15
    21
    35
    50
    89
    Negative Elements
    -89
    -50
    -35
    -21
    -15
    -11
    -8
    -4
    -2
    -1
    1
    2
    4
    8
    11
    15
    21
    35
    50
    89*/
     
  2. jcsd
  3. Mar 2, 2013 #2

    rcgldr

    User Avatar
    Homework Helper

    You're not doing the first part of the problem. Your commented out code was just displaying (cout) positive and negative values. According to the problem statement, you need three arrays, values[], positive[], and negative[], and you're supposed to store all the positive numbers in positive[], and all the negative numbers in negative[]. Then after copying numbers from values[] into positive[] and negative[], you're suppose to sort positive[] and negative[].
     
  4. Mar 2, 2013 #3
    Here is a slightly better attempt at the first part of the problem. I am now stuck on how to transfer the positive and negative arrays to the display function and show the arrays on the screen by way of the display function. Also I just realized the prototype function for the display function is missing.

    #include <iostream>

    using namespace std;

    void split( int [] , int );

    int main()
    {
    const int size = 20;

    int values[ size ] = { -11, -21, -8, -4, -1,
    15, 35, 50, 89, 2,
    11, 21, 8, 4, 1,
    -15, -35, -50, -89, -2 };
    split ( values , size );

    return 0;
    }
    void split ( int calledArray [] , int amount )
    {
    int i, negativeNumber, positiveNumber;

    int negativeArray [ 20 ] = { 1,2,3,4,5,6,7,8,9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,20};

    int plusArray [ 20 ] = { 1,2,3,4,5,6,7,8,9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,20};

    for ( i = 0 ; i < amount ; i++)
    {
    if ( calledArray[ i ] > 0 )
    {
    positiveNumber = calledArray [ i ];
    plusArray[ i ] = positiveNumber;
    //cout << plusArray [ i ] << endl;
    }
    else
    {
    negativeNumber = calledArray [ i ];
    negativeArray [ i ] = negativeNumber;
    //cout << negativeArray [ i ] << endl;
    }
    }

    return;
    }
    /*void displayArray ( int positiveArray [] , int negativeArray [] , int dimension )
    {
    for ( j = 0 ; j < 10 ; j++ )
    {
    cout << positiveArray [j] << endl;
    }

    for ( k = 0 ; k < 10 ; k++ )
    {
    cout << negativeArray [ k ] << endl;
    }

    return;
    }*/
     
  5. Mar 3, 2013 #4

    rcgldr

    User Avatar
    Homework Helper

    You've made this more complicated than it needs to be. Also, you're not using the names that the problem statement asked for, positive[], and negative[]. You'll also probably want to have separate integer variables to use as indexes into positive[] and negative[] to store the numbers you copy from values[].
     
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: Sorting array into ascending order in C++
  1. Sorting Array (Replies: 6)

Loading...