Click here to Skip to main content
15,789,840 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
1.Given an array (containing at most 1000 positive integers),find the second largest integer.If there exists no second largest integer,return,-1.

Input Specifications:
Input1:length of the array
Input2:array of positive integers

Output Specification:
Return the second largest number or -1 accordingly.

// Read only region start

int SecondLargest(int input1,int input2[])
// Read only region end
//write code here

Void print2largest(int arr[],int arr_size)

What I have tried:

I have tried a lot,but getting lot of errors
Updated 7-Feb-19 21:19pm
OriginalGriff 7-Feb-19 10:00am    
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Stefan_Lang 7-Feb-19 10:37am    
This is not a good question for all the reasons stated in the QA guidelines and more.

The task given to you obviously inlcudes the code up to the line "//Read only region end", so you didn't actually provide any usable code at all.

I don't see any indication that you've "tried a lot" as you claim, since you didn't even hint at the errors you were getting.

If you hope for someone providing the full answer, no, we don't do that: you are supposed to do your homework yourself because only then do you have a chance to actually learn something! We can offer help if you are stuck with a _specific_ problem, but up to now you've been as unspecific as can be.
Patrice T 7-Feb-19 10:48am    
"I have tried a lot,but getting lot of errors"
Show your work and the errors you get.
Richard MacCutchan 7-Feb-19 10:54am    
You need to complete the section where it says //write code here.
Rick York 7-Feb-19 16:20pm    
No. I'll let you answer that question.

Maybe you are used to Java. First of all you cannot pass an array like that in C. in C

int arr1[10];
int arr2[20];

Are two different types and arr[] is no type at all that is why your code does not compile.

Here is an example of how you can pass an array: Passing array to function in C programming with example[^]

Then you can start with getLargest(int size, int *arr) ...

I suggest you avoid sorting, costs a lot of CPU.

Without sorting you can implement getSecondLargest(int size, int largest, int *arr) with a bit of thinking

And you might want pretty code and be consistent. Not one function with upper-case and one with lower-case.
Share this answer
Stefan_Lang 7-Feb-19 10:43am    
int arr[] is indeed a valid function parameter specification in C and C++, and it's _not_ the same as int *arr! (although for the most part it behaves the same)

Btw., the code listed appears to have been provided up to and including the line "//Read only region end", and if you look upwards you'll find the function header
int SecondLargest(int input1,int input2[])
which is perfectly legal C code.
Stefan_Lang 7-Feb-19 10:55am    
"Not one function with upper-case and one with lower-case."
Looks like one was provided by the tutor or teacher, but not the other. Not that it shouldn't be consistent anyway, but that is wishful thinking at this level of homework ;-)
CPallini 7-Feb-19 16:54pm    
I bet it is not exactly the code provided by the tutor. Or, at least, I know of no C tutor who would write void capitalized.
Stefan_Lang 8-Feb-19 5:34am    
That's what I meant by "one was provided by the tutor or teacher, but not the other". The 'readonly region' apparently was provided.
Do not sort the array because of the cost and it is not needed. Run over the array for the search of the value. Set the initial value of max2 (second biggest value) of -1.

the solution is quite easy, when you find a new mxaimum than max2 is the old maximum.
Share this answer
CPallini 8-Feb-19 3:20am    
Now, this solution make sense. My 5.
Ok here is one approach:
Sort the array smallest to largest(use google if you are unsure how to sort an array of ints)
Now get the next to last element of the array
and presto, Bob's you uncle
Share this answer
Stefan_Lang 7-Feb-19 10:45am    
Looks like homework - just read the comments in the supplied code.
Given the simplicity of the task I would not assume the OP knows how to sort (or at least write or use code for sorting). Not to mention that it's needlessly wasteful.
RmcbainTheThird 7-Feb-19 11:26am    
If you don't sort then that means you are going to have to use a for loop twice, once to find the largest number and then a second time to find the next largest.
Stefan_Lang 8-Feb-19 5:41am    
I don't follow that logic. Are you saying that two for loops are more complex than a sorting algorithm? Also, after sorting you can't just take the second last value - if the largest value has duplicates in your input array, the second last value is the same as the last. You will have to run a loop to find the first value that is actually smaller..
megaadam 8-Feb-19 6:16am    
Looping twice sure is cheaper than sorting! But you can also do it in a single loop with two locals, as seen above by KarstenK.
Stefan_Lang 8-Feb-19 8:33am    
Sure thing. I was only complaining about the sorting advice probably not being appropriate for a homework level question.

I did have a similar solution in mind but decided not to post it because I was still hoping for the OP to at least try and post something worthwhile under "what have I tried" ;-).
Sort the array largest to smallest then iterate through the array returning the first number which is less than the number at the beginning of the array - if no such number exists (ie all the numbers are the same) return -1
Share this answer
Stefan_Lang 7-Feb-19 10:51am    
Good of you to think of possible duplicates, but otherwise my comment to solution 1 applies here as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900