Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Print 2nd largest value from array

This is a discussion on Print 2nd largest value from array within the C and C++ forums, part of the Software Development category; Write a function to get the values in array but it souldnt get any negative values and print the second largest value from the values in the array?...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 03-05-2008
Junior Member
 
Join Date: Feb 2008
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
gunasekarselvaraj is on a distinguished road
Print 2nd largest value from array

Write a function to get the values in array but it souldnt get any negative values and print the second largest value from the values in the array?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2008
Junior Member
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
cssprasad is on a distinguished road
Re: Print 2nd largest value from array

func()
{
int a[10];
int b;
int j;
clrscr();
for(int i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[j]>a[j+1])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
}
printf("%d\t",a[i-2]);
}

Last edited by cssprasad; 03-08-2008 at 01:25 PM.
Reply With Quote
  #3 (permalink)  
Old 03-17-2008
Junior Member
 
Join Date: Feb 2008
Location: kolkata
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
swarup_saha4 is on a distinguished road
Re: Print 2nd largest value from array

murkho!!..........................
Reply With Quote
  #4 (permalink)  
Old 03-17-2008
Contributing Member
 
Join Date: Mar 2008
Location: Pune
Posts: 30
Thanks: 0
Thanked 13 Times in 8 Posts
chennaprashanth is on a distinguished road
Thumbs up Re: Print 2nd largest value from array

Hai,
Here is the Query in SQL Server 2005, to get the Second highest Salary from a EMP Table.

select max(sal)<(select max(sal) from emp)


Regards,
Prashanth Chenna.
Reply With Quote
  #5 (permalink)  
Old 03-17-2008
Junior Member
 
Join Date: Mar 2008
Location: Kurnool
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
sreenivasuluyc is on a distinguished road
Smile Re: Print 2nd largest value from array

#include<stdio.h>
void main()
{
int i,j,n,a[20],first_max,second_max;
printf("Enter how many numbers");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter a positive number");
scanf("%d",&a[i]);
}

if(a[0]>a[1])
first_max=a[0];
second_max=a[1];
else
first_max=a[1];
second_max=a[0];

for(j=2;j<n;j++)
if(a[j]>second_max)
if(a[j]>first_max)
{
first_max=a[j];
second_max=first_max;
}
else
second_max=a[j];
printf("The second largest number is %d", second_max);
}
Reply With Quote
The Following User Says Thank You to sreenivasuluyc For This Useful Post:
  #6 (permalink)  
Old 09-21-2009
Junior Member
 
Join Date: Sep 2009
Location: Kharagpur
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
sb_geologist is on a distinguished road
Smile Re: Print 2nd largest value from array

Quote:
Originally Posted by sreenivasuluyc View Post
#include<stdio.h>
void main()
{
int i,j,n,a[20],first_max,second_max;
printf("Enter how many numbers");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter a positive number");
scanf("%d",&a[i]);
}

if(a[0]>a[1])
first_max=a[0];
second_max=a[1];
else
first_max=a[1];
second_max=a[0];

for(j=2;j<n;j++)
if(a[j]>second_max)
if(a[j]>first_max)
{
first_max=a[j];
second_max=first_max;
}
else
second_max=a[j];
printf("The second largest number is %d", second_max);
}
this is definitely the best answer.. thank you
Reply With Quote
  #7 (permalink)  
Old 10-08-2009
Junior Member
 
Join Date: Mar 2009
Location: COIMBATORE
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
sumitsolution is on a distinguished road
Re: Print 2nd largest value from array

use simple concept......
swap the largest element at first position a[0]....second largest at second position at a[1].......smallest at last ...then print a[1]...
*******************************
#include<stdio.h>
#include<conio.h>
void main(){
int a[100],n,i,t;
clrscr();
printf("enter the value of n:\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);}
for(i=0;i<n-1;i++){
if(a[i+1]>a[i]){t=a[i];
a[i]=a[i+1];
a[i+1]=t;}
}
printf("the second largest %d",a[1]);
getch();
}
*************************************
sumitsolution@gmail.com
Reply With Quote
  #8 (permalink)  
Old 10-09-2009
Junior Member
 
Join Date: Mar 2009
Location: COIMBATORE
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
sumitsolution is on a distinguished road
Re: Print 2nd largest value from array

sort the numbers in decending order.......so second element will be at a[1]......
*********************************************************
#include<stdio.h>
#include<conio.h>
void main(){
int a[100],n,i,t;
clrscr();
printf("enter the value of n:\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);}
for(i=0;i<n-1;i++){
if(a[i+1]>a[i]){t=a[i];
a[i]=a[i+1];
a[i+1]=t;}
}
printf("the second largest %d",a[1]);
getch();
}

****************************************************
sumitsolution@gmail.com
Reply With Quote
  #9 (permalink)  
Old 10-18-2009
Junior Member
 
Join Date: Oct 2009
Location: Hapur
Posts: 6
Thanks: 0
Thanked 2 Times in 2 Posts
amitgupta_knmiet is on a distinguished road
Re: Print 2nd largest value from array

Quote:
Originally Posted by cssprasad View Post
func()
{
int a[10];
int b;
int j;
clrscr();
for(int i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[j]>a[j+1])
{
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
}
printf("%d\t",a[i-2]);
}
sorry to say this i think its not right bcz its work as a swao my freind!
Reply With Quote
  #10 (permalink)  
Old 10-18-2009
Junior Member
 
Join Date: Oct 2009
Location: Hapur
Posts: 6
Thanks: 0
Thanked 2 Times in 2 Posts
amitgupta_knmiet is on a distinguished road
Re: Print 2nd largest value from array

Quote:
Originally Posted by cssprasad View Post
func() { int a[10]; int b; int j; clrscr(); for(int i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(a[j]>a[j+1]) { b=a[j]; a[j]=a[j+1]; a[j+1]=b; } } printf("%d\t",a[i-2]); }
//program to find second largest number from an array
#include<stdio.h>
#include<conio.h> void main() { int i,j,m,a[10]; clrscr(); printf("enter your ten numbers plz\n"); for(i=0;i<=9 && i>=0;i++) scanf("%d",&a[i]); for(i=0;i<=9;i++) for(j=0;j<=9;j++) if(a[i]>a[j]) { m=a[i]; a[i]=a[j]; a[j]=m; } printf("\n\nthe second largest number in the array is=%d",a[1]);
getch(); }
Reply With Quote
  #11 (permalink)  
Old 4 Weeks Ago
Junior Member
 
Join Date: Oct 2009
Location: Kolkata
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cybersandipan is on a distinguished road
Talking Re: Print 2nd largest value from array

#include<iostream>

using namespace std;

int main()
{
int a[6] = { 7, 5, 9 , 4, 8 ,0};
int n1,n2;
n1 = n2 = a[0];

for (int i = 0 ; i < 6 ; i++)
{
if( n2 < a[i])
{
if( a[i] > n1)
{
n2 = n1;
n1 = a[i];
}
else
{
n2 = a[i];
}
}
}

cout << "Second Largest number : " << n2 << endl;
return 0;
}

Last edited by cybersandipan; 4 Weeks Ago at 05:15 PM.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Print elements in array swetha_joein C and C++ 4 05-21-2008 02:21 AM
How to print an array in Richtext box prakash.kudrekar VB.NET 3 12-26-2007 05:57 AM
Customize and print your photos with pre-designed print templates. JobHelper Geeks Lounge 0 12-12-2007 06:40 AM
Find the two largest number Manojks Brainteasers 8 02-21-2007 05:28 AM
Find the largest value of the sum jamesravid Brainteasers 2 12-06-2006 03:24 AM


All times are GMT -4. The time now is 10:43 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved