Find Min and Max Number in Array in C programming language. Which is accessed by using common name. we use two types of arrays in C programming one and two-dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int max, min, i;
int scores[] = {39, 5, 40, 100, 49, 7}; //'scores' is name of Array.
max = scores[0]; //assigning 0 number Element of 'scores' Array to 'max'.
min = scores[0]; //assigning 0 number Element of 'scores' Array to 'min'.
for(i = 0; i < 6; i++) { //loop looping on 'scores' Array.
if(min > scores[i]) { //if 'min' value Greater then looped number element of
min = scores[i]; //Array'scores' then assign those number Array'scores'
} //Element to 'min'.
if(max < scores[i]) { //if 'max' value Less then looped number element of
max = scores[i]; //Array'scores' then assign those number Array'scores'
} //Element to 'max'.
}
printf("Minimum Number is: %d\n", min); //printing 'min' value.
printf("Maximum Number is: %d", max); //printing 'max' value.
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, p, swap;
int arr[] = { 6, 22, 33, 1, 4, 3, 54, 2 };
for (i = 0; i < 8; i++) //loop looping on 'arr'Array.
{
//loop looping on 'arr'Array U also write 7 on 'arr.Length-1'.
for (j = 0; j < 8 - 1; j++)
{
if (arr[j] > arr[j + 1])
{ //if 'arr[j]' greater then 'arr[j+1]' means 1st element greater then
swap = arr[j]; //2nd element of 'arr' array then assign 'arr[j]'
arr[j] = arr[j + 1]; //means 1st element to 'swap', assign 'arr[j+1]' to
arr[j + 1] = swap; //'arr[j]' means 2nd element to 1st, assign 'swap' value
} //to 'arr[j+1]' means to 2nd element of 'arr' Array.
}
}
for (p = 0; p < 8; p++) //loop looping on 'arr' Array.
{
printf("%d\n", arr[p]); //and printing 'arr' Array Elements.
}
getch();
}