Hi good afternoon, well my program is about of a atraction park (like disneyland) where any atraction have 2 values, 1 string with their name and the fun level, example:
roller_coster 10
terror_house 80
crazy_cups 30
my program read 3 things, m (quantity) n (the most popular atraccion, are that atraction with more fun level) and h (this part still don't use now in my program)
in my program read the atraction until m , but I have to organize the atraction for popular n;
example:
roller_coster 10
terror_house 80
crazy_cups 30
exit:
terror_house
crazy_cups
roller_coaster
but the momnet to run my program don't do nothing, read the name and fun level but no show me something , please can you help me with this program?
I use the selection sort for organize the array, so I don't know if the code is good, but I based in these way for the organize it
#include <iostream>
#include <string>
usingnamespace 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 indexmax,aux;
string nombre;
atraccion ar [100000]; //arreglo que almacena cada atraccion
cin>>m>>n>>h;
for (int i=0;i<=m;i++) {
cin>>nombre;
cin>>div;
ar[i].set_name(nombre);
ar[i].set_x (div);
}
for(int i=0;i<=100000-1;i++){
indexmax=i;
for (int j=i+1;j<100000;j++){
if (ar[j].get_x() > ar[indexmax].get_x()){
indexmax= j;
}
if (i != indexmax){
aux= ar[i].get_x();
ar[i].set_x(indexmax);
ar[indexmax].set_x(aux);
}
}
}
for (int j=0;j<=n-1;j++) {
cout<<ar[j].get_name()<<endl;
}
return 0;
}
Line 38: It's clear what m is. It's not clear what n and h represent. Use meaningful names.
Line 41: 100,000 attractions? An amusement park can't possibly have 100,000 attractions. If you don't know how many in advance, use a std::vector.
Line 45,52,54,66: Your for loops are faulty. You're going to have one extra iteration of each loop because the condition is <=. The termination condition should be <.
Lines 52,54: Why is your upper limit 100,000 when you asked the user for the number of attractions (m)? You're going to be sorting garbage entries. All entries past ar[m-1] are uninitialized garbage.
Line 66: Why are you using n here? The number of attractions has not changed. Why would m and n be different?
n and h I expline before, n (the most popular atraction) and h is a value that i will use more later,
100,000 because in my homework tell 1<=M<=100000, 1<=N<=10 , sorry I forget tell that, and for screen show the n popular atraction , if i have 6 and 2 popular atracions , the screen show these 2 popular atraccions
the loops, ok thank you I will change of <= to <
how i don't know the quanty of m ,I put 100,000 upper limit for the move for the array