Sorting array into ascending order in C++

  • Comp Sci
  • Thread starter tri5
  • Start date
  • #1
11
0

Homework Statement


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.


Homework Equations





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*/
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,707
534
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[].
 
  • #3
11
0
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;
}*/
 
  • #4
rcgldr
Homework Helper
8,707
534
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[].
 

Related Threads on Sorting array into ascending order in C++

Replies
12
Views
3K
Replies
1
Views
5K
  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
8K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
6
Views
6K
Replies
6
Views
2K
Replies
0
Views
1K
Replies
6
Views
19K
Top