sorting - C++ Insertion Sort: error when passing values between functions -
so i'm coding program class, in c++, ask user input size of array, , numbers inside of it. asks user if wish have sorted in ascending, or descending order.
i have tested each of sorting algorithms individually, , both seem work flawlessly, however, when combine them in sortarr function , pass values entered user main function, monstrous error cant decipher. know problem program is?
#include <iostream> #include <stdio.h> using namespace std; void sortarr(bool order, int size) { int imax; int max; int arr[size]; if (order == true) { (int = size - 1; > 0; i--) { max = arr[0]; imax = i; (int j = 0; j < i; j++) { if (arr[j] > arr[imax]) { max = arr[j]; imax = j; } } if (imax != i) { int temp = arr[i]; arr[i] = arr[imax]; arr[imax] = temp; } } } else if (order == false) { (int = 0; < size; i++) { max = arr[0]; imax = i; (int j = size - 1; j > i; j--) { if (arr[j] > arr[imax]) { max = arr[j]; imax = j; } } if (imax != i) { int temp = arr[i]; arr[i] = arr[imax]; arr[imax] = temp; } } } } int main() { int size; int a; bool order; string output; cout << "enter size of array: "; cin >> size; if (size < 0) { cout << "error: entered incorrect value array size!" << endl; return 1; } int arr[size]; cout << "enter numbers in array, seperated space, , press enter: "; (int = 0; < size; i++) { scanf("%d", &arr[i]); } cout << "sort in ascending (0) or descending (1) order? "; cin >> a; if (a == 0) { order = true; cout << "this sorted array in ascending order: "; } else if (a == 1) { order = false; cout << "this sorted array in descening order: "; } output = sortarr(order, size); cout << output << endl; return 0; } cout << "sort in ascending (0) or descending (1) order? "; cin >> a; if(a == 0){ order = true; cout << "this sorted array in ascending order: "; } else if(a == 1) { order = false; cout << "this sorted array in descening order: "; } output = sortarr(order, size); cout << output << endl; return 0; } the error code i'm receiving this:
sortarray1.cpp: in function 'int main()': sortarray1.cpp:84:9: error: no match 'operator=' (operand types 'std::string {aka std::basic_string<char>}' , 'void') output = sortarr(order, size); ^ sortarray1.cpp:84:9: note: candidates are: in file included /usr/include/c++/4.8/string:52:0, /usr/include/c++/4.8/bits/locale_classes.h:40, /usr/include/c++/4.8/bits/ios_base.h:41, /usr/include/c++/4.8/ios:42, /usr/include/c++/4.8/ostream:38, /usr/include/c++/4.8/iostream:39, sortarray1.cpp:1: /usr/include/c++/4.8/bits/basic_string.h:546:7: note: std::basic_string<_chart, _traits, _alloc>& std::basic_string<_chart, _traits, _alloc>::operator=(const std::basic_string<_chart, _traits, _alloc>&) [with _chart = char; _traits = std::char_traits<char>; _alloc = std::allocator<char>] operator=(const basic_string& __str) ^ /usr/include/c++/4.8/bits/basic_string.h:546:7: note: no known conversion argument 1 'void' 'const std::basic_string<char>&' /usr/include/c++/4.8/bits/basic_string.h:554:7: note: std::basic_string<_chart, _traits, _alloc>& std::basic_string<_chart, _traits, _alloc>::operator=(const _chart*) [with _chart = char; _traits = std::char_traits<char>; _alloc = std::allocator<char>] operator=(const _chart* __s) ^ /usr/include/c++/4.8/bits/basic_string.h:554:7: note: no known conversion argument 1 'void' 'const char*' /usr/include/c++/4.8/bits/basic_string.h:565:7: note: std::basic_string<_chart, _traits, _alloc>& std::basic_string<_chart, _traits, _alloc>::operator=(_chart) [with _chart = char; _traits = std::char_traits<char>; _alloc = std::allocator<char>] operator=(_chart __c) ^ /usr/include/c++/4.8/bits/basic_string.h:565:7: note: no known conversion argument 1 'void' 'char'
you have 2 variables named "arr": 1 in main , 1 in sortarr.
you're reading 1 in main , sorting 1 in sortarr (which uninitialised, program undefined).
pass array parameter:
void sortarr(bool order, int arr[], int size){ there's issue of attempting assign non-existent return value (void) of sortarr std::string.
don't that.
call function this:
sortarr(order, arr, size); (int arr[size]; relying on g++ extension – variable length arrays – non-standard. consider using std::vector. it's been around couple of decades , nothing fear more.)
Comments
Post a Comment