Computer Science, asked by motekranthisai, 4 months ago

write a program which should consist of a user defined function Task()[Function returns no value].Pass 1D array to the function,along with number of elements of array and element to search.Function should implement linear search, and if the element is not found in the array,then insert that element at the end of array.Display the final array after insertion [after function is called]in the main() function.[Note:Array should be passed using by reference approach].( c language)​

Answers

Answered by Anonymous
9

Answer:

#include<stdio.h>

int task(int*, int, int);

int main()

{

 int array[100], search, c, n, position;

 

 printf("Enter the number of elements in array\n");

 scanf("%d",&n);

 

 printf("Enter %d numbers\n", n);

 

 for ( c = 0 ; c < n ; c++ ){

   scanf("%d",&array[c]);

 }

     

 

 printf("Enter the number to search\n");

 scanf("%d",&search);

 

 position = task(array, n, search);

 

 if ( position == -1 ){

   printf("%d is not present in array.\n", search);

 }

     

 else{

   printf("%d is present at location %d.\n", search, position+1);

 }

 return 0;

}  

 

int task(int *pointer, int n, int find)

{

 int c;

 

 for ( c = 0 ; c < n ; c++ )

 {

    if ( *(pointer+c) == find ){

     return c;

 }

         

 }

 

 return -1;

}

Similar questions