Notice: Undefined offset: 13054520 in /var/www/html/qa-external/qa-external-users.php on line 744
Write the following program - OnlineGDB Q&A
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.

Write the following program

+18 votes
asked Sep 24, 2024 by asish sarkar (670 points)
Write a C program and define a function search() that search an element in the array and return the index of the element.

4 Answers

+1 vote
answered Sep 25, 2024 by Shubh Sharma (180 points)
#include<iostream>
using namespace std;

int search(int ar[],int n){
    int ele;
    cout<<endl<<"Enter the element you want to search: "<<endl;
    cin>>ele;
    
    for(int i=0;i<n;i++){
        if(ar[i]==ele){
            cout<<"index of searched element= "<<i<<endl;
            return i;
        }
    }
    cout<<"element not found";
}

int main(){
    int n;
    
    cout<<"Enter size of the array= ";
    cin>>n;
    int ar[n];
    
    cout<<"enter "<<n<<" elements in array: "<<endl;
    for(int i=0;i<n;i++){
        cin>>ar[i];
    }
    cout<<"Given array is: "<<endl;
    for(int i=0;i<n;i++){
        cout<<ar[i]<<"  ";
    }
    search(ar,n);
    return 0;
}
commented Sep 25, 2024 by Shubh Sharma (180 points)
this code will give the first index at which searched element is find
commented Sep 25, 2024 by Peter Minarik (101,340 points)
This is NOT a C code. This is C++.

The return value of search() is not defined when the element is not found.
commented Sep 25, 2024 by Shubh Sharma (180 points)
Bro it will return element not found
commented Sep 25, 2024 by Shubh Sharma (180 points)
Bro sorry only change the header and cout cin
commented Sep 25, 2024 by Peter Minarik (101,340 points)
"Bro it will return element not found"

that's not a return value of a function, that's a text printed on the standard output. See my comments (// <<<<) in the below code snippet.

int search(int ar[],int n){
    int ele;
    cout<<endl<<"Enter the element you want to search: "<<endl;
    cin>>ele;
    
    for(int i=0;i<n;i++){
        if(ar[i]==ele){
            cout<<"index of searched element= "<<i<<endl;
            return I; // <<<< Here you return the index of the found element.
        }
    }
    cout<<"element not found";
    // <<<< You should return something here too to indicate no element is found. That's typically -1
}
commented Sep 25, 2024 by Shubh Sharma (180 points)
Ok thanks for correcting me mate
+2 votes
answered Sep 25, 2024 by Manas Sharma (190 points)
#include <stdio.h>

int linearSearch(int arr[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == target) {
            return i;
        }
    }
    return -1;  
}

int main() {
    int n;
    printf("Enter size of array: ");
    scanf("%d", &n);
    
    int arr[n];   
    printf("Enter elements:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);  
    }
    
    int target;
    printf("Enter target element to search: ");
    scanf("%d", &target);   
    
    int result = linearSearch(arr, n, target);
    
    if (result != -1) {
        printf("Element found at index: %d\n", result);
    } else {
        printf("Element not found in the array.\n");
    }
    
    return 0;
}
0 votes
answered Oct 11, 2024 by Nikhila Nandyala (410 points)
public class Car{
         String model;
         public Car( String model){
            this.model=model;
}
         public void start(){
                System.out.println("The"+model+" is starting.")
   }
}

    public class Main{
         public static void main (string []args){
             Car myCar= new Car("Toyota");
              myCar.start();
    }
}
+2 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.
...