0% found this document useful (0 votes)
14 views1 page

Here Is A C Program That Searches For A Specific Element in An Array of Size 7

The document presents a C program that searches for a specific element in an array of size 7. It defines a function to check for the element's presence and returns the index if found, or -1 if not. The program prompts the user for input and displays the result based on the search outcome.

Uploaded by

nickitagiramata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Here Is A C Program That Searches For A Specific Element in An Array of Size 7

The document presents a C program that searches for a specific element in an array of size 7. It defines a function to check for the element's presence and returns the index if found, or -1 if not. The program prompts the user for input and displays the result based on the search outcome.

Uploaded by

nickitagiramata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Here is a C program that searches for a specific element in an array of size 7, initialized with

values {12, 45, 23, 78, 56, 89, 34}, and prints the index if the element is found or a
message saying it is not found:

#include <stdio.h>

int search_element(int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index if the element is found
}
}
return -1; // Return -1 if the element is not found
}

int main() {
int arr[] = {12, 45, 23, 78, 56, 89, 34};
int size = 7; // Size of the array
int target;
printf("Enter the element to search for: ");
scanf("%d", &target);
int index = search_element(arr, size, target);
if (index != -1) {
printf("Element %d found at index %d.\n", target, index);
} else {
printf("Element %d not found in the array.\n", target);
}

return 0;
}

How it works:

1. The function search_element takes the array arr[], its size, and the target element
as input.
2. It uses a for loop to iterate through the array and check if the target element is present.
3. If the element is found, it returns the index of that element. If it is not found, it returns -1.
4. The main function takes the target element input from the user and calls the
search_element function.
5. Based on the return value, it either prints the index of the element or a message saying
that the element was not found.

Example Run:
Enter the element to search for: 56

Element 56 found at index 4.

You might also like