Homework 28

Write a program that reads 10 positive `int`s from the user and prints them in order so that all the odd numbers come first (in increasing size) followed by all the even numbers (in increasing size).

Here is a sample run of the program (with user input in red):

`        `

Enter 10 positives ints: 18 2 7 14 29 3 5 8 16 11

Before sorting, the numbers are: 18 2 7 14 29 3 5 8 16 11

After sorting, the numbers are: 3 5 7 11 29 2 8 14 16 18

` `

Hint: Reread the lecture notes that use the before() function.  In this homework, you will need to make a before() function that meets the specific needs of your program. You are welcome to use the below program or develop one from scratch. All that is missing from the below program is the definition of the boolean function before() that is used by selectionSort() to allow sorting by following notion:  odd numbers come first (in increasing size) followed by the even numbers (in increasing size)

Turn In:

1.      A screen capture of your program running on the above input, and a printout of your source code.

// DMN

// Hmwk28

// This reads 10 positive ints from the user and prints them in order

// so that all the odd numbers come first (in increasing size) followed

// by all the even numbers (in increasing size). Students need to complete

// the before() function.

#include <iostream>

using namespace std;

//prototypes

bool before(int,int);

bool odd(int);

void print(int*,int);

void selectionSort(int*, int);

int main(){

int* theNums = new int[10];

cout << "Enter 10 positives ints: ";

for(int i=0;i<10;i++)

cin >> theNums[i];

cout << "Before sorting, the numbers are: ";

print(theNums,10);

selectionSort(theNums,10);  // odds before evens

cout << "After sorting, the numbers are: ";

print(theNums,10);

delete [] theNums;

theNums = NULL;

return 0;

}

//This prints the array

void print(int* numsIn, int size){

for(int i=0;i<size;i++)

cout << numsIn[i] << ' ';

cout << endl;

}

// This sorts the array with odds occurring first

void selectionSort(int *arrayToSort, int size){

for(int length = size; length > 1; length--){

// Find iMax, the index of the largest

int iMax = 0, i;

for(i = 1; i < length; i++)

if (before(arrayToSort[iMax],arrayToSort[i]))

iMax = i;

// Swap A[iMax] & the last element

int temp = arrayToSort[iMax];

arrayToSort[iMax] = arrayToSort[length - 1];

arrayToSort[length - 1] = temp;

}

}

//Returns true if a is odd

bool odd(int numToTest){

if (numToTest%2 == 0)

return false;

else

return true;

}

/* before() is used by selectionSort() to allow sorting by the following:

odd numbers come first (in increasing size) followed by the even

numbers (in increasing size).  Here is the general logic:

// if both are odd, return whether the left is smaller than the right

// if left is odd and right is even, left is before so return true

// if left is even and right is odd then left is not

// before right so return false

// otherwise, both must even, so return whether left is smaller than right

*/

bool before(int left, int right){

cout << "Still need to give the definition for before()!" << endl;

return false;  // hint: remove this line and the one above it!

} // end before()