Informatics3-2024/Homework7

A MathWikiből

Send an email with the solutions as attachments (.c, .cpp or if you have .h and .hpp files) to the following email address: tofihomework+2024info3en@gmail.com

You shouldn't attach the compiled programs. If you feel like you're sending a lot of files (5+) you can put them in a zip, but you don't need to.

Template sort

Write a template function to sort an array of arbitrary type in ascending order, provided that the type has an ordering (i.e. it has the right operator<). It doesn't matter what sorting algorithm we use, but it's important not to change the input array, but to create a new dynamically reserved array, sort it and return it.

You can test this with the following main function:

int main(void) {
  int a[] = {5, 2, 1, 7, -2};
  float b[] = {1.3, -0.2, 2.4, 10.2, -11.4};
  int* a_sorted = sort<int>(a, 5);
  float* b_sorted = sort<float>(b, 5);
 
  for(int i = 0; i < 5; i++) {
	cout << a_sorted[i] << " ";
  }
  cout << endl;
  for(int i = 0; i < 5; i++) {
	cout << b_sorted[i] << " ";
  }
  cout << endl;
 
  delete[] a_sorted;
  delete[] b_sorted;
 
  return 0;
}
Személyes eszközök