void sortArray(int arr[], int length) { for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { if (arr[j] < arr[i]) { // Swaps values int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } cout << "{"; for (int i = 0; i < length; i++) { cout << arr[i]; if (i < length - 1) cout << ", "; } cout << "}"; } // Sort Array
C++: I am trying to sort the given array in the main method but I am getting the following error:
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘void’) cout << "\nSorted Array: " << sortArray(arr, length);
Code:
void sortArray(int arr[], int length) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (arr[j] < arr[i]) {
// Swaps values
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << "{";
for (int i = 0; i < length; i++) {
cout << arr[i];
if (i < length - 1)
cout << ", ";
}
cout << "}";
} // Sort Array
int main() {
int arr[] = {3, 4, 1, 2};
int length = sizeof(arr) / sizeof(arr[0]);
cout << "\nSorted Array: " << sortArray(arr, length);
}
Step by step
Solved in 2 steps with 1 images