Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h> // WinApi header
#include <cstdio>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
// small helper method to display the before and after arrays
void displayArray (int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
// you'll see this pattern a lot for nicely formatting
// lists--check if we're past the first element, and
// if so, append a comma
if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}
void newSwitch (int array[], int size, int subtracting_this_many_array_elements, int *high, int *medium, int *low)
{
for ( int i = size; i-- > subtracting_this_many_array_elements; )
{
switch(array[i])
{
case 8:
case 7:
case 6:
cout << "Input: " << i << " too high\n";
*high += 1;
break;
case 5:
case 4:
case 3:
cout << "Input: " << i << " might be good\n";
*medium += 1;
break;
case 2:
case 1:
case 0:
cout << "Input: " << i << " is good\n";
*low += 1;
break;
}
}
}
int which_value_is_largest (int high, int medium, int low)
{
int value = 0;
if ( high > medium && high > low && medium > low )
{
value = 13;
}
else if ( high > medium && high > low && medium == low )
{
value = 12;
}
else if ( high > medium && high > low && low > medium )
{
value = 11;
}
else if ( medium > high && medium > low && high > low )
{
value = 10;
}
else if ( medium > high && medium > low && high == low )
{
value = 9;
}
else if ( medium > high && medium > low && low > high )
{
value = 8;
}
else if ( low > medium && low > high && high > medium )
{
value = 7;
}
else if ( low > medium && low > high && high == medium )
{
value = 6;
}
else if ( low > medium && low > high && medium > high )
{
value = 5;
}
else if ( medium == low && high < medium )
{
value = 4;
}
else if ( high == low && medium < high )
{
value = 3;
}
else if ( high == medium && low < medium )
{
value = 2;
}
else
{
value = 1;
}
return value;
}
int interpreting_whichValueIsLargest (int value)
{
switch(value)
{
case 13:
case 12:
case 11:
cout << "The inputs are mostly high counter value\n";
break;
case 10:
case 9:
case 8:
cout << "The inputs are mostly medium counter value\n";
break;
case 7:
case 6:
case 5:
cout << "The inputs are mostly low counter value\n";
break;
case 4:
cout << "The medium and low inputs are equal and higher than the high counter value\n";
break;
case 3:
cout << "The high and low inputs are equal and higher than the medium counter value\n";
break;
case 2:
cout << "The high and medium inputs are equal and higher than the low counter value\n";
break;
case 1:
cout << "The counter values are equal\n";
break;
}
}
int side (int array[], int size, int subtracting_this_many_array_elements)
{
int too_high = 0;
int might_be_good = 0;
int good_stuff = 0;
int value = 0;
string choice = "";
cout << "____________________________________\n";
cout << "Original array: ";
displayArray( array, size );
cout << '\n';
sort( array, size );
cout << "Sorted array: ";
displayArray( array, size );
cout << '\n';
cout << '\n';
newSwitch( array, size, subtracting_this_many_array_elements, &too_high, &might_be_good, &good_stuff );
cout << '\n';
cout << "high counter = " << too_high << '\n';
cout << "medium counter = " << might_be_good << '\n';
cout << "low counter = " << good_stuff << '\n';
cout << '\n';
value = which_value_is_largest (too_high, might_be_good, good_stuff);
interpreting_whichValueIsLargest(value);
cout << '\n';
return value;
}
int randomizing_array_elements ( int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
// keep the numbers small so they're easy to read
array[ i ] = rand() % 9;
}
}
int do_this_if_left_or_right_is_greater_else(int array[], int left_side, int right_side, int subtracting_this_many_array_elements)
{
int did_I_take_a_step = 0;
cout << "____________________________________\n";
cout << "Left side value is: " << left_side << '\n';
cout << "Right side value is: " << right_side << '\n';
cout << '\n';
if ( left_side > right_side )
{
for ( int i = 0; i < 8; i++ )
{
randomizing_array_elements(array, 9);
subtracting_this_many_array_elements++;
left_side = side( array, 9, subtracting_this_many_array_elements );
cout << "Left side value is: " << left_side << '\n';
if (left_side <= 5)
{
cout << "____________________________________\n";
cout << "Took a step\n";
did_I_take_a_step++;
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
break;
}
cout << '\n';
}
}
else if ( right_side > left_side )
{
for ( int i = 0; i < 8; i++ )
{
randomizing_array_elements(array, 9);
subtracting_this_many_array_elements++;
right_side = side( array, 9, subtracting_this_many_array_elements );
cout << "Right side value is: " << right_side << '\n';
if (right_side <= 5)
{
cout << "____________________________________\n";
cout << "Took a step\n";
did_I_take_a_step++;
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
break;
}
cout << '\n';
}
}
else
{
randomizing_array_elements(array, 7);
left_side = side( array, 9, subtracting_this_many_array_elements );
randomizing_array_elements(array, 9);
right_side = side( array, 9, subtracting_this_many_array_elements );
cout << "Left side value is: " << left_side << '\n';
cout << "Right side value is: " << right_side << '\n';
cout << '\n';
}
return did_I_take_a_step;
}
int testing_four_directions()
{
int array[ 9 ];
int left_side = 0;
int right_side = 0;
int step_counter = 0;
int did_I_take_a_step = 0;
int subtracting_this_many_array_elements = 0;
srand( time( NULL ) );
for ( int i = 0; i < 9; i++ )
{
randomizing_array_elements(array, 9);
left_side = side( array, 9 , subtracting_this_many_array_elements);
randomizing_array_elements(array, 9);
right_side = side( array, 9, subtracting_this_many_array_elements );
step_counter = do_this_if_left_or_right_is_greater_else(array, left_side, right_side, subtracting_this_many_array_elements);
did_I_take_a_step = did_I_take_a_step + step_counter;
}
return did_I_take_a_step;
}
int main ()
{
int north1 = 0;
int south1 = 0;
int east1 = 0;
int west1 = 0;
int north2 = 0;
int south2 = 0;
int east2 = 0;
int west2 = 0;
north1 = testing_four_directions();
south1 = testing_four_directions();
east1 = testing_four_directions();
west1 = testing_four_directions();
north2 = testing_four_directions();
south2 = testing_four_directions();
east2 = testing_four_directions();
west2 = testing_four_directions();
cout << "I tried to take 9 steps in the north direction, but I actual took " << north1 << " steps.\n";
cout << "I tried to take 9 steps in the south direction, but I actual took " << south1 << " steps.\n";
cout << "I tried to take 9 steps in the east direction, but I actual took " << east1 << " steps.\n";
cout << "I tried to take 9 steps in the west direction, but I actual took " << west1 << " steps.\n";
cout << '\n';
if ( north1 < south1 && north1 < east1 && north1 < west1 )
{
cout << "North being the lowest value is blocked to current feeler, \nstep 1.) change feeler in that direction.\nstep 2.) feel again in that direction\n";
}
else if ( south1 < north1 && south1 < east1 && south1 < west1 )
{
cout << "South being the lowest value is blocked to current feeler, \nstep 1.) change feeler in that direction.\nstep 2.) feel again in that direction\n";
}
else if ( east1 < north1 && east1 < south1 && east1 < west1 )
{
cout << "East being the lowest value is blocked to current feeler, \nstep 1.) change feeler in that direction.\nstep 2.) feel again in that direction\n";
}
else if ( west1 < north1 && west1 < south1 && west1 < east1 )
{
cout << "South being the lowest value is blocked to current feeler, \nstep 1.) change feeler in that direction.\nstep 2.) feel again in that direction\n";
}
else
{
cout << "The directions with the lower numbers are blocked to current feeler, \nstep 1.) change feeler in those directions.\nstep 2.) feel again in that direction\n";
}
cout << '\n';
cout << "Values with new feeler\n";
cout << "north value " << north2 << "\n";
cout << "south value " << south2 << "\n";
cout << "east value " << east2 << "\n";
cout << "west value " << west2 << "\n";
cout << '\n';
cout << "If the direction with the lowest value is still the lowest value\n\n- go in a different direction\nflow in the direction with the largest value, like water\n" <<
"- else keep going in that direction, the new feeler opened something up.\n";
getchar();
}
I'm reading the c++ book on this website and haven't gotten all the way through yet, so I'm not quite sure if my code is viral or not.