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. |
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?...
|
|||||||
|
|||
|
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?
|
| Sponsored Links |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
#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); } |
| The Following User Says Thank You to sreenivasuluyc For This Useful Post: | ||
|
|||
|
Quote:
|
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
Re: Print 2nd largest value from array
sorry to say this i think its not right bcz its work as a swao my freind!
|
|
|||
|
Re: Print 2nd largest value from array
Quote:
#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(); } |
|
|||
|
#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. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
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 |