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