Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Help me to Convert the following program using function

+15 votes
asked Sep 24, 2024 by asish sarkar (670 points)
include<stdio.h>

int main()

{

int arr[5],i,item;

printf("enter any five element=\n");

for(i=0;i<5;i++)

{

scanf("%d",&arr[i]);

}

printf("enter the item to be searched=");

scanf("%d",&item);

for(i=0;i<5;i++)

{

if(arr[i]==item)

{

printf("%d is found in the series",item);

break;

}}

if(i==5)

{

printf("%d is not found in the series",item);

}

return 0;

}

5 Answers

0 votes
answered Sep 24, 2024 by Yartik_ Kamboj (140 points)
#include <stdio.h>

int search(int *arr, int size,int item)

{

    int i, loc=0;

    for (i = 0; i < size; i++)

    {

        if (arr[i] == item)

        {

            loc = i + 1;

        }

    }

    return loc;

}

void main(){

    int arr[100],item, size, i,location;

    printf("Enter the size of the array:");

    scanf("%d", &size);

    printf("Enter the elements of the array:");

    for (i=0; i<=size-1; i++)

    {scanf("%d", &arr[i]);}

    printf("Enter the element to be search:");

    scanf("%d", &item);

    location = search(arr, size,item);

    if(location == 0)

        {

            printf("Element not found.");

        } else{

            printf("Element found  at location %d.", location);

        }

}
commented Sep 27, 2024 by Peter Minarik (101,340 points)
edited Sep 27, 2024 by Peter Minarik
This is a very unusual implementation. Normally, you return the element's index in the array, not index + 1.
If the element is not found a negative number (typically -1) is returned, not 0.
0 votes
answered Sep 25, 2024 by Anuja Rode-Tungar (150 points)
# include<stdio.h>
int search(int a[], int x);

int main()

{ int arr[5],i,item, found=0;
  printf("enter any five element=\n");
  for(i=0;i<5;i++)
  {   scanf("%d",&arr[i]);  }
  
  printf("enter the item to be searched="); scanf("%d",&item);
   
  found= search(arr,item);
  if (found==1) printf ("\n found") ; else printf("\nnot found");
  
}// end of main

int search(int a[], int x)
{ int i,ans=0;
  for(i=0;i<5;i++)
  { if(a[i]==x)
    {  printf("%d in function is found in the series",x);ans=1;
      break;}
  }
 
  if(i==5)
  { ans=0; printf("%d in function is not found in the series",x);}
       
   return(ans);
}
commented Sep 27, 2024 by Peter Minarik (101,340 points)
edited Sep 27, 2024 by Peter Minarik
This does not work right. If the element is found, search() always returns 1, although it normally should return the index of the element.

Furthermore, the search() function knows about the length of the passed-in array, which makes this function very specific and would not work with any other arrays with different lengths. The usual solution is to pass in the length of the array as well.
0 votes
answered Sep 25, 2024 by Peter Minarik (101,340 points)
0 votes
answered Sep 25, 2024 by FS20IF058 (140 points)
This is how your going to change your code to the functions

#include<stdio.h>

int arr[5],i,item;

void enterelements()

{

printf("enter any five element=\n");

for(i=0; i<5; i++)

{

scanf("%d",&arr[i]);

}

}

void searchelements() {

printf("enter the item to be searched=");

scanf("%d",&item);

for(i=0; i<5; i++)

{

if(arr[i]==item)

{

printf("%d is found in the series",item);

break;

}

}

if(i==5)

{

printf("%d is not found in the series",item);

}

}

int main()

{

    enterelements(arr[5],i);

    searchelements(item,i,arr[5]);

return 0;

}
commented Sep 26, 2024 by Peter Minarik (101,340 points)
enterelements(arr[5],i);
searchelements(item,i,arr[5]);

your functions take no parameters, so you shouldn't provide any arguments. Especially wrong ones.
0 votes
answered Oct 27, 2024 by Shared codes (230 points)

#include <stdio.h>

void search(int arr[],int ser[],int s,int t){

    for(int i=0;i<s;i++){

        if(arr[i]==t)ser[i]=1;

        else ser[i]=0;

    }

    return;

}

 int main(){

    int s;

    printf("Enter size of array: ");

    scanf("%d", &s);

   

    int arr[s];  

    printf("Enter elements : ");

    for (int i = 0; i < s; i++) {

        scanf("%d", &arr[i]);  

    }

   

    int t;

    printf("Enter target element to search: ");

    scanf("%d", &t);

    int ser[s];

    search(arr,ser,s,t);

    printf("Elements with same target value : ");

    int sum=0;

    for(int i=0;i<s;i++){

        sum+=ser[i];

    if(ser[i]==1){printf(" %d",i+1);}

    }

    if(sum==0){printf("Not Found");}

    return 0;

 }

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...