Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program (not a C++ program!) that will contain the following: Write a function that will make a copy of the values from

Write a C program (not a C++ program!) that will contain the following:

Write a function that will make a copy of the values from one array to another array. Suggested prototype:

void arrayCopy (int fromArray[], int toArray[], int size);

Write your own function that will sort an array in ascending order. You may use whatever sorting algorithm you wish. Suggested prototype:

void myFavorateSort (int arr[], int size);

Write your own function that will perform a linear search on the unsorted array. The function is to return two values. The first should be the position in the array where the value was found or -1 if the value was not found. The second is the number of comparisons needed to determine if/where the value is located in the array. Suggested prototype:

int linSearch (int arr[], int size, int target, int* numComparisons);

Write your own function that will perform a binary search on the sorted array. The function is to return two values. The first should be the position in the array where the value was found or -1 if the value was not found. The second is the number of comparisons needed to determine if/where the value is located in the array. Suggested prototype:

int binSearch (int arr[], int size, int target, int* numComparisons);

Inside of main:

read in integer input from standard input and store these values into an array. The values will have a terminal value of -999. So you read in these values in a loop that stops when the value of -999 is read in. The use of informative prompts is required for full credit. You may not assume how many numeric values are given on each line. The use of a scanf() with the following form is expected to read in the values:

scanf (%d, &val);

make a copy of the integer array using the arrayCopy() function described above

sort one of the arrays (using the sort() function described above), leave the other array unsorted

Inside of main (continued):

read in integer input from standard input (again, the use of scanf() is expected) and for each of the values read in search for the value using both search functions described above (i.e. for each value read in, perform a linear search on the unsorted array and perform a binary search on the sorted array). Using the information returned/sent back from the search functions, print out from main():

1. Whether the value was found or not found,

2. The number of comparisons needed to determine whether the value exists or not in each algorithm,

3. And the location in the array where the number is located (if the value does exist in the array).

The above information MAY NOT be printed out in the linear search or binary search functions. Those functions MUST RETURN/SEND BACK the information to be printed by the main function.

Repeat reading in integer values and searching the arrays until the terminal value of -999 is read in. The use of informative prompts AND descriptive result output is required for full credit. Again, scanf() is expected to read the input.

You may not assume the input will be less than any set size. Thus you will need to dynamically allocated space for the array.

Dynamic Array Allocation allows the space in an array to change during the course of the execution of a program. In C, this requires the use of the malloc() function. To dynamically allocate space for 100 integers, the malloc() code would be as follows:

int *darr;

int size = 100;

darr = (int *) malloc (size * sizeof(int) );

This array can only hold 100 integers and it not really dynamic. To make it truly dynamic, we need to grow the array when we try to put more values into it than can be held by its current size. The following code will double the size of the array.

int *temp;

temp = (int *) malloc ( size * 2 * sizeof(int) );

int i;

for ( i = 0 ; i < size ; i++) temp[i] = darr[i];

free (darr);

darr = temp;

size = size * 2; You may use these .txt files to test your program: https://www.cs.uic.edu/pub/CS211/ProjectS16/proj1data1.txt https://www.cs.uic.edu/pub/CS211/ProjectS16/proj1data2.txt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

=+ Who is the negotiation partner at company level?

Answered: 1 week ago

Question

=+Which associations exist?

Answered: 1 week ago