C++ Program

// Purpose : Using a function that finds the component in a one-dim array
// that is closest in value to the first component.

// -----------------------------------------------------------------------------

#include <iostream.h>


const int max = 9; // global variable for max

int less[9], great[9]; // creating two arrays

int low, low1, pass, pass1; // low and low1 will keep the lowest integers
// and pass will keep track of times looped through



void closest ( int num, int eye, int first)

//------------------------------------------------------------------------------
//Preconditions : num is actual number entered by the user, eye is the number
// of the array, and first is the first number entered by the
// user.
//Postconditions : The number closest to the first number will be displayed.
//------------------------------------------------------------------------------

int temp,j;

if (num < first )

less[eye] = num;

if (num > first )

great[eye] = num;


//------------------------------------------------------------------------------
//beginning of loop for numbers that do not equal the first number and are
//less than the first number. Will store the subtraction of first and
//num(number entered by user) and store it in variable low and then store num
//in less[0].
//------------------------------------------------------------------------------

if ((first != num) && (num < first))


for(j=eye; j < eye+1 ; j++)

temp = first - less[j]; // subtract first and less[j] to get temp.


// if this is the first pass through this loop store values of temp and num.
if ((j == 1)|| (pass == 0))

low = temp;

less[0] = num;
}

// if temp is less than low, store the new number in low and the actual number
// in less[0].

if (temp < low)


low = temp;

less[0] = num;

} // end of if loop

} // end of For loop

++pass; //increment pass by one

} // end of if loop

//------------------------------------------------------------------------------
//beginning of loop for numbers that do not equal the first number and are
//greater than the first number. Will store the subtraction of num(number
//entered by user)and of first and store it in variable low1 and then store
//num in great[0].
//------------------------------------------------------------------------------

if ((first != num) && (num > first))


for(j=eye; j < eye+1 ; j++)

temp = great[j] - first; // subtract great[j] and first to get temp.


if ((j == 1) || (pass1 == 0))

// if this is the first pass through this loop store values of temp and num.

low1 = temp;

great[0]= num;
}
// if temp is less than low, store the new number in low1 and the actual
//number in great[0].

if (temp < low1)

low1 = temp;

great[0] = num;

} // end of if loop

} // end of For loop

++pass1; // increment pass1 by one

} // end of if loop


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

if (eye == max-1) // if eye reaches the end of the array display results

cout <<endl;

cout<<"-------------------------------------------------------------"<<endl;

if ((low1 != 0) && (low == 0))

cout << " The closest number to "<< first<<" was "<< great[0]<< endl;


if ((low1 < low) && (low != 0)&& (low1 != 0))

cout << " The closest number to "<< first<<" was "<< great[0]<< endl;


if ((low != 0) && (low1 == 0))

cout << " The closest number to "<< first<<" was "<< less[0]<< endl;


if ((low1 > low) && (low1 != 0) && (low != 0))

cout << " The closest number to "<< first<<" was "<< less[0]<< endl;


//if a number greater than the first number is the same distance to a number
//that is smaller than the first number display both.

if((low1 == low) && (low1 !=0) && (low != 0))

cout <<" The closest numbers to "<< first<<" were " << great[0]

<<" and "<< less[0]<< endl;

cout <<"------------------------------------------------------------"<<endl;

cout <<endl;

cout <<endl;

cout<<"Bye Bye and thank you for playing my game."<<endl;

cout <<endl;

} // end of if loop



return ;

} // end of Function closest

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

int main ()


int list[max]; // initialize an array

int i, f;

pass = 0;

pass1 = 0;

cout <<"Welcome, Welcome to my game!"<<endl;

cout <<endl;

cout <<"This program while miraculously find the closest number to"<<endl;

cout <<"your first number!!!!"<<endl;

cout <<endl;

cout <<"Ok here we go!"<<endl;

cout <<endl;

cout <<"You will be asked to enter NINE numbers."<<endl;

cout <<endl;

for (i = 0; i < max; ++i) //loop through to get numbers from user


cout << "Please enter number[" <<i+1<<"]"<< endl;

cin >> list[i];

cout<<endl;

f = list[0];

closest( list[i],i, f); // pass parameters to Function closest

} // end of For loop


return 0;
}