May 21, 2015 at 7:49pm UTC
hello, in my program have array of objetcs about of a atraction park in my class atraction have 2 attribute string (for the name) and integer x (fun level), I read m (quantify) of atractions n the populars atraction (atraction with more fun level)and h (I not still use, but i'll use later). the atraction organized for more fun level, (descending) and that it's ok, but if two atraction have equal fun level, the first going to the atraction that have name Lexicographical order smaller,
and in my organized I put a "if" that the compare and do it the instruction, but not compile :(
please, can you help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include <string>
using namespace std;
class atraccion {
private :
int x;
string name;
public :
atraccion () {}
atraccion (int var, string nnombre){
x=var;
name = nnombre;
}
~atraccion() {}
void set_x (int val) { //modifica el x
x = val;
}
int get_x () { //devuelve el x
return x;
}
void set_name ( string nom) { //modifica el nombre
name =nom;
}
string get_name () { //devuelve el nombre
return name;
}
};
int main (){
int m, n, h,div;
int posmayor,mayor;
string nombre;
atraccion intercambio;
cin>>m>>n>>h;
atraccion ar [m]; //arreglo que almacena cada atraccion
for (int i=0;i<m;i++) {
cin>>nombre;
cin>>div;
ar[i].set_name(nombre);
ar[i].set_x (div);
}
for (int w=0;w<=m-2;w++){ //ordenamiento //modelo 2
posmayor = w;
mayor = ar[w].get_x();
for (int k=w+1; k<=m-1; k++){
if ((ar[k].get_x() == mayor) && (ar[k].get_name < ar[w].get_name())){
mayor= ar[k];
posmayor=k;
}else {
if (ar[k].get_x() > mayor) {
mayor = ar[k].get_x();
posmayor =k;
}
}
}
intercambio = ar[posmayor]; //se encarga de cambiarlos
ar[posmayor] =ar[w];
ar[w] = intercambio;
}
for (int j=0;j<n;j++) {
cout<<ar[j].get_name()<<endl;
}
return 0;
}
Last edited on May 21, 2015 at 7:50pm UTC
May 21, 2015 at 11:17pm UTC
Line 45 is not legal c++. When declaring an array, the dimension must be a const value or use the
new
operator to allocate a dynamic array.
Line 58: The first get_name is missing the () to make it a function call.
Line 59: You can't assign an atraccion object to an int. I assume you meant:
BTW, why write your own sort, when the std:: library provides one?
http://www.cplusplus.com/reference/algorithm/sort/
All you need to do is provide your own comparison function.
Last edited on May 21, 2015 at 11:18pm UTC