Question

In: Computer Science

Code: C++ Write a very general sort method that can sort any type of data arrays/lists....

Code: C++

Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support these features, implement a revised version (clearly state the revision you made to the problem with a brief discussion of the language’s weakness.) Then test the following cases:

(a) 4, 3, 7, 2, 1, 9 in ascending order

(b) 4.5, 2.0, 9.0, 8.4, 7.2, 6.1, 20.5, 2.1 in descending order

(c) John 40, Casey 45, Ben 47, Zadi 41, Kay 39, Tay 43 in ascending order of names

Solutions

Expert Solution

Solutions:

Integer:

In Asc order:

int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);

In Desc Order:

int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater<int>());

Doubles

In Asc order:

double arr[] = {1.32, 5.423, 8.234, 9.23, 6.123, 7.324, 3.123, 4.234, 2.235, 0.235};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);

In Desc Order:

double arr[] = {1.32, 5.423, 8.234, 9.23, 6.123, 7.324, 3.123, 4.234, 2.235, 0.235};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater<double>());

Student Sort score as Desc Order and name as Asc order;

code:

#include <bits/stdc++.h>
using namespace std;
  
struct Student
{
string name; // Given
int scores; // Scores
};
  
// Function for comparing two students according
// to given rules

bool compare(string a, string b) {
   if(a.compare(b) < 0)
   return true;
   else
   return false;
}

bool compareTwoStudents(Student a, Student b)
{   
   // here you can set priority to which parameter you want to sort first like score and name || name and score
   // currently score and name is active ( first score then name )
if (a.scores != b.scores)
return a.scores > b.scores;

return compare(a.name, b.name);
}
  
// Fills total marks and ranks of all Students
void computeRanks(Student a[], int n)
{
sort(a, a+5, compareTwoStudents);
}
  
// Driver code
int main()
{
int n = 5;
  
// array of structure objects
Student a[n];
  
// Details of Student 1
a[0].name = "Bryan" ;
a[0].scores = 80 ;
  
// Details of Student 2
a[1].name= "Kevin" ;
a[1].scores= 95 ;
  
// Details of Student 3
a[2].name = "Nick" ;
a[2].scores = 95 ;
  
// Details of Student 4
a[3].name = "AJ" ;
a[3].scores = 80 ;
  
// Details of Student 5
a[4].name = "Howie" ;
a[4].scores = 80 ;
  
computeRanks(a, n);

  
// Display details of Students
for (int i=0; i<n; i++)
{
cout << a[i].name << " " << a[i].scores << " "<<endl;
}
  
return 0;
}

// Happy hack :)

// please let me know if you have any dout.


Related Solutions

Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
A very common application of arrays is computing statistics on lists of numbers. Below you will...
A very common application of arrays is computing statistics on lists of numbers. Below you will find a template of a program that uses four user-defined methods: input an array of numbers, compute the average the array, find the largest number in the array, and find the smallest number in the array. Enter the following program into C# (ignore the line numbers) and replace the lines marked with // *** with the appropriate C# code (may require more than one...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code...
Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code is given as a separate file along with this final document, and also appended at the end of this document). Test your program that prints before sorting, sorts, and prints after sorting an Integer array, a Double array, and a String array as follows:       Integer[] dataInt = {63, 19, 65, 38, 26, 74, 27, 25, 70, 38};       Double[] dataDouble = {102.5, 1.98,...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT