0% found this document useful (0 votes)
47 views

27 Sortarray

The code provided is intended to sort an integer array in descending order but contains logical errors. The task is to debug the code to pass two provided test cases by correcting the implementation without modifying the overall approach. The code provided sorts the array using a selection sort algorithm but contains an error in the closing brace of the main for loop.

Uploaded by

Tharun konda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

27 Sortarray

The code provided is intended to sort an integer array in descending order but contains logical errors. The task is to debug the code to pass two provided test cases by correcting the implementation without modifying the overall approach. The code provided sorts the array using a selection sort algorithm but contains an error in the closing brace of the main for loop.

Uploaded by

Tharun konda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*You are required to fix all logical errors in the given code.

You can click on


Compile & Run anytime to check the compilation/execution
status of the
program. You can use System.out.println to debug your code. The submitted
code should be logically/syntactically correct and pass all test cases. Do not
write the main() function as it is not required.
Code Approach: For this question, you will need to correct the given
implementation We do not expect you to modify the approach or incorporate
any additional library methods.
The function sortArray(int * arr,int len) accepts an integer array
arr of length (len>0) as an input and perform an in place sort operation
on it. The function is expected to return the input array sorted in
descending order The function compiles successfully but fails to return
the desired results due to logical errors
Your task is to debug the program to pass all the test cases
TESTCASE 1:
Input:
[23, 12, 14, 24, 21], 5
Expected Return Value:
[24, 23, 21, 14, 12]

TESTCASE 2:
Input:
[1, 1, 1, 1, 1], 5
Expected Return Value:
[1, 1, 1, 1, 1]

int * sortArray(int *arr,int len )


{
int i,max,location,j,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i+1;j<len;j++)
{
if(max<arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[location];
arr[location]=arr[i];
arr[i]=temp;
}
return arr;
}
}
*/
int * sortArray(int *arr,int len )
{
int i,max,location,j,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i+1;j<len;j++)
{
if(max<arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[location];
arr[location]=arr[i];
arr[i]=temp;
}
return arr;
}

int main()
{
//int arr[] = {23, 12, 14, 24, 21}, size, ind;
//int arr[] = {1,1,1,1,1,1,1 }, size, ind;
int arr[] = {23, 12, 14, 24, 21, 45, 78, 66, 33, 24, 21}, size, ind;
size = sizeof(arr) / sizeof(arr[0]);
sortArray(arr, size);
for(ind = 0; ind < size; ind++)
printf("%d ", arr[ind]);

return 0;
}

// Errors
// Remove the last {

You might also like