1
#include <iostream>
using namespace std;
// merging two sorted array

void merge1(int a[], int b[], int m, int n)
{
  int c[m + n];
  for (int i = 0; i < m; i++)
    c[i] = a[i];
  for (int i = 0; i < n; i++)
    c[m + i] = b[i];
  sort(c, c + m + n);
  for (int i = 0; i < (m + n); i++)
    cout << c[i] << " ";
}

int main()
{
  int a[] = {10, 15, 20, 20};
  int b[] = {1, 12};
  merge1(a, b, 4, 2);
}

Error :

error: 'sort' was not declared in this scope; did you mean 'qsort'?
   21 |   sort(c, c + m + n);
      |   ^~~~
      |   qsort
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 2
    `int c[m + n];` -- This is not valid C++. Arrays in C++ must have their size denoted by a constant expression, not a runtime value. That should be: `std::vector c(m + n);`. – PaulMcKenzie Dec 17 '21 at 13:59
  • 5
    The code is missing `#include ` where `std::sort` is declared. – Eljay Dec 17 '21 at 14:00
  • did you really mean to call the std::sort function in the middle of your merge1 function? mergesort is typically implemented in a recursive way, so you probably meant to call `merge1` instead of sort? but then the parameters don't fit ... – codeling Dec 17 '21 at 14:01
  • In addition, even if you were to use `std::sort`, using non-standard variable length arrays as parameters to the algorithm functions caused compilation issues in various versions of the g++ compiler (which you are probably using). Thus you really have no choice but to use `vector` or some *standard* C++ there. – PaulMcKenzie Dec 17 '21 at 14:03
  • 2
    You should merge the two arrays without sorting the result - it receives two *sorted* arrays because it can be done without sorting. There are plenty of examples of merging both online and in books. – molbdnilo Dec 17 '21 at 14:07
  • [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Dec 17 '21 at 14:21
  • Also you should pass a[] and b[] with &a and &b. https://stackoverflow.com/a/5816766/8935178 – Awesome JSF Dec 17 '21 at 14:35

2 Answers2

0

The program works but on higher versions of clang and gcc, which implies a higher version of the standard being required. This problem can be reproduced if you drop the version of the compiler being used. If you drop the compiler version, you will need to include <algorithm> and the program will work fine.

Take a look.

My best guess is either the compiler somehow recognizes the function at compile-time, or the function is visible in higher C++ standard versions via some hidden includes in the <iostream> or headers that it includes.

You can see a similar story on clang++ side too.

Notes
  • You need to check the version of the compiler you are using and in general, observe if the function you are using is supported by the standard you are running.
  • Also, please include proper header files when you run something like this.
  • Also, please for crying out loud stop doing using namespace std;. Please.
sarthakt
  • 42
  • 1
  • 7
0

include header file for sort or use #include<bits/stdc++.h>

Sourabh
  • 44
  • 4
  • 2
    DONT. thats a non-standard header – Raildex Jul 10 '23 at 08:44
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – BoP Jul 11 '23 at 18:34
  • In addition to the `bits/stdc++.h` issue, "include header file for sort", which header exactly? This critical info should really be included in the answer. – Weijun Zhou Jul 12 '23 at 12:02