// ConsoleApplication2.cpp : This file contains the ‘main’ function. Program execution begins and ends there.
//
#include “pch.h”
#include
#include //to be used for malloc
void arrayCopy(int fromArray[], int toArray[], int size);
void sort(int arr[], int size);
int linSearch(int arr[], int size, int target, int* numComparisons);
int binSearch(int arr[], int size, int target, int* numComparisons);
void displayArray(int arr[], int size);
int const exitVal = -999; //terminating value for comparisons
int main(void) {
int userInput; //for user input
int size = 100; //size to grow dynamically
int counter = 0; //counter
int *Arr1 = (int *)malloc(sizeof(int) * size); // array 1
//prompt for user input
printf(“Enter values into array.
Enter “-999″ when finished.n”);
//loop to enter userInput and put vals into Arr1
while (userInput != exitVal) { //stops when user input is -999
scanf(“%d”, &userInput); //accept user input
if (counter >= size) { //checks if the array is too small
int i; //varible for the for-loop
int *temp = (int *)malloc(sizeof(int) * size * 2); //make the array larger
for (i = 0; i < size; i++) //copys the values to the Arr1
temp[i] = Arr1[i];
free(Arr1); //frees Arr1
Arr1 = temp;
size = size * 2; //makes the array bigger for new values
}
Arr1[counter] = userInput; //adds values to array
counter++; //increment counter
}
int *dArr2 = (int *)malloc(sizeof(int) * size); // array 2
//copy values from dArr1 to dArr2
arrayCopy(Arr1, dArr2, counter – 1);
//sort dArr1
sort(Arr1, counter – 1);
printf(“nSORTED ARRAYn”);
displayArray(Arr1, counter – 1);
printf(“UNSORTED ARRAYn”);
displayArray(dArr2, counter – 1);
userInput = 0; //reset value for use with next loop
//prompt for user input for values to search for
printf(“nEnter values to search for in array.
Enter “-999″ when finished.”);
//loop to search for values
while (userInput != exitVal) {
int positionLin = 0, comparisonsLin = 0; //variables for linear search
int positionBin = 0, comparisonsBin = 0; //variables for binary search
scanf(“%d”, &userInput); //accept user input
if (userInput == exitVal) { //check to see if user input is -999
printf(“PROGRAM FINISHED…nn”);
break;
}
positionLin = linSearch(dArr2, counter – 1, userInput, &comparisonsLin); //search for value using linear search
positionBin = binSearch(Arr1, counter – 1, userInput, &comparisonsBin); //search for value using binary search
if (positionLin == -1 && positionBin == -1) {
printf(“n%d not found in array. Done in %d comparisons using LINEAR SEARCHn”, userInput, comparisonsLin);
printf(“%d not found in array. Done in %d comparisons using BINARY SEARCHnn”, userInput, comparisonsBin);
//reset values for next search
comparisonsLin = 0;
positionLin = 0;
comparisonsBin = 0;
positionBin = 0;
}
else {
printf(“nLINEAR SEARCH: %d found at index %d. Done in %d comparisons.n”, userInput, positionLin, comparisonsLin);
printf(“BINARY SEARCH: %d found at index %d. Done in %d comparisons.nn”, userInput, positionBin, comparisonsBin);
//reset values for next search
comparisonsLin = 0;
positionLin = 0;
comparisonsBin = 0;
positionBin = 0;
}
}
return 0;
}//end main()
/***FUNCTION DEFINITIONS***/
//function to copy values from one array to another
void arrayCopy(int fromArray[], int toArray[], int size) {
int i;
for (i = 0; i < size; i++)
toArray[i] = fromArray[i];
}//end arrayCopy(…)
//function to sort array in ascending order (SELECTION SORT)
void sort(int arr[], int size) {
int i, j, position, swap;
for (i = 0; i < (size – 1); i++) {
position = i;
for (j = i + 1; j < size; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
}//end sort(…)
//function to search for target value in linear fashion
int linSearch(int arr[], int size, int target, int* numComps) {
int i;
for (i = 0; i < size; i++) {
*numComps += 1;
//printf(“Comparing %d with %dn”, target, arr[i]); //FOR DEBUGGING
if (target == arr[i])
break;
}
if (i == size)
return -1;
else
return i;
}//end linSearch(…)
//function to search for target value in binary fashion
int binSearch(int arr[], int size, int target, int* numComps) {
int bottom = 0; //to represent smallest number in array
int top = size – 1; //to represent largest number in array
int middle = (bottom + top) / 2; //to represent middle value in array
while (bottom top)
return -1;
}//end binSearch(…)
//FOR DEBUGGING; function to display array
void displayArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf(“%d. %dn”, i, arr[i]);
printf(“n”);
}//end displayArray(…)