Sorting array into ascending order in C++

In summary: SIZE = 20;void split(int[], int, int[], int[], int&, int&);void display(int[], int, int[], int);int main(){ int values[SIZE] = { -11, -21, -8, -4, -1, 15, 35, 50, 89, 2, 11, 21, 8, 4, 1, -15, -35, -50, -89, -2 }; int positive[SIZE], negative[SIZE]; int pSize = 0,
  • #1
tri5
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*/
 
Physics news on Phys.org
  • #2
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
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
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[].
 
  • #5




Thank you for providing your code and explaining your thought process. I can see that you have attempted to use a selection sort algorithm to sort the positive and negative arrays. However, there are a few issues with your implementation that may be causing problems.

Firstly, in your splitNegative function, you are using the same loop condition as in your splitPositive function (j < dimension). This means that your loop will run for 20 iterations, even though there are only 10 elements in the negative array. This could potentially cause errors or incorrect sorting.

Secondly, in your inner loop, you are comparing the current element (negativeArray[j]) with the minimum element, and if it is smaller, you are swapping them. However, in your code, you are not actually swapping the elements, but instead assigning the minimum element to the current element. This means that the value at the current element will be overwritten and lost. To fix this, you need to swap the elements using a temporary variable, just like you did in your splitPositive function.

Finally, your algorithm is only sorting the first 10 elements in the array. This is because in your outer loop, you have specified that it should only run for 10 iterations (j < 10). To sort the entire array, you need to change this condition to j < dimension.

To summarize, here are the changes you need to make in your splitNegative function:

1. Change the loop condition to j < dimension instead of j < 10.
2. Use a temporary variable to swap the elements in your inner loop.
3. Change the loop condition in your inner loop to k < dimension instead of k < 10.

With these changes, your code should be able to sort both the positive and negative arrays into ascending order. I hope this helps and good luck with your future programming endeavors!
 

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

To sort an array in ascending order in C++, you can use the built-in function std::sort() from the algorithm library. This function takes in the beginning and end iterators of the array, as well as an optional comparison function. For example, to sort an array of integers arr in ascending order, you can use std::sort(arr, arr + size); where size is the size of the array.

2. Can I use a custom comparison function to sort an array in ascending order?

Yes, you can use a custom comparison function with the std::sort() function. The comparison function should take in two elements of the array and return true if the first element is considered less than the second, or false otherwise. For example, if you have an array of strings arr and want to sort them in ascending alphabetical order, you can use std::sort(arr, arr + size, myComparisonFunction);

3. Are there any other sorting algorithms I can use in C++?

Yes, there are many other sorting algorithms available in C++. Some common ones include std::stable_sort(), std::partial_sort(), and std::nth_element(). It's important to choose the right sorting algorithm for your specific use case, as they may have different time and space complexities.

4. How do I sort an array of objects in ascending order in C++?

To sort an array of objects in ascending order in C++, you can either define a custom comparison function or overload the < operator for your object class. Then, you can use std::sort() as you would with an array of primitive data types.

5. What is the time complexity of sorting an array in ascending order in C++?

The time complexity of sorting an array in ascending order in C++ depends on the sorting algorithm used. For example, std::sort() has a worst-case time complexity of O(nlogn) while std::stable_sort() has a worst-case time complexity of O(nlog^2n). It's important to consider the time complexity when choosing a sorting algorithm for your specific use case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
Back
Top