banner



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.

  1.                       /*                    
  2.                        * C plan to read N integers into an assortment A and                    
  3.                        * a) Find the sum of all numbers                    
  4.                        * b) Find the boilerplate of all numbers                    
  5.                        * Display the results with suitable headings                    
  6.                        */                    
  7.                   
  8.                       #include <stdio.h>                    
  9.                   
  10.                       int                      main(                      )                    
  11.                       {                    
  12.                   
  13.                       int                      size,                      i,                      largest;                    
  14.                   
  15.                       printf                      (                      "\northward                        Enter the size of the assortment: "                      )                      ;                    
  16.                       scanf                      (                      "%d"                      ,                      &size)                      ;                    
  17.                       int                      array[size]                      ;                    
  18.                   
  19.                       printf                      (                      "\due north                        Enter %d elements of  the assortment:                        \n"                      ,                      size)                      ;                    
  20.                   
  21.                       for                      (i                      =                      0                      ;                      i                      <                      size;                      i++                      )                    
  22.                       {                    
  23.                       scanf                      (                      "%d"                      ,                      &array[i]                      )                      ;                    
  24.                       }                    
  25.                   
  26.                       largest                      =                      array[                      0                      ]                      ;                    
  27.                   
  28.                       for                      (i                      =                      ane                      ;                      i                      <                      size;                      i++                      )                    
  29.                       {                    
  30.                       if                      (largest                      <                      array[i]                      )                    
  31.                       largest                      =                      array[i]                      ;                    
  32.                       }                    
  33.                   
  34.                       printf                      (                      "\n                        largest element present in the given assortment is : %d"                      ,                      largest)                      ;                    
  35.                   
  36.                       return                      0                      ;                    
  37.                   
  38.                       }                    

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

Manish Bhojasia - Founder & CTO at Sanfoundry

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel