Homework 28 Solution

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.

2.      A short (no more than one paragraph) discussion of the purpose of the following two lines that appear near the bottom of main. If you are unsure, see pages 486 and 492 of the course text.

delete [] theNums;      // releases dynamically allocated memory to prevent memory leak

theNums = NULL;     // set ptr to NULL for safety in case someone tries to use it after what it points to is deallocated

Solution

// 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;  // releases dynamically allocated memory

theNums = NULL;     // set ptr to NULL for safety

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)

*/

bool before(int left,int right){

// if both are odd return whether the a is smaller than the b

if (odd(left) && odd(right))

return left<=right;

// if the a is odd and the b even, a is before so return true

else if(odd(left))

return true;

// if the a is even and the b is odd then the a is not

// before the b so return false

else if(odd(right))

return false;

// Both are even so return whether a is smaller than b

else

return left<right;

}