Question
wrote the code for this reason and I cant seems to get around the errors, Please fix my code. #include #include //Function one for copying
wrote the code for this reason and I cant seems to get around the errors,
Please fix my code.
#include
#include
//Function one for copying array
void arrayCopy(int fromArray[], int toArray, int size)
{
for (int i = 0; i
{
toArray[i] = fromArray[i];
}
for ( int j = 0; j
{
printf(" Copy Array: %d," toArray[j]);
}
printf(" ");
}
//function for sorting Array
void myFavoriteSort(int ar[], int size)
{
int i, j;
int temp, x;
for ( i = 0; i
{
for (j = 0 ; j
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[ j + 1] = temp;
}
}
}
for ( x = 0; x
{
printf(" myFavorite Sorted Array: %d", arr[x]);
}
printf(" ");
}
int TwoSumFunction(int arr[], int size, int target, int* index1, int* index2) //two sum fucntin to calculate the sum
{
*index1 = 0;
*index2 = size -1;
while ( (*index1)
{
int sum = arr[*index1]+arr[*index2];
if (sum== target)
return 1;
else if (sum
(*index1)=(*index1)+1;
else (*index2)=(*index2)-1;
}
return 0; // if
}
int main (int argc, char** argv)
{
int val, *darr;
int size = 100;
darr =(int *)malloc(size * sizeof(int));
int val = 0;
int pos = 0;
/* prompt the user for input */
printf ("Enter in a list of numbers ito be stored in a dynamic array. ");
printf ("End the list with the terminal value of -999 ");
/* loop until the user enters -999 */
scanf ("%d", &val);
/* store the value into an array */
while ( val != -999)
{
if ( pos >= size)
{
//create temporary array to allocated double memory
int *temp;
temp = (int *)malloc(size * 2 * sizeof(int));
int i;
//filling original array
for ( i = 0; i
{
temp[i] = darr[i];
}
free(darr);
//assigning double memory to original array
darr = temp;
size = size * 2;
}
darr[pos] = val;
pos++;
//scan get the next value
scanf("%d", &val);
}
int size = pos;
int toArray[size];
int index1;
int index2;
int target;
/* call function to make a copy of the array of values */
arrayCopy(darr, toArray, size);
/* call function to sort one of the arrays */
myFavoriteSort(darr, size);
/* loop until the user enters -999 */
printf ("Enter in a list of numbers to use for searching. ");
printf ("End the list with a terminal value of -999 ");
scanf ("%d", &val);
while (val != -999)
{
/* call function to perform target sum operation */
if (TwoSumFunction( toArray, size, target,&indext1, &indext2))
/* print out info about the target sum results */
printf(" Pair exist: %d", &target);
else if (!TwoSumFunction(darr, size, target, &indext1, &indext2))
printf("Doesnt Exist ");
/* get next value */
scanf("%d", &val);
}
printf ("Good bye ");
return 0;
}
Example: List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! Your C program will contain the following: Write a function that will make a copy of the values from one array to another array. Suggested prototype: void makeArrayCopy (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 myFavoriteSort (int arr[], int size); Write your own function that will find whether there exist two integers that sum to the target integer. The function is to "return" three values. First, return 1 if the integers were found, return"-1" if your search was not successful. If you find two integers which add up to the target value, you should return their respective index position inside the array Suggested prototype: int TwoSumFunction (int arr[], int size, int target, int* indexl, int* index2); Inside TwoSumFunction: Pass the sorted array to the function. Set two pointers at the beginning and the end of the array, then start moving the pointers inward while checking their sum. If it's exactly the target, then we are done, and you can return 1. If it exceeds the target" value, then any sum using the larger element is too large, so move the pointer corresponding to that element inward. If the sum is less than target" value, then any sum using the lower element is too small, so move the pointer corresponding to that element inwards. If you are done with scanning the array and cannot find any two elements that sum up to "target" value, return -1. Inside of main: Read in integer input from standard input and store these values into a dynamic array. This array is to grow in size if the array is full. 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, nor the total number of values contained in make a copy of the integer array using the arrayCopy(function described above sort the copy array (using the myFavoriteSort() function described above) read in integer input from standard input (again, the use of scanf() is expected) and for each of the values read in perform the TwoSum evaluation. Using the information returned sent back from the search functions, print out from main(): 1. The target value, 2. Whether the Two Sum evaluation was successful or not 3. Locations of the elements in the array which make up the sum. The above information MAY NOT be printed out in TwoSum Function. The function MUST RETURN/SEND BACK the information to be printed by the main function. Not doing this will SEVERELY LOWER your score on this project. Repeat reading in integer values and searching the array 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 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 allocated = 100; darr = (int *) malloc (allocated * sizeof(int)); This array can only hold 100 integers and is 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 = darr; darr = (int *) malloc (allocated * 2 * sizeof(int)); int i; for (i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started