write a c program to find the second largest elements of a given array.

27 Dec 2022 Balmiki Mandal 0 C Programming

C Program to Find the Second Largest Element in an Array

"Write a C program to find the second largest element of a given array" means that you are being asked to write a computer program in the C programming language that will find the second largest value in a specified array.

The program should take the array as input from the user, and then find and output the second largest element. The second largest element is the element in the array that has the second highest value, after the largest element.

To accomplish this task, the program needs to compare each element of the array with the largest element found so far, and update the second largest element if a new element is found that is larger than the current second largest element but smaller than the largest element.

C program to find the second largest element in a given array:

#include<stdio.h>

int main() {
    int arr[100], n, i, largest, secondLargest;

    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    printf("Enter the elements of the array:\n");
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // find the largest element in the array
    largest = arr[0];
    for(i = 1; i < n; i++) {
        if(arr[i] > largest) {
            largest = arr[i];
        }
    }

    // find the second largest element in the array
    secondLargest = arr[0];
    for(i = 1; i < n; i++) {
        if(arr[i] != largest && arr[i] > secondLargest) {
            secondLargest = arr[i];
        }
    }

    printf("The second largest element in the array is: %d", secondLargest);

    return 0;
}

In this program, we first take the input array from the user using scanf(). Then we use a loop to find the largest element in the array. We initialize the largest element to the first element of the array, and then we compare each subsequent element to the largest element so far. If an element is greater than the current largest element, we update the largest element.

Next, we use another loop to find the second-largest element in the array. We initialize the second-largest element to the first element of the array, and then we compare each subsequent element to both the largest element and the current second-largest element. If an element is not equal to the largest element (since we want the second-largest element) and is greater than the current second-largest element, we update the second-largest element.

Finally, we print out the second largest element to the user using printf().

Further Reading:

write a program to find the largest elements of a given array with an index.

write a c program to find the second largest elements of a given array.

write a program to find the first largest and second-largest elements of a given array.

Write a c program find largest elements in this array number or index

Write a C program to find out the second largest and second smallest elements of any unsorted array without using any shorting Technique

write a program to find the smallest elements of a given array with an index.

write a program to find the first largest and first smallest elements of a given array.

write a program to find the second smallest elements of a given array.

write a program to find the second largest and second smallest elements of a given array.

write a program to find the first smallest and second smallest elements of a given array.

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.