473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

find out the largest and second largest number in an array

Expand|Select|Wrap|Line Numbers
  1. int a[10],i,l,s,n;
  2. l=0,s=0;
  3. printf("Enter the limit\n");
  4. scanf("%d",&n);
  5. printf("Enter the array\n");
  6. for(i=0;i<n;i++)
  7. scanf("%d",&a[i]);
  8. for(i=0;i<n;i++)
  9. if(a[i]>l)
  10. {
  11. s=l;
  12. l=a[i];
  13. }
  14. printf("\nLargest no.is%d",l);
  15. printf("\nSecond Largest no.is%d",s);
Nov 18 '11 #1
5 25102
Banfa
9,065 Expert Mod 8TB
You haven't asked a question
Nov 18 '11 #2
Hey dear here is a program to find second greater and third greater no i made two functions to get second and third greater no:


#include<iostream>
using namespace std;
int sec(int x,int y)
{
int greater;
if(x>y)
greater=x;
else
greater=y;
return greater;
}
int third(int x,int y,int z)
{ int m;
m=sec(sec(x,y),z);
return m;

}

int main()

{
int a,b,c,z,d;
cin>>a;
cin>>b;
cin>>z;

c=sec(a,b);
cout<<"2nd greater : "<<c<<endl;

d=third(a,b,z);
cout<<"finally Third greater no is: "<<d<<endl;

system("pause");
return 0;
}
Hope it helps:))
Nov 18 '11 #3
@sunithasiraj: Program u posted will not work out for the following inputs :
1) limit = 5, values are 50, 40, 30, 20, 10. the output is 50, 50.
2) This will not find out the largest for Negative numbers too...( Limit = 5, values are -50 -40 -30 -20 -10 and the output is 0, 0)
Feb 14 '12 #4
Maraj
24
Here is the program which Outputs First and second largest number
Expand|Select|Wrap|Line Numbers
  1. // max.cpp : Defines the entry point for the console application.
  2. //Finds second maximum number in the array.
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7. void max(int [],int *,int *);            //Define a function max which gives maximun ans second maxium number.
  8.                         //Pass two int variables by-refernce and an array.
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     int max1,max2,i,a[5];            //Define three int variables and an array of your required size.
  13.     cout<<"enter array elements"<<endl;    //Input value of array elements.
  14.     for(i=0;i<5;i++)            //Loop for input.
  15.     {
  16.     cin>>a[i];
  17.     }
  18.     max(a,&max1,&max2);            //Call the function.
  19.  
  20.  
  21.  
  22.     cout<<"\nMaximum number is"<<max1<<endl;//These lines will execute after function execution.
  23.     cout<<"\n 2nd maximum number is"<<max2<<endl;
  24.     return 0;
  25. }
  26.  void max(int b[5],int *m1,int *m2)        //Function will receive address of variables and array.
  27.  {    int j,min=b[0];                //Define an int min required to find second maximum number.
  28.     *m1=b[0];
  29.  
  30.     for(j=0;j<5;j++)            //First find maximum number.            
  31.     {
  32.     if(b[j]>*m1)
  33.     *m1=b[j];                //Maimum number is in *m1.
  34.     }
  35.     for(j=0;j<5;j++)            //Now find minimum nubmer.
  36.     {
  37.         if(b[j]<min)
  38.             min=b[j];        //Minimum nuber goes to min.
  39.     }
  40.     *m2=min;
  41.     cout<<"\nMinimum number is"<<min<<endl;
  42.  
  43.     for(j=0;j<5;j++)            //Now here the logic for second maximum number.
  44.     {
  45.  
  46.                         //If b[J] is greatar than *m2 and b[j] is less than *m1 which is maximum number
  47.                         //then assign *m2 the value of element of array pointing currently.
  48.     if(b[j]>*m2 && b[j]<*m1)        
  49.     {
  50.     *m2=b[j];
  51.  
  52.     }
  53.     }
  54.  
  55.  
  56.  }
  57.  
  58.  
Feb 18 '12 #5
whodgson
542 512MB
why not just sort the array....the largest will be the last array element and the second largest will be the second last element.
if the array is a[200]then sort it with sort(a,a+200) which is defined in the <algorithm> header. a[199]and a[198] will be the largest and second largest resp.

edit:providing last element NOT duplicated
Mar 2 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
2
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
20
by: Rajesh | last post by:
Hello Everybody, Can anybody help me to write a C program for finding the second largest element in an array. without using any sort algo. The array may conatin duplicate elements. The algo...
2
by: k1ckthem1dget | last post by:
I dont know how to find the mode of an array. I am to do the following. (1) Given the following: · A list of 11 integers; 1,2,3,3,3,2,2,1,3,4,5 (2) The program should:
1
by: faizan qazi | last post by:
Hello Greetings Guide Me To Find Second Highest Number In Array
40
by: ravi | last post by:
Can anybody tell me a method to Use only N + O(log n) comparisons to find the second largest (or smallest) element in a list of N elements. Thnx in advance
1
karthickkuchanur
by: karthickkuchanur | last post by:
how to find the seventh largest number in a column for example 1,2,3,4,5,6,7
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
6
by: AsHantoosH | last post by:
#include<stdio.h> main() { int a; printf("Enter a: \n"); scanf("%d", &a);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.