This is a C Program to calculate the largest of two numbers in a given Array.

This program will implement a one dimentional array, then compares the elements and finds which is the largest two element in a given array.

1. Create an array and define the elements of the array.

2. Considering the first element of the array to be the largest number and second element of the array to be the second largest element.

3. Interchange these two numbers if required.

4. Now run the loop from third element of the array to the last element.

5. Scan element of the array, comparing array elements with first largest and second largest numbers, changing both or one if required.

6. At the end, after for loop, we will be getting the actual first largest and second largest number in the array.

Here is source code of the C program to calculate the largest of two numbers in a given array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C program to read in four integer numbers into an array and find the`

`* average of largest two of the given numbers without sorting the array.`

`* The program should output the given four numbers and the average.`

`*/`

`#include <stdio.h>`

`#define MAX 4`

void main()

`{`

int array[MAX], i, largest1, largest2, temp;

printf("Enter %d integer numbers \n", MAX);

for (i = 0; i < MAX; i++)

`{`

scanf("%d", &array[i]);

`}`

printf("Input integer are \n");

for (i = 0; i < MAX; i++)

`{`

printf("%5d", array[i]);

`}`

printf("\n");

`/* assume first element of array is the first larges t*/`

largest1 = array[0];

`/* assume first element of array is the second largest */`

largest2 = array[1];

if (largest1 < largest2)

`{`

temp = largest1;

largest1 = largest2;

largest2 = temp;

`}`

for (i = 2; i < 4; i++)

`{`

if (array[i] >= largest1)

`{`

largest2 = largest1;

largest1 = array[i];

`}`

else if (array[i] > largest2)

`{`

largest2 = array[i];

`}`

`}`

printf("n%d is the first largest \n", largest1);

printf("%d is the second largest \n", largest2);

printf("\n Average of %d and %d = %d \n", largest1, largest2,

(largest1 + largest2) / 2);

`}`

1. Declare an array of some fixed size, say 4.

2. Using for loop, define the elements of the array.

3. Consider the first element of array to be the first largest number (store it in a variable, largest1).

4. Consider the second element of array to be the second largest number (store it in a variable, largest2).

5. Now interchange both if largest1 is smaller than largest2.

6. Run a for loop from third element of the array till the last element of the array, wherein each element will be compared to the largest1.

i) If the array element is greater than largest1, then largest1 is assigned the array element. And the value of largest1 gets transferred to largest2.

ii) If the array element is smaller than largest1 and but greater than largest2, then only largest2 is assigned the array element.

7. Running loop till end, will give us the actual first largest and second largest number.

8. Exit

Enter 4 integer numbers 80 23 79 58 Input interer are 80 23 79 58 80 is the first largest 79 is the second largest Average of 80 and 79 = 79

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.