answersLogoWhite

0


Best Answer

Improved Solution to this problem would be able to find any first, second, third.. largest number...

here it is, in Java:

class Sam {

public static void main(String args[]) {

int[] nums = new int[] { 445, 43, 1045, 110, 209, 109, 33, 24, 1566 };

int cnt = 0;

int size = nums.length;

int temp;

int largest = 0;

int secondLargest = 0;

int thirdLargest = 0;

int fourthLargest = 0;

int fifthLargest = 0;

//the logic rests here

while (cnt < size) {

temp = nums[cnt];

if (temp > largest) {

secondLargest = largest;

largest = temp;

} else if (temp > secondLargest) {

thirdLargest = secondLargest;

secondLargest = temp;

} else if (temp > thirdLargest) {

fourthLargest = thirdLargest;

thirdLargest = temp;

} else if (temp > fourthLargest) {

fifthLargest = fourthLargest;

fourthLargest = temp;

} else if (temp > fifthLargest) {

fifthLargest = temp;

}

cnt++;

}

System.out.println("largest:" + largest);

System.out.println("secondLargest:" + secondLargest);

System.out.println("thirdLargest:" + thirdLargest);

System.out.println("fourthLargest:" + fourthLargest);

System.out.println("fifthLargest:" + fifthLargest);

}

}

// Regards,

// SwapniM

// mail me at: swap.masane@gmail.com

#include

using std::cin;

using std::cout;

using std::endl;

double maxFirst(const double data[], intindex);

double maxSecond(const double data[], intindex, double maxNumber1);

double sum(double max1, double max2);

int main()

{

double myArray[] = {

1.0,

4.6,

32.1,

9.7,

41.2,

41.3,

343,

23,

566.02,

345.8,

675.5,

654.4

};

int arraySize = (sizeof myArray)/(sizeof myArray[0]);

cout << "Your array is: ";

for (int k = 0; k < arraySize; k++)

{

cout << endl << myArray[k];

}

double max1 = maxFirst(myArray, arraySize);

double max2 = maxSecond(myArray, arraySize, max1);

cout << endl << "First biggest number is: " << max1;

cout << endl << "Second biggest number is: " << max2;

system("PAUSE");

return 0;

}

double maxFirst(const double data[], intindex)

{

double maxNumber1 = data[0];

for (int i = 0; i < index; i++)

{

if (maxNumber1 < data[i])

{

maxNumber1 = data[i];

}

}

return maxNumber1;

}

double maxSecond(const double data[], intindex, double maxNumber1)

{

double maxNumber2 = data[1];

for (int j = 0; j < index; j++)

{

if (data[j] == maxNumber1)

{

continue;

}

else if (maxNumber2 < data[j])

{

maxNumber2 = data[j];

}

}

return maxNumber2;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Because you can't use built in PHP functions to sort the array, you need to loop through the array and find the second largest number.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program to find the second largest number in a given set of numbers using arrays without sorting?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program for sorting for numbers without repited values?

Any number variable is extends the Number in java and it already implemented Comparable interface. So when you have a list of numbers, just use Arrays.sort().


Sorting an array in PHP without using sort function?

plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++


What is the largest ten digit number without repeating the numbers?

9,876,543,210 9876543210


What is the Largest 4 digit odd number without repeating numbers?

9875.


What is the largest 9-digits numbers without digit same?

It is 987654321.


What is the largest 4 digit in which no number is repeated?

9,876 since these are the largest single digit numbers that can be done without repeat.


What is the definition of internal sorting?

Sorting that is accomplished entirely in memory without using disks or tapes for temporary files.


The largest known prime number is equal to how many inches?

Prime numbers are pure numbers, without units or dimensions.


How can you accept sum and print numbers without creating variables?

It is very easy. The program begins here..... /*Program to sum and print numbers without creating variables*/ #include&lt;stdio.h&gt; main() { clrscr(); printf("%d+%d=%d",5,2,5+2); getch(); } /*Program ends here*/ Now just by changing the numbers in the "printf" statement we can add, subtract, multiply and divide the numbers without using variables. Hence the problem is solved..........


What is the largest 5 digit number with 3 in the thousands place using numbers 1 through 9 without repeating the numbers?

93,876


What is use of sorting algorithm?

sorting means arranging a list of numbers or elements in an order (ascending or descending).


What is the largest 5 digit even number you can make with a 3 in the thousands place without repeating numbers?

93876