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
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);
}
Sample input and output:
Write a function that gets an array as a parameter, sorts it in a descending fashion...
A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...
in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can verily your subscription Sign In ASSIGNMENT Unit 9 Methods, Arrays, and the Java Standard Class Library Reimplementing the Arrays class (60 points) The Arrays class, which is also part of the Java standard class library, provides various class methods that perform common tasks on arrays, such as searching and sorting. Write a public class called MyArrays, which provides some of the functionality which is...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
16. (5 points) Trace counting sort algorithm as it sorts the array into a descending order. ASSUMPTION: (1) Each data element (i.e., aſil) is associated with a complicated object. As discussed, counting sort with a simple frequency count does not work. (2) Show how to guaranted the stability of repeating values. As we did in the class, please use a subscript to specify the order of the same keved values. (3) Show the steps as detail as possible. A =...
Java code com/extra/ThirdPartyProviders/FLVS/32959 AP CompSciv9_CA/module17/rtfmod17/17.03 Assig Assessment Instructions Instructions: For this assessment, you are going to create some sorting methods that use 1. Create a folder called Assessment in your unit 7 assessments folder. Copy selection sort. your movie class Movie.java to your Assessment folder. You are to create a class called TestMovie3 and save it as TestMovie3.java. 2. Make sure that you create an array called myMovien. Add the following movies: Title Year Studio ts Take Manhattan 2001 Columbia...
Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...
Javascript function that takes an array parameter and returns an array value. The assumption is that the array is an integer array. The returned array should be composed of the square of the elements in the array parameter for the corresponding position.
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...