In: Computer Science
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer.
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*index1, 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 the input. 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 array copy() 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 allocate 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 < allocated; i++)
darr[i] = temp[i];
free (temp);
allocated = allocated* 2;
Make sure your program runs properly when compiled using gcc on the bert.cs.uic.edu machine!(Do not use g++ or any other C++ compiler with this program.)
project base:
#include <stdio.h> int main (int argc, char** argv) { int val; /* prompt the user for input */ printf ("Enter in a list of numbers ito be stored in a dynamic array.\n"); printf ("End the list with the terminal value of -999\n"); /* loop until the user enters -999 */ scanf ("%d", &val); while (val != -999) { /* store the value into an array */ /* get next value */ scanf("%d", &val); } /* call function to make a copy of the array of values */ /* call function to sort one of the arrays */ /* loop until the user enters -999 */ printf ("Enter in a list of numbers to use for searching. \n"); printf ("End the list with a terminal value of -999\n"); scanf ("%d", &val); while (val != -999) { /* call function to perform target sum operation */ /* print out info about the target sum results */ /* get next value */ scanf("%d", &val); } printf ("Good bye\n"); return 0; }
Hi,Please find the solution and rate the answer. For every 5 items, I am reallocating the array. You will get nice idea so as to how to do it. Please go thr the program carefully. It is only a C program.
//
// main.c
// IntegerThing
//
// Created by Abhirama Gopala Dasa on 12/09/19.
// Copyright © 2019 Abhirama Gopala Dasa. All rights reserved.
//
#include <stdio.h>
void makeArrayCopy (int *fromArray, int *toArray, int size){
for (int i=0; i<size; i++) {
toArray[i]=fromArray[i];
}
}
struct indexes {
int index1;
int index2;
};
void printArray(int *arr,int size){
printf("\n");
for (int k=0; k<size; k++) {
printf("%d,",arr[k]);
}
printf("\n");
}
struct indexes targetSum(int target,int *arr,int size){
struct indexes x;
x.index1=-1;
x.index2=-1;
for (int i=0; i<size-1; i++) {
for (int k=i+1; k<size; k++) {
if(arr[i]+arr[k]==target){
x.index1 = i;
x.index2 = k;
return x;
}
}
}
return x;//x will contain -1 if target sum not found
}
void sort(int *arr,int size){
for (int i=0; i<size-1; i++) {
for (int k=i+1; k<size; k++) {
if(arr[i]>arr[k]){
int temp = arr[i];
arr[i]=arr[k];
arr[k]=temp;
}
}
}
}
int main(int argc, const char * argv[]) {
int val;
/* prompt the user for input */
printf ("Enter in a list of numbers ito be stored in a dynamic array.\n");
printf ("End the list with the terminal value of -999\n");
/* loop until the user enters -999 */
int *darr;
int allocated =5;int n=1;
darr = (int *)malloc(allocated*sizeof(int));
scanf ("%d", &val);
int i=0;
while (val != -999)
{
/* store the value into an array */
darr[i++]=val;
if(i%5==0){
int *temp = (int*)malloc(allocated* ++n *sizeof(int));//reallocation
for (int m=0; m<allocated*(n-1); m++) {
temp[m] = darr[m];
}
darr = temp;
}
printf("Enter another value");
/* get next value */
scanf("%d", &val);
}
// int finalSize = allocated*n;
int arr[i];
printArray(darr, i);
/* call function to make a copy of the array of values */
makeArrayCopy(darr, arr, i);
printArray(arr, i);
/* call function to sort one of the arrays */
sort(arr,i);
printArray(arr, i);
/* loop until the user enters -999 */
printf("Enter the target Integer:");
scanf ("%d", &val);
//I am using the previous list only for target sum.
while (val != -999)
{
/* call function to perform target sum operation */
struct indexes x = targetSum(val, arr, i);
/* print out info about the target sum results */
if(x.index2==-1 && x.index1 ==-1){
printf("Could not find the target:");
}else{
printf("Found at %d and %d index,",x.index1,x.index2);
}
printf("Enter value for trying again or -999 to quit\n");
/* get next value */
scanf("%d", &val);
}
printArray(arr, i);
printf ("\nGood bye\n");
return 0;
}
Output:Everything is working great. Extract what and how you want.