Question

Write a function that gets an array as a parameter, sorts it in a descending fashion...

  1. Write a function that gets an array as a parameter, sorts it in a descending fashion and returns the number of elements that did not change position in the sort (they were originally in the right position).

int sortDESC(int* myArray, int size)

if array = { 2, 3, 1, 0, -1} after calling the function the array should become

{-1, 0, 1, 2 , 3 } and the function should return 1 (only one element has not changed place in the array)

Hint: 1.You need to make a copy of the array before sorting it!

2. and the answer need to include output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Here if we sort in ascending order then we get the output as shown in Question.

Required Function:

int sortDESC(int* myArray,int size)
{
   int orgArray[size];
   int i=0,j=0,temp=0;
   for(i=0;i<size;i++)
       orgArray[i]=myArray[i] ; // array copying

   for(i=0;i<size-1;i++)
   {
       for(j=i+1;j<size;j++)
       {
           if(myArray[i]>myArray[j]) // sorting the array in descending order
           {
               temp=myArray[i];
               myArray[i]=myArray[j];
               myArray[j]=temp;
           }
       }
   }
   int count=0;
   for(i=0;i<size;i++)
       if(myArray[i]==orgArray[i])
           ++count;

   return count;
}

Complete Source Code:

#include <stdio.h>
int sortDESC(int* myArray,int size)
{
   int orgArray[size];
   int i=0,j=0,temp=0;
   for(i=0;i<size;i++)
       orgArray[i]=myArray[i] ; // array copying

   for(i=0;i<size-1;i++)
   {
       for(j=i+1;j<size;j++)
       {
           if(myArray[i]>myArray[j]) // sorting the array in descending order
           {
               temp=myArray[i];
               myArray[i]=myArray[j];
               myArray[j]=temp;
           }
       }
   }
   int count=0;
   for(i=0;i<size;i++)
       if(myArray[i]==orgArray[i])
           ++count;

   return count;
}
int main()
{
   int size=5;
   int array[]={2,3,1,0,-1};
   int res=sortDESC(array,size);
   printf("No. of unchanged elements:%d\n",res);
}

{ 11 { 1 #include <stdio.h> 2 int sortDESC(int* myArray,int size) 3 { 4 int orgArray[size]; 5 int i=0,j=0, temp=0; 6 for(i=0;Sample input and output:

(base) rgukti@rgukt-TravelMate-P243-M:-/Desktop$ ./a.out No. of unchanged elements: 1 (base) rgukti@rgukt-TravelMate-P243-M:-

Add a comment
Know the answer?
Add Answer to:
Write a function that gets an array as a parameter, sorts it in a descending fashion...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT