// Kurt Feiereisel CSC5 Chapter 8, p.4, #10
/*******************************************************************************
*
* Sort Two Arrays
* _____________________________________________________________________________
* This program will display two arrays unsorted, then sort the two arrays.
* While the program is sorting the arrays it will display the elements of
* the arrays after each time an element is sorted.
* _____________________________________________________________________________
* INPUT:
* first[] : This array holds 8 integer values
* second[] : This array holds the same 8 integers as array first[]
*
* OUTPUT:
* first[] : Sorted first array
* second[] : Sorted second array
*
*
* ****************************************************************************/
#include <iostream>
using namespace std;
// Function Prototypes
void sortFirst(int first[], int SIZE);
void sortSecond(int second[], int SIZE);
void displayFirst(int first[], int SIZE);
void displaySecond(int second[], int SIZE);
int main()
{
// Initalize Arrays and Constants
const int SIZE = 8;
int first[SIZE] = {14, 24, 25, 75, 34, 23, 86, 12};
int second[SIZE] = {14, 24, 25, 75, 34, 23, 86, 12};
// Function Calls
displayFirst(first, SIZE);
sortFirst(first, SIZE);
displayFirst(first, SIZE);
displaySecond(second, SIZE);
sortSecond(second, SIZE);
displaySecond(second, SIZE);
return 0;
}
/*
* Definition of Function: displayFirst
* This function displays the array first[].
*/
void displayFirst(int f[], int size)
{
cout << "First Array" << endl;
// For loop to display each element of array
for(int index = 0; index < size; index++)
cout << f[index] << " ";
cout << endl;
}
/*
* Definition of Function: sortFirst
* This function put the array in ascending order by using a bubble sort.
* This function will display the array after each pass of the sort
*/
void sortFirst(int f[], int size)
{
// Declare Variables
bool swap;
int temp;
// Implement do-while loop
do
{
// Initalize swap to false
swap = false;
// Implement for loop to compare each element of the array
for (int count = 0; count < (size - 1); count++)
{
// If element count is greater than element count + 1, swap elements
if (f[count] > f[count + 1])
{
temp = f[count];
f[count] = f[count + 1];
f[count + 1] = temp;
swap = true;
// Display elements after swap
for(int index = 0; index < size; index++)
cout << f[index] << " ";
cout << endl;
}
}
// while swap = true
} while(swap);
}
/*
* Definition of Function: displaySecond
* This function displays the array second[].
*/
void displaySecond(int s[], int size)
{
// Display elements of second array
cout << "Second Array" << endl;
for(int index = 0; index < size; index++)
cout << s[index] << " ";
cout << endl;
}
/*
* Definition of Function: sortSecond
* This function will sort the the function second[] in ascending order
* using the bubble function. This function will display the elements of the
* array after each sort.
*/
void sortSecond(int s[], int size)
{
// Declare Local Variables
bool swap;
int temp;
// Implement do-while loops
do
{
// Initalize Local Variable
swap = false;
// For loop to compare element count and count + 1
for(int count = 0; count < (size - 1); count++)
{
// If element count is greater than element count + 1, then swap
if(s[count] > s[count + 1])
{
temp = s[count];
s[count] = s[count + 1];
s[count + 1] = temp;
swap = true;
for (int index = 0; index < size; index++)
cout << s[index] << " ";
cout << endl;
}
}
// while swap = true
} while(swap);
}