Skip to content

Commit

Permalink
added and modified programming challenges in chapter 8
Browse files Browse the repository at this point in the history
  • Loading branch information
jesushilarioh committed Jul 22, 2020
1 parent 99c9b07 commit 892e344
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ void bubbleSort(int array[], int size)
index;

for (maxElement = size - 1; maxElement > 0; maxElement--)
{
// {
// for (int i = 0; i < size; i++)
// {
// cout << array[i] << " ";
// }
// cout << endl;

for (index = 0; index < maxElement; index++)
{
if (array[index] > array[index + 1])
{
swap(array[index], array[index + 1]);

}

}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
using namespace std;

int inputValidation(int);
void selectionSort(int [], const int);
bool binarySearch(const int[], const int, const int);
bool binarySearch(const int[],
const int,
const int);
void selectionSort(int[], int);
void swap(int &, int &);

int main()
{
const int NUMBER_OF_ACCOUNTS = 18;

int account_numbers[NUMBER_OF_ACCOUNTS] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002 };
int account_numbers[NUMBER_OF_ACCOUNTS] = { 5658845, 4520125, 7895122,
8777541, 8451277, 1302850,
8080152, 4562555, 5552012,
5050552, 7825877, 1250255,
1005231, 6545231, 3852085,
7576651, 7881200, 4581002 };

cout << "Enter an account #: " << endl;
int user_account_number = inputValidation(user_account_number);

// for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++)
// cout << account_numbers[i] << ", ";
// cout << endl;

selectionSort(account_numbers, NUMBER_OF_ACCOUNTS);

// for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++)
// cout << account_numbers[i] << ", ";
// cout << endl;

bool user_account_number_is_valid = binarySearch(account_numbers, NUMBER_OF_ACCOUNTS, user_account_number);
bool user_account_number_is_valid = binarySearch(account_numbers,
NUMBER_OF_ACCOUNTS,
user_account_number);

if (user_account_number_is_valid)
cout << "Your account number is valid." << endl;
Expand All @@ -56,19 +56,19 @@ int inputValidation(int user_number)
return user_number;
}

void selectionSort(int array[], const int ARRAY_SIZE)
void selectionSort(int array[], int ARRAY_SIZE)
{
int min_index,
min_value;

for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++)
for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++) // 0 - 17
{
min_index = start_index;
min_value = array[start_index];

for (int index = start_index + 1; index < ARRAY_SIZE; index ++)
for (int index = start_index + 1; index < ARRAY_SIZE; index++) // 1 - 18
{
if (array[index] < min_value)
if (array[index] < min_value) // Ascending
{
min_value = array[index];
min_index = index;
Expand All @@ -78,33 +78,33 @@ void selectionSort(int array[], const int ARRAY_SIZE)
}
}

void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

bool binarySearch(const int array[], const int ARRAY_SIZE, const int user_number)
{

int first_index = 0,
middle_index,
last_index = ARRAY_SIZE - 1;
last_index = ARRAY_SIZE - 1; // 18 - 1 = 17

bool found = false;

while (!found && first_index <= last_index)
{
middle_index = (first_index + last_index) / 2;
middle_index = (first_index + last_index) / 2; // 8

if (array[middle_index] == user_number)
if (array[middle_index] == user_number) // Mid point
found = true;
else if (array[middle_index] > user_number)
last_index = middle_index - 1;
else
else if (array[middle_index] > user_number) // Lower half
last_index = middle_index - 1; // 8 - 1 = 7
else // Upper Half
first_index = middle_index + 1;

}

return found;
}
}

void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,15 @@ int main()
{
double rainfall[ARRAY_SIZE];
int months[ARRAY_SIZE] = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12 };
string month_names[ARRAY_SIZE] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
7, 8, 9, 10, 11, 12 };
string month_names[ARRAY_SIZE] = { "January", "February", "March",
"April" , "May" , "June",
"July" , "August" , "September",
"October", "November", "December" };

cout << "Enter average rainfall for each month: " << endl;
getValues(rainfall);

selectionSort(rainfall, months, ARRAY_SIZE);

cout << endl;
for (int i = 0; i < ARRAY_SIZE; i++)
cout << month_names[months[i] - 1] << "("
<< months[i] << ")" << ": "
<< rainfall[i]
<< endl;
cout << endl;

double total_rainfall = calculateTotal(rainfall);
double avarage_rainfall = average(total_rainfall);

Expand All @@ -57,6 +49,16 @@ int main()
vector<int>lowest_months;
findLowestMonths(rainfall, lowest_months);

selectionSort(rainfall, months, ARRAY_SIZE);

cout << endl;
for (int i = 0; i < ARRAY_SIZE; i++)
cout << month_names[months[i] - 1] << "("
<< months[i] << ")" << ": "
<< rainfall[i]
<< endl;
cout << endl;

display(total_rainfall, avarage_rainfall, highest_months, lowest_months);

return 0;
Expand Down Expand Up @@ -229,4 +231,4 @@ void swap(int &a, int &b)
int temp = a;
a = b;
b = temp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ using namespace std;

void displayArray(int[], int, string);
void bubbleSort(int[], int, int &);
void selectionSort(int[], int, int &);
void swap(int &, int &);
void selectionSort(int[], int, int &);

int main()
{
// 1. two identical arrays of at least 20 integers.
const int ARRAY_SIZE = 20;

int array_1[ARRAY_SIZE] = {6, 5, 90, 33, 45,
Expand All @@ -35,75 +34,63 @@ int main()
55, 60, 2, 9, 11,
91, 65, 45, 8, 3};

// 2. call a function that uses the bubble sort
// algorithm to sort one of the arrays in ascending order.
// The function should keep a count of the number of
// exchanges it makes.
int number_of_exchanges = 0;
displayArray(array_1, ARRAY_SIZE, "array_1 UNSORTED:\n----------------");
displayArray(array_1, ARRAY_SIZE, "UNSORTED-------------------|");
bubbleSort(array_1, ARRAY_SIZE, number_of_exchanges);
displayArray(array_1, ARRAY_SIZE, "\narray_1 SORTED:\n----------------");
displayArray(array_1, ARRAY_SIZE, "SORTED-------------------|");
cout << "\nNumber of exchanges = " << number_of_exchanges << endl;

// 3. call a function that uses the selection sort
// algorithm to sort the other array.
// It should also keep count of the number of
// exchanges it makes.
number_of_exchanges = 0;
displayArray(array_2, ARRAY_SIZE, "\narray_2 SORTED:\n-----------------");
displayArray(array_2, ARRAY_SIZE, "UNSORTED-------------------|");
selectionSort(array_2, ARRAY_SIZE, number_of_exchanges);
displayArray(array_2, ARRAY_SIZE, "\narray_2 SORTED:\n-----------------");
displayArray(array_2, ARRAY_SIZE, "SORTED-------------------|");
cout << "\nNumber of exchanges = " << number_of_exchanges << endl << endl;

// 4. Display these values on the screen.
return 0;
}

void displayArray(int array[], int array_size, string prompt)
void displayArray(int array[], int ARRAY_SIZE, string prompt)
{
cout << prompt << endl;
for (int i = 0; i < array_size; i++)
for (int i = 0; i < ARRAY_SIZE; i++)
cout << array[i] << ", ";
cout << endl;
cout << endl;
}

void bubbleSort(int array[], int array_size, int &number_of_exchanges)
void bubbleSort(int array[], int ARRAY_SIZE, int &number_of_exchanges)
{
int max_element,
index;

for (max_element = array_size - 1; max_element > 0; max_element--)
// <---------
for (max_element = ARRAY_SIZE - 1; max_element > 0; max_element--)
{
// --------->
for (index = 0; index < max_element; index++)
{
// array[0] > array[1];
if (array[index] > array[index + 1])
{
swap(array[index], array[index + 1]);
number_of_exchanges++;
}

}
}
}
}

void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

void selectionSort(int array[], int array_size, int &number_of_exchanges)
void selectionSort(int array[], int ARRAY_SIZE, int &number_of_exchanges)
{
int min_index,
min_value;

for (int start_index = 0; start_index < (array_size - 1); start_index++)
// 0 -> 18
for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++)
{
min_index = start_index;
min_value = array[start_index];

for (int index = start_index + 1; index < array_size; index++)
// 1 -> 19
// array[0] array[1]
for (int index = start_index + 1; index < ARRAY_SIZE; index++)
{
if (array[index] < min_value)
{
Expand All @@ -114,4 +101,12 @@ void selectionSort(int array[], int array_size, int &number_of_exchanges)
}
swap(array[min_index], array[start_index]);
}

}

void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Loading

0 comments on commit 892e344

Please sign in to comment.