how to find the largest number in an array
This is a C Program to find the largest number in an array.
Problem Clarification
We have to write a program in C such that the plan will read a one-dimensional array and observe out the largest element present in the array.
Expected Input and Output
If we are inbound 5 elements (N = 5), with array element values as 12, 56, 34, 78 and 100
Then, largest chemical element nowadays in the given assortment is: 100
Problem Solution
Fundamentally, an array is a information construction 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[5] = {12, 56, 34, 78, 100};
/* here 12,56,34,78,100 are the elements at indices 0,1,2,3,iv respectively */
In this example, assortment Arr is a collection of 5 integers. Each integer tin exist identified and accessed by its index. The indices of the array start with 0, so the outset chemical element of the array will take index 0, the next volition accept index 1 and then on.
In this program, we take to find the largest element present in the array. We will practise this past first saving the value of the first element in the variable 'largest'. Then we will compare with remaining elements of the array and store the value if another larger number is plant in this assortment. This volition proceed N-1 times and the program ends.
The sequence of steps for the solution volition exist as follows:
i. Create an array of user-defined size.
2. Run the for loop till the user-defined size to insert the element at each location.
iii. Considering the first element of the assortment to be the largest, compare all the remaining elements of the assortment, and alter the largest value if assumed largest element is smaller than the element beingness compared.
4. At terminal, the largest element will hold the actual largest value in the array. Thus, print information technology.
Plan/Source Code
Hither is the source lawmaking of the C Program to notice the largest number in an array. The plan is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
-
/*
-
* C plan to read N integers into an assortment A and
-
* a) Find the sum of all numbers
-
* b) Find the boilerplate of all numbers
-
* Display the results with suitable headings
-
*/
-
#include <stdio.h>
-
int main( )
-
{
-
int size, i, largest;
-
printf ( "\northward Enter the size of the assortment: " ) ;
-
scanf ( "%d" , &size) ;
-
int array[size] ;
-
printf ( "\due north Enter %d elements of the assortment: \n" , size) ;
-
for (i = 0 ; i < size; i++ )
-
{
-
scanf ( "%d" , &array[i] ) ;
-
}
-
largest = array[ 0 ] ;
-
for (i = ane ; i < size; i++ )
-
{
-
if (largest < array[i] )
-
largest = array[i] ;
-
}
-
printf ( "\n largest element present in the given assortment is : %d" , largest) ;
-
return 0 ;
-
}
Program Caption
1. Have the size of the assortment as input from the user.
2. And so, initialize an array of size given by the user.
3. Using for loop, accept array element as input from users and insert them into the array.
iv. Later inserting all the elements of the array, consider the very first element of array to be the largest.
five. Run a for loop, from one to arraySize-1, extracting array chemical element one by one and comparing it to the largest element.
6. If the largest element is smaller than the element existence compared, and so the largest element is updated with the value of the electric current element of the array.
vii. In the end, the largest chemical element volition hold the actual largest value present in the array.
Runtime Exam Cases
Here is the runtime output of the C program where the user is reading array of 5 elements with values as 12, 56, 34, 78 and 100. Then information technology finds out the largest element and displays its value.
Enter the size of the assortment: 5 Enter 5 elements of the array: 12 56 34 78 100 largest element present in the given array is: 100
Sanfoundry Global Didactics & Learning Series – 1000 C Programs.
Here's the list of Best Books in C Programming, Data-Structures and Algorithms
Next Steps:
- Become Free Certificate of Merit in C Programming
- Participate in C Programming Certification Competition
- Go a Top Ranker in C Programming
- Have C Programming Tests
- Chapterwise Do Tests: Chapter 1, 2, iii, iv, five, 6, 7, 8, 9, x
- Chapterwise Mock Tests: Chapter one, two, iii, 4, 5, vi, vii, viii, 9, x
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on evolution of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his costless Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
Source: https://www.sanfoundry.com/c-program-find-largest-number-array/
Posted by: salazarlinut1989.blogspot.com
0 Response to "how to find the largest number in an array"
Post a Comment