C Program to Find The Second Largest Element Of An Array

Arrays provide the great way to store the values. And plays ah important role in C. I made many programs on arrays. And as we know array elements are always store in Contiguous Memory Location so it’s always great to combine array with pointers that makes accessing and manipulating array more handy. 

You might made the program to find the largest element of array. I come with the code to find the second largest element and also the use of macros in the program to run it more faster and avoid re-righting the same line again and again.

Lets Have ah Look at the Program ►►



#define loop for(i=0;i<size;i++)        // macro definition

void main()


int arr[100];

int size,larg=0,seclarg=0,i;

printf(“Enter the size of Array\n”);      //Number of Elements array will store


printf(“Enter the Elements\n”);             //Entering the data



loop                                                 //Finding the largest element





loop                                            //Finding the second largest element


if(arr[i]>seclarg && arr[i]!=larg)



printf(“%d is the Second Largest element of Array\n”,seclarg);



 I think you already made out the things. 

What I did is first made a macro (using #define) for loop because I know that I’ll need to use this at many place in the program so it eliminates the need for re-righting the same line. Then I first find the largest element of the array by conditioning if(arr[i]>larg) this will check the every element of the array and the element which will be greater will simply assign to larg using larg=arr[i]; statement.

Then I again use the same code and now this time I add the arr[i]!=larg condition in if statement which means the element which is greater should not be equal to the largest value which we assign first (larg). Remember the if(arr[i]>seclard && arr[i]!=larg) will only be true only when both the two conditions will satisfy. And then simply I printed out the Second Largest Element.

Remember to initialize the larg and seclag variable to 0(zero) to find the larg value otherwise any garbage value will be conditioned it that case.

Confused ?? Or Want to ask something .. Feel Free to Ask


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s