my work doesn't compile

Answered
#include <iostream>

#define MAX_ELEM 10
#define MIN_NUM -100
#define MAX_NUM 100

void ordina(int v[MAX_ELEM], int n_elem, bool (*f)(int, int));
void print_vector(int v[MAX_ELEM], int n_elem);

bool by_max(int a, int b);
bool by_min(int a, int b);
bool by_sign(int a, int b);

int main() {
int vett[MAX_ELEM];
int i;

for (i = 0; i < MAX_ELEM; i++) {
vett[i] = MIN_NUM + ((int)random() % (MAX_NUM - MIN_NUM + 1));
}

std::cout << "Vett (original)" << std::endl;
print_vector(vett, MAX_ELEM);

ordina(vett, MAX_ELEM, by_min);
std::cout << "Vett (by_min)" << std::endl;
print_vector(vett, MAX_ELEM);

ordina(vett, MAX_ELEM, by_max);
std::cout << "Vett (by_max)" << std::endl;
print_vector(vett, MAX_ELEM);

ordina(vett, MAX_ELEM, by_sign);
std::cout << "Vett (by_sign)" << std::endl;
print_vector(vett, MAX_ELEM);

return 0;
}

void ordina(int v[MAX_ELEM], int n_elem, bool (*f)(int, int)) {
int i = 0, j;

for(; i < n_elem - 1; i++) {
for (j = i; j < n_elem; j++) {
// ascendente: v[i] > v[j] => true
// discendente: v[i] < v[j] => true
if ( f(v[i],v[j]) ) { //f() => true se devo fare lo scambio
// v[i] XOR v[j]
v[i] ^= v[j];
// v[i] = 010 XOR 011 = 001
v[j] ^= v[i];
// v[j] = 011 XOR 001 = 010
v[i] ^= v[j];
// v[i] = 001 XOR 010 = 011
}
}
}
}

void print_vector(int v[MAX_ELEM], int n_elem) {
for (int i = 0; i < n_elem; i++) {
std::cout << v[i];
if (i < n_elem - 1) std::cout << " ";
else std::cout << std::endl;
}
}

// true se a > b => ordinamente ascendente
bool by_max(int a, int b) {
return (a > b);
}
// true se a < b => ordinamente discendente
bool by_min(int a, int b) {
return (a < b);
}
//true se a e b non hanno lo stesso segno
bool by_sign(int a, int b) {
return (a*b < 0);
}



====================[ Build | Lecture4_ordinaFN | Debug ]=======================
"C:\Program Files\JetBrains\CLion 2020.2\bin\cmake\win\bin\cmake.exe" --build "C:\Users\Giuseppe\Desktop\SECONDO SEMESTRE\ALGORITMI&CALCOLATORI\progetti algoritmi\Lecture4_ordinaFN\cmake-build-debug" --target Lecture4_ordinaFN -- -j 6
[ 50%] Building CXX object CMakeFiles/Lecture4_ordinaFN.dir/main.cpp.obj
C:\Users\Giuseppe\Desktop\SECONDO SEMESTRE\ALGORITMI&CALCOLATORI\progetti algoritmi\Lecture4_ordinaFN\main.cpp: In function 'int main()':
C:\Users\Giuseppe\Desktop\SECONDO SEMESTRE\ALGORITMI&CALCOLATORI\progetti algoritmi\Lecture4_ordinaFN\main.cpp:19:35: error: 'random' was not declared in this scope
vett[i] = MIN_NUM + ((int)random() % (MAX_NUM - MIN_NUM + 1));
^~~~~~
C:\Users\Giuseppe\Desktop\SECONDO SEMESTRE\ALGORITMI&CALCOLATORI\progetti algoritmi\Lecture4_ordinaFN\main.cpp:19:35: note: suggested alternative: 'rand'
vett[i] = MIN_NUM + ((int)random() % (MAX_NUM - MIN_NUM + 1));
^~~~~~
rand
mingw32-make.exe[3]: *** [CMakeFiles\Lecture4_ordinaFN.dir\build.make:82: CMakeFiles/Lecture4_ordinaFN.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/Lecture4_ordinaFN.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/Lecture4_ordinaFN.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:137: Lecture4_ordinaFN] Error 2

1
1 comment

Hello!

This question is not related to CLion functionality. You need to check and correct your code.

0

Please sign in to leave a comment.