Question

In: Computer Science

For your first project, write a C program (not a C++ program!)that will read in a...

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;
} 

Solutions

Expert Solution

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.


Related Solutions

write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run output Please input the number of nodes: 6 Please input the adjacency relation: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run (to make program output more clear, I have put it in boldface): Please input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT