Write a program to find the second largest and second smallest number from a 2d array. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program to find the second largest and second smallest number from a 2d array.

Please solve this problem I used to stuck in finding second largest and second smallest. You can do it by C ,C++ or Java I try it pls check it (how to minimize steps) https://code.sololearn.com/cM22IwZ9c18A/?ref=app

17th Mar 2022, 6:16 PM
Md Saif Ali
Md Saif Ali - avatar
11 Answers
+ 1
Md Saif ali A) since it seems like you dont understand what a set is Ive adjusted the program https://code.sololearn.com/cK2H3qmUH4HN/?ref=app B) like i said thats up to you to decide in such case
18th Mar 2022, 12:03 PM
Raul Ramirez
Raul Ramirez - avatar
+ 4
Please show your attempt. One ways of solving this task could be sorting the array or 1st determining the min and the max
17th Mar 2022, 6:23 PM
Lisa
Lisa - avatar
+ 2
Lisa can we convert the 2d array into 1 d array say A of size 16( say size of 2d was 4*4) then apply bubble sort(in ascending) and then print A[1] andA[14] as second smallest and largest ? I don't think so finding min and Max will help us .
17th Mar 2022, 6:31 PM
Md Saif Ali
Md Saif Ali - avatar
+ 1
Lisa and rodwynnejones Is this approach correct? Or you may have alternative [I have displayed each matrix for ease] https://code.sololearn.com/cM22IwZ9c18A/?ref=app
17th Mar 2022, 8:16 PM
Md Saif Ali
Md Saif Ali - avatar
+ 1
Raul Ramirez 1. inputs to be provided by user. 2. Program fails when a) atleast two largest or smallest number is present b) when all elements are same
18th Mar 2022, 5:25 AM
Md Saif Ali
Md Saif Ali - avatar
+ 1
Md Saif ali 1. I just made it random for demonstration purposes 2. The program doesnt fail you just need to adjust the ouput in terms of your definition a) idk what you mean b) if all elements are the same by my definition there isnt a 2nd smallest or 2nd largest value There just exist one value
18th Mar 2022, 5:33 AM
Raul Ramirez
Raul Ramirez - avatar
+ 1
Raul Ramirez 1. Ok I got it 2. a) what will your code print for value {{1,2,3},{7,8,9},{1,9,1}} Will ur code display 2 and 8 for second smallest and largest b)If all elements is same then it is better to display "Not possible" in each case
18th Mar 2022, 10:38 AM
Md Saif Ali
Md Saif Ali - avatar
0
What if A[1] has the same value as A[0] and A[14] has the same value as A[15]?
17th Mar 2022, 7:23 PM
rodwynnejones
rodwynnejones - avatar
0
I hard coded some values for your 2D array (and some other editing to bypass the user inputs) tested with:- {{1,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; <= works as you intended as the values are unique. {{2,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}, {13, 14, 16, 16,}}; <= second smallest should be 3, but still outputs 2, second largest should be 14, but still outputs 16.
17th Mar 2022, 9:02 PM
rodwynnejones
rodwynnejones - avatar
0
rodwynnejones where should I make changes and what should be the changes.? And in case of all numbers being same ,it will show error
18th Mar 2022, 3:41 AM
Md Saif Ali
Md Saif Ali - avatar
18th Mar 2022, 4:59 AM
Raul Ramirez
Raul Ramirez - avatar