Introduction
Finding the largest element in an array is a common task in programming. This guide will show you how to write a C program to identify the largest element in an array provided by the user.
Problem Statement
Create a C program that:
- Takes the size of the array as input from the user.
- Takes the elements of the array as input.
- Finds the largest element in the array.
- Displays the largest element.
Example:
- Input: Array size = 5, Elements = [1, 22, 13, 44, 5]
- Output: Largest element = 44
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the array size, the array elements, and the largest element.
- Input the Array Size: Use
scanfto take input from the user for the size of the array. - Input the Array Elements: Use a loop to take input from the user for the elements of the array.
- Find the Largest Element: Use a loop to iterate through the array and find the largest element.
- Display the Largest Element: Use
printfto display the largest element.
C Program
#include <stdio.h>
/**
* C Program to Find the Largest Element in an Array
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the array size, elements, and largest element
int n, largest;
// Step 2: Prompt the user to enter the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Step 3: Declare an array to hold the elements
int arr[n];
// Step 4: Input the array elements
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Step 5: Initialize the largest variable with the first element of the array
largest = arr[0];
// Step 6: Find the largest element in the array
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
// Step 7: Display the largest element
printf("Largest element in the array: %d\n", largest);
return 0; // Step 8: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
nis declared to store the size of the array.largestwill store the largest element found in the array.
Step 2: Input the Array Size
- The program prompts the user to enter the size of the array using
printf. Thescanffunction then reads the input and stores it in the variablen.
Step 3: Declare the Array
- The program declares an array
arrof sizento hold the elements provided by the user.
Step 4: Input the Array Elements
- The program uses a
forloop to take input for each element of the array. The loop iterates from 0 ton-1, reading the elements usingscanf.
Step 5: Initialize the Largest Variable
- The program initializes the variable
largestwith the first element of the array,arr[0]. This assumes that the first element is the largest, and it will be updated as the program checks the other elements.
Step 6: Find the Largest Element in the Array
- The program uses another
forloop to iterate through the array starting from the second element (index 1). For each element, it checks if the element is greater than the current value oflargest. If it is,largestis updated to that element’s value.
Step 7: Display the Largest Element
- The program displays the largest element found in the array using the
printffunction.
Step 8: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example:
Enter the number of elements in the array: 5
Enter 5 elements:
1
22
13
44
5
Largest element in the array: 44
Conclusion
This C program demonstrates how to find the largest element in an array. It covers basic concepts such as arrays, loops, and conditional statements, making it a useful example for beginners learning C programming.