How to Find Maximum Value in an Array in C? (original) (raw)
Last Updated : 22 Feb, 2024
In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C.
**Example
**Input: arr = {5,3,1,2,4}
**Output: The maximum value of the array is: 5
Finding Maximum Value in an Array in C
We can find the maximal value in an array by taking a variable max and repeatedly comparing it to the elements of the array using loops.
Algorithm
- Initialize a variable maxVal with value arr[0].
- Traverse through the whole array. During traversal:
- If maxVal is less than the current value of array update the value of maxVal.
- Else continue.
- Exit the loop after whole traversal and Print res which denotes the maximum value in array.
C Program to Find the Maximum Value in an Array
C `
// C program to find maximum value in an array #include <stdio.h>
int main() {
// Initialize an array
int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 };
// Find the size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Intialize the variable which will denote the maximum
// element
int res = arr[0];
// Find the maximum value in the array and store it in
// res
for (int i = 0; i < n; i++) {
if (res < arr[i])
res = arr[i];
}
// print the elements of the array
printf("Array Elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// print the maximum value
printf("The maximum value of the array is: %d", res);
return 0;
}
`
Output
Array Elements: 23 12 45 20 90 89 95 32 65 19 The maximum value of the array is: 95
**Time Complexity: O(N) where N is the number of elements in the array.
**Auxiliary Space: O(1)
Similar Reads
- C Program to Find Minimum Value in Array In this article, we will learn how to find the minimum value in the array.The easiest and straightforward method is to iterate through each element of the array, comparing each value to an assumed minimum and updating it if the current value is less.C#include <stdio.h> int findMin(int arr[], i 3 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read
- How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read
- How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read
- C Program For Maximum and Minimum of an Array Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1Â Â Â Â Â Â Â Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output:Â Minimum eleme 7 min read
- C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 3 min read
- C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h 3 min read
- C Program to Traverse an Array Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro 3 min read