How to Find the Second Greatest Number from Array in C++

This is the best program in C++ for logic building and understanding the arrays. The program will find out the second greatest number in a given array.

Source Code   

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define MIN -1
int main()
{
cout<<"\a\n\n\n"
<<"\t******************************************************"<<endl
<<"\t** **"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** **"<<endl
<<"\t** Second Largest **"<<endl
<<"\t** **"<<endl
<<"\t** Demo **"<<endl
<<"\t** **"<<endl
<<"\t******************************************************"<<endl<<endl<<endl;cout<<"";


int largest=MIN,secondLargest=MIN;
int array[6]={3,1,7,3,9,5};
for(int i=0;i<5;i++){
cout<<array[i]<<",";
}
for(int i=0;i<5;i++){
if(array[i]>largest){
secondLargest=largest;
largest=array[i];
}
}
cout<<endl<<"Second Largest:"<<secondLargest;
int i;
cin>>i;
return 0;
}

Output of the Program

Find the Second Greatest Number from Array in C++


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment