In: Computer Science
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
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