Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write a program that read 2o numbers in and array and
output the second largest number. Can anybody help??

Answer Posted / tase

a more generic implementation + error checking
(observation: if we have 100 100 99 then the 2nd largest is
considered 100 and not 99)



#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>

#define NR_INPUTS 20


void print_nth_larger(int nth_larger, int *int_array)
{
int ordered_nth[NR_INPUTS];
if(nth_larger > NR_INPUTS || nth_larger < 1)
{
cout << "Illegal nth parameter, valid values are from 1 to
" << NR_INPUTS << endl;
return;
}
ordered_nth[0]=int_array[0];
for(int i=1; i < NR_INPUTS; i++)
{
for(int j = 0; j < nth_larger; j++)
{
if(int_array[i] > ordered_nth[j])//if current value from
array is greater than current from ordered
{
for(int k = nth_larger-1; k > j ; k--)
ordered_nth[k]=ordered_nth[k-1];//shifting all other
smaller values to the right by 1
//now inserting the current input number in the ordered
array of the first nth elements
ordered_nth[j] = int_array[i];
break;//getting out of this for loop as we already
inserted the value in the ordered array

}
}
}//end for for the 20 elements array
//now let's print the nth
cout << "The " << nth_larger << " larger element of the
array is: " << ordered_nth[nth_larger-1] << endl;
}


int main()
{
//we have to read 20 numbers in an array and print the
second largest
int input_numbers[NR_INPUTS]; //Allocating memory for input
data

cout << "Please input each number one by one then press
[enter]" <<endl;

char *input_data = new char[100]; //here we should either
use more or do it dynamically


for(int i=0;i < NR_INPUTS;i++)
{
bool invalid_number = true;
while(invalid_number)
{
cin >> input_data;

input_numbers[i] = atoi(input_data);
if(input_numbers[i] != 0 || strcmp(input_data, "0") ==
0)//to be able to input 0 also
invalid_number = false;
else
{
cout << "You did not put in a number, please try
again!, the input value was" << input_data << endl;
invalid_number = true;
}
}//end while
}//end for
cout << "The input numbers are: " << endl;
for(int i=0; i < NR_INPUTS; i++)
{
cout << input_numbers[i] << ", ";
}
print_nth_larger(2, input_numbers);
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is c++ runtime?

1049


What is a lambda function c++?

1031


Explain selection sorting. Also write an example.

983


What are virtual functions in c++?

1121


What is the difference between a "copy constructor" and an "assignment operator" in C++?

1016


How delete [] is different from delete?

956


describe private access specifiers?

1134


How does list r; differs from list r();?

1087


What is the this pointer?

1071


How does class accomplish data hiding in c++?

1117


What is flush c++?

920


Does c++ have arraylist?

966


What is the difference between the compiler and the preprocessor?

1049


What is the default access level?

1025


What is jump statement in C++?

1055