0

Help fix my error on c langauge

Write a function to find the largest number and the second largest number from an array. However, a function cannot return multiple values. We need to use pointers to implement the function as follows: void find_two_largest(int a[], int n, int *largest, int *second_largest); When passed an array a of length n , the function will search a for its largest and second -largest elements, storing them in the variables pointed to by largest and second_largest , respectively. The input is array’s size followed by the elements in the array. The maximum size of the array is 10. For example: 4 5 6 7 8 9 The ou tput should be: Largest number: 9 Second largest number:8 this is my try https://code.sololearn.com/c98A85A1a17a I want the answer as a code

2nd Mar 2021, 2:01 PM
STOP
STOP - avatar
2 Respuestas
+ 1
Your logic is incorrect. Here, if(a[i]> a[j]) { a[i]= *largest; this will assign undefined values to a[i],a[j] next because values are not assigned still to pointers.. If you trying a[i] to *largest then it must be like *largest = a[i]; *second = a[j]; But actually, you need condition is if(a[i] > *largest ) //for largest finding... { *largest=a[i]; } Other simple way ,sort array first by if(a[i]<a[j] ) { //swap values between a[i],a[j]. } a[0] will be largest and a[1] will be 2nd largest.. a[j]= *second_largest; }
2nd Mar 2021, 2:16 PM
Jayakrishna 🇮🇳
2nd Mar 2021, 3:51 PM
visph
visph - avatar