ConsoleApplication2cpp This file contains the ‘main’ function Essay

// ConsoleApplication2.cpp : This file contains the ‘main’ function. Program execution begins and ends there.

//

Don't use plagiarized sources. Get Your Custom Essay on
ConsoleApplication2cpp This file contains the ‘main’ function Essay
From as low as $9/Page
Order Essay

#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(…)

How to place an order?

Take a few steps to place an order on our site:

  • Fill out the form and state the deadline.
  • Calculate the price of your order and pay for it with your credit card.
  • When the order is placed, we select a suitable writer to complete it based on your requirements.
  • Stay in contact with the writer and discuss vital details of research.
  • Download a preview of the research paper. Satisfied with the outcome? Press “Approve.”

Feel secure when using our service

It's important for every customer to feel safe. Thus, at The Homework Writings, we take care of your security.

Financial security You can safely pay for your order using secure payment systems.
Personal security Any personal information about our customers is private. No other person can get access to it.
Academic security To deliver no-plagiarism samples, we use a specially-designed software to check every finished paper.
Web security This website is protected from illegal breaks. We constantly update our privacy management.

Get assistance with placing your order. Clarify any questions about our services. Contact our support team. They are available 24\7.

Still thinking about where to hire experienced authors and how to boost your grades? Place your order on our website and get help with any paper you need. We’ll meet your expectations.

Order now Get a quote