Question

In: Computer Science

C language only please and please make a simple code Write a function that will find...

C language only please

and please make a simple code

Write a 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 thearray, 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.

Solutions

Expert Solution

#include<stdio.h>

int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2)
{
       *index1 = 0;
       *index2 = size-1;
      
       while((*index1)<(*index2))
       {
           int sum = arr[*index1]+arr[*index2];
           if(sum==target)//means such pair found
           return 1;
           else if(sum<target)
           (*index1) = (*index1 )+ 1;
           else
           (*index2) = (*index2 )-1;  
       }
       return 0;//if not found
}
int main()
{
   int a[] ={1,2,3,4,5,6,7,8,9};
   int t1 = 9;
   int t2=22;
   int l,r;
   if(TwoSumFunction(a,9,t1,&l,&r))//if found
   printf("Such pair found: %d,%d\n",a[l],a[r]);
   if(!TwoSumFunction(a,9,t2,&l,&r))//if not found
   printf("Not found\n");
  
   return 0;
}

output:

Such pair found: 1,8
Not found


Process exited normally.
Press any key to continue . . .


Related Solutions

PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only)....
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only). The function code should follow the conventions for MIPS function calls including passing parameters and returning results. Your function code must be written with the minimum number of machine instructions to be executed and without any use of MIPS pseudo-instructions. Myfunction(unsigned int a, unsigned int b, unsigned int c) { int i=0; while (a > c) { a /= b; i++; } return i;...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
** in C language please Write a function that remove a substring of str beginning at...
** in C language please Write a function that remove a substring of str beginning at index idx and length len. // param str: the string being altered // param idx: the starting index of the removed chunk // param len: the number of characters to remove (length of sub> // removed) assumptions: // 0 <= idx // str is a valid string that terminates properly // // TODO: complete the function void remove_substr(char str[], int idx, int len) {
Please solve using simple python programming language and make it easy to understand explain your code...
Please solve using simple python programming language and make it easy to understand explain your code as I am a beginner, use appropriate variable names which make the code easy to understand and edit if needed. A subsystem responsible for delivering priority numbers to an automated irrigation system has stopped working and you need to deliver a quick fix that will work until the actual subsystem is fixed by senior developer. As you are the newest addition to the development...
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT