- int a[10],i,l,s,n;
-
l=0,s=0;
-
printf("Enter the limit\n");
-
scanf("%d",&n);
-
printf("Enter the array\n");
-
for(i=0;i<n;i++)
-
scanf("%d",&a[i]);
-
for(i=0;i<n;i++)
-
if(a[i]>l)
-
{
-
s=l;
-
l=a[i];
-
}
-
printf("\nLargest no.is%d",l);
-
printf("\nSecond Largest no.is%d",s);
5 25102 Banfa 9,065
Expert Mod 8TB
You haven't asked a question
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:))
@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)
Here is the program which Outputs First and second largest number -
// max.cpp : Defines the entry point for the console application.
-
//Finds second maximum number in the array.
-
-
#include "stdafx.h"
-
#include<iostream>
-
using namespace std;
-
void max(int [],int *,int *); //Define a function max which gives maximun ans second maxium number.
-
//Pass two int variables by-refernce and an array.
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
int max1,max2,i,a[5]; //Define three int variables and an array of your required size.
-
cout<<"enter array elements"<<endl; //Input value of array elements.
-
for(i=0;i<5;i++) //Loop for input.
-
{
-
cin>>a[i];
-
}
-
max(a,&max1,&max2); //Call the function.
-
-
-
-
cout<<"\nMaximum number is"<<max1<<endl;//These lines will execute after function execution.
-
cout<<"\n 2nd maximum number is"<<max2<<endl;
-
return 0;
-
}
-
void max(int b[5],int *m1,int *m2) //Function will receive address of variables and array.
-
{ int j,min=b[0]; //Define an int min required to find second maximum number.
-
*m1=b[0];
-
-
for(j=0;j<5;j++) //First find maximum number.
-
{
-
if(b[j]>*m1)
-
*m1=b[j]; //Maimum number is in *m1.
-
}
-
for(j=0;j<5;j++) //Now find minimum nubmer.
-
{
-
if(b[j]<min)
-
min=b[j]; //Minimum nuber goes to min.
-
}
-
*m2=min;
-
cout<<"\nMinimum number is"<<min<<endl;
-
-
for(j=0;j<5;j++) //Now here the logic for second maximum number.
-
{
-
-
//If b[J] is greatar than *m2 and b[j] is less than *m1 which is maximum number
-
//then assign *m2 the value of element of array pointing currently.
-
if(b[j]>*m2 && b[j]<*m1)
-
{
-
*m2=b[j];
-
-
}
-
}
-
-
-
}
-
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
|
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...
|
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...
|
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:
|
by: faizan qazi |
last post by:
Hello
Greetings
Guide Me To Find Second Highest Number In Array
|
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
|
by: karthickkuchanur |
last post by:
how to find the seventh largest number in a column
for example
1,2,3,4,5,6,7
|
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...
|
by: AsHantoosH |
last post by:
#include<stdio.h>
main() {
int a;
printf("Enter a: \n");
scanf("%d", &a);
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |