organize array of objetc in OOP

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
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
#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 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?




Last edited on
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
Topic archived. No new replies allowed.