Question

In: Computer Science

In C language Write a program that includes a function search() that finds the index of...

In C language

Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays the output. Name program part1.c

int search(int a[], int n, int value);

Example 1:

Enter length of array: 4

Enter elements of array: 8 2 3 9

Enter the value for searching: 2

Output: 1

Example 2:

Enter the length of the array: 6

Enter the elements of the array: 4 6 1 0 2 9

Enter the value for searching: 5

Output: -1

2. Modify the part 1 program so that it deletes all instances of the value from the array. As part of the solution, write and call the function delete() with the following prototype. n is the size of the array. The function returns the new size of the array after deletion (The array after deletion will be the same size as before but the actual elements are the elements from index 0 to new_size-1). In writing function delete(), you may include calls to function search(). The main function takes input, calls the delete()function, and displays the output. Name your program part2.c.

int delete(int a[], int n, int value);

Example 1:

Enter the length of the array: 6

Enter the elements of the array: 4 3 1 0 3 9

Enter the value for deleting: 3

Output array:

4 1 0 9

Solutions

Expert Solution

ANSWER:-

QUESTION (1):-

#include <stdio.h>
int search(int arr[],int n,int value)
{
   for(int i=0;i<n;i++)
   {
       int index=-1;
       if(arr[i]==value)
       {
           index=i;
           return index;
       }
   }
   return -1;
}
int main(void) {
   int n,num;
   printf("Enter length of array : ");
   scanf("%d",&n);
   int arr[n];
   printf("Enter elements of array : ");
   for(int i=0;i<n;i++)
   scanf("%d",&arr[i]);
   printf("Enter the value for searching : ");
   scanf("%d",&num);
   int index=search(arr,n,num);
   if(index>=0)
   printf("index value is %d",index);
   else
   printf("%d",index);
   return 0;
}

// OUTPUT:

QUESTION (2):-

#include <stdio.h>
int search(int arr,int value)
{
if(arr==value)
return 1;
else
return 0;
}
int delete(int arr[],int n,int value)
{
for(int i=0;i<n;i++)
{
int k=search(arr[i],value);
if(k==1)
{
for(int j=i;j<n;j++)
arr[j]=arr[j+1];
n--;
}
}
printf("Modified array : ");
for(int i=0;i<n;i++)
printf("%d ",arr[i]);
}
int main(void) {
int n,num;
printf("Enter length of array : ");
scanf("%d",&n);
int arr[n];
printf("Enter elements of array : ");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter the value for searching : ");
scanf("%d",&num);
delete(arr,n,num);
return 0;
}

OUTPUT:-

Success #stdin #stdout Os 4496KB Enter length of array : 4 Enter elements of array : 8 2 3 9 Enter the value for searching : 2 index value is 1

Success #stdin #stdout Os 4368KB Enter length of array : 6 Enter elements of array : 4 3 1 0 3 9 Enter the value for searching : 3 4109

Success #stdin #stdout Os 4496KB Enter length of array 4 Enter elements of array 8 2 3 9 Enter the value for searching 2 index value is 1

We were unable to transcribe this image


Related Solutions

1. Write a program that includes a function search() that finds the index of the first...
1. Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. Name your program .C The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function,...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function...
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function definitions. You will also write a main() function that calls these four functions to demonstrate that they work as expected. The four functions are: 1. printExitMessage() : This function prints a message to the screen saying something like "Thanks for using this software. Goodbye." 2. getMin(): This function takes two float inputs and returns the lesser of the two float values. For example, if...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
write a program in C language Create a function to perform the insertion sort. Now the...
write a program in C language Create a function to perform the insertion sort. Now the program will perform the following steps: Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float). Read the N single precision floating-points elements to the allocated array. Invoke a function to sort the array using insertion sort (the insertion...
Write a program in a language of your choice to perform a search using the A*...
Write a program in a language of your choice to perform a search using the A* algorithm for the eight puzzle problem, in which numbers may be shifted one space at a time to transform the initial state into the goal state (see p. 103 – 3rd Ed., pp. 105-106 – 2nd Ed. of the text). 2. a) Use the start state-goal state combination given in pp. 103, Figure 3.28 (3rd Ed.), [pp. 105, Figure 4.7 (2rd Ed.)], as (start_1,...
Write a C program in Unix which uses a function called search to find the location...
Write a C program in Unix which uses a function called search to find the location of a value in THREE arrays of floats. The function should take three parameters :          the value to be found          the array to be searched          the size of the array N.B.!!!! The main program should read in three arrays of varying size          example : array a has twelve elements                    array b has six elements                    array c has nine...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below. The elements of the array are: 0 8 -4 6 7 The sum of the array is: 17 Press any key to continue . . .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT