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??

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / mms zubeir

void main()
{
int numbers[20];
int biggest=0, secondBiggest=0;

for(int index = 0; index < 20; ++index)
{
int input;
cin>>input;

if(input == biggest)
continue;

if(input > biggest)
{
secondBiggest = biggest;
biggest = input;
}
else if(input > secondBiggest)
secondBiggest = input;
}

cout<<endl<<"Biggest : "<<biggest<<endl<<"Second
biggest : "<<secondBiggest<<endl;

getch();
}

Is This Answer Correct ?    6 Yes 6 No

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

Answer / sujeet pardeshi

int k;
for(i=0;i<2;i++)
{
max=0;
for(j=n;j>=i;j--)
{
if(a[j]>max)
{
max=a[j];
k=j;
}
}
swap(a[i],a[k]);
}
printf("2nd highest no is:%d",max);

Is This Answer Correct ?    3 Yes 3 No

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

Answer / fahad

#include<iostream.h>
void main()
{
int a[20];
int i;
for(i=o ; i<20 ;i++)
cin>>a[i];
}
int(max=a[0];
for(i=1; i<20; i++)
{
if(a[i]>max) max=a[i];
}
cout<<"\n maimun number is ="<< max<<"\n";
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / ajit

#include<stdio.h>
void main()
{
int a[20],i,j,temp;
printf("enter the 20 values\n");
for(i=0;i<20;i++)
scanf("%d",&a[i]);
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("the second largest number is %d\n",a[18]);
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / ada

oid SecondMax(int *a)
{
int *p = a;
int i, max, smax;

smax = max = *p++;
for(i=1;i<20;i++,p++)
{
if(*p>max)
{
smax = max;
max = *p;
}
}

if(smax!=max)
{
cout<<"The second largest number is "<<smax<<endl;
}
else
{
cout<<"There's no second largest number: all elements are
equal"<<endl;
}
}

Is This Answer Correct ?    2 Yes 5 No

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

Answer / sivaraj

ya i above is correct

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More C++ General Interview Questions

What is the sequence of destruction of local objects?

0 Answers  


What type of question are asked in GE code writing test based on c++ data structures and pointers?

0 Answers  


Why isn't sizeof for a struct equal to the sum of sizeof of each member?

0 Answers  


What is general format for a prototype?

0 Answers  


Where do I find the current c or c++ standard documents?

0 Answers  


Visual Basic 6 All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. A. Discuss the various types of variables. B. How to control the file? Q2. A. How the image application? B. Write short notes on trees structures work. Q3. A. How to load and unload child forms. B. When should we use recursive programming Q4. A. Write short notes on "The ole control's shortcut menu" B. How to test the AX stat class Q5. A. How to use advanced data-bound controls. B. Write notes on manipulating the recordset object Q6. A. What is script control? B. How to down load image properties. C. What is meant by building a recordset. PROJECT 2002 All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. A. How can we determine performance standards? B. Write note on chaining views. Q2. A. Discuss the working with task in Gantt chart view. B. Explain the creation project schedule. Q3. A. What is understand in resource tracking? B. Write short notes on Caturing Baseline. Q4. A. How to customize workgroup message? B. How to use hyperlink? Q5. A. Explain the installation of project server? B. Discuss the working with to-do lists. Q6. A. Explain the working of templates? B. What is visual basic? C. Write shortnotes on user forms. PROGRAMMING WITH C++ All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A. Explain how a pointer to function can be declared in C++? B. List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header ("Include") Files. Java All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. A. Write note on "The class path Environment Variable". B. Which are different kinds of source code? Q2. A. How to create an interface? B. Why convert an applet to an application? Q3. A. How to use Media tracker Class. B. How to use string tokenizer class. Q4. A. Explain the overview of UDP messaging. B. Difference between SQL Exception class and SQL Warning class. Q5. A. How to create com object in Java? B. Write short notes on "The properties class" Q6. A. When object is created and destroyed? B. Explain the JDB in depth & command line. C. Write short notes on Web Sites. MULTIMEDIA All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q.1 Write a C language program to perform CCITT group 3 compression. If possible, scan some images and use your program to compress the images. What kind of compression did you achieve? Q2. How does a magneto-optical technology differ from WORM technology? Explain the differences in the manner in which you would use them for a multimedia system. Q3. What network considerations would you contemplate in designing an enterprise-wide multimedia system which supports fully distributed integrated messaging, sharing of corporate multimedia information databases, and custom business process applications? Q4. Explain the difference between the various types of multimedia object servers. How would you set up hierarchical storage for a large distributed organization? Q5. Explain the role of each type of server required in a multimedia system and the type of storage media that should be used for it. Q6. How do your decisions affect the design? Would different decisions on network technologies and object server technologies result in a different solution? OPERATING SYSTEM All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. What is an interrupt? How are multiple interrupts dealt with? Q2. Explain the difference between a MONOLITHIC KERNAL and a MICROKERNAL? Q3. List reasons why a Mode switch between threads may be cheaper than a Mode switch between processes. Q4. What is the difference among deadlock avoidance, detection and prevention? Q5. What items of information about a task might be useful in real time scheduling? Q6. Discuss some of the reasons for implementing process migration ? How is the process address space handled during process migration? WEB APPLICATIONS All the questions are compulsory. The first five questions shall be of 16 marks each and the last question shall be of 20 marks. Q1. Explain Java script in Web pages? Q2. Explain the advantages of Java script. Q3. Write the Properties of HTML object. Q4. How a JavaScript enabled browser handles the document object? Q5. What are cookies? Q6. Explain the process of controlling the Program flow in Pearl.

0 Answers   BirlaSoft,


Can a program run without main function?

0 Answers  


Define friend function.

0 Answers  


How the endl and setw manipulator works?

0 Answers  


What is array give example?

0 Answers  


What is null pointer and void pointer?

0 Answers  


What compiler was used?

6 Answers   Intel,


Categories