# C find max value in array

By John

An array is a data structure containing a collection of values or variables. The simplest type of array is a linear array or one-dimensional array. An array can be defined in C with the following syntax:

int Arr = {12, 56, 34, 78, 100};
/* here 12,56,34,78,100 are the elements at indices 0,1,2,3,4 respectively */

In this example, array Arr is a collection of 5 integers. Each integer can be identified and accessed by its index. The indices of the array start with 0, so the first element of the array will have index 0, the next will have index 1 and so on.

Largest element of the array is the array element which has the largest numerical value among all the array elements.

Examples:

If we are entering 5 elements (N = 5), with array element values as 12, 56, 34, 78 and 100
Then, largest element present in the given array is: 100

If we are entering 4 elements (N = 4), with array element values as 1, 6, 3, and 2
Then, largest element present in the given array is: 6Method 1: Largest Element of an Array in C using Loops (Naive Approach)
In this approach, at first we assume that the first element is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element.

Example:

Given Array: [1,2,4,1,6,4]

Initially, Largest Element: 1

Now we start traversing the array.

Index: 1
Index element > Largest Element, so updating the largest element with 2.

Index: 2
Index element > Largest Element, so updating the largest element with 4.

Index: 3
Index element < Largest Element, so Largest element remains the same. Index: 4 Index element > Largest Element, so updating the largest element with 6.

Index: 5
Index element < Largest Element, so Largest element remains the same. So Largest Element is 6. Program/Source Code Here is the source code of the C Program to find the largest number in an array using loops. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below. /* * C Program to find the largest number in an array using loops */ #include

int main()
{
int size, i, largest;

printf(“\n Enter the size of the array: “);
scanf(“%d”, &size);
int array[size]; //Declaring array

//Input array elements

printf(“\n Enter %d elements of the array: \n”, size);

for (i = 0; i < size; i++) { scanf(" %d", &array[i]); } //Declaring Largest element as the first element largest = array; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("\n largest element present in the given array is : %d", largest); return 0; } Program Explanation 1. Take the size of the array as input from the user. 2. Then, initialize an array of size given by the user. 3. Using for loop, take array element as input from users and insert them into the array. 4. After inserting all the elements of the array, consider the very first element of array to be the largest. 5. Run a for loop, from 1 to arraySize-1, extracting array element one by one and comparing it to the largest element. 6. If the largest element is smaller than the element being compared, then the largest element is updated with the value of the current element of the array. 7. In the end, the largest element will hold the actual largest value present in the array. Time Complexity: O(n) In the program, we are traversing the array so the time complexity is O(n). Space Complexity: O(n) Space is required to store the array, so space complexity is O(n). Runtime Test Cases Here is the runtime output of the C program where the user is reading array of 6 elements with values as 1, 2, 4, 1, 6, and 4. Then it finds out the largest element and displays its value. Enter the size of the array: 6 Enter 5 elements of the array: 1 2 4 1 6 4 largest element present in the given array is: 6