arrayName.length()?

So i started programming again after a long break, and im doing some practice problems. Here is the code i am trying to run.
1
2
3
4
5
6
7
8
9
10
11
12
	for( int i = 0; i < (people.length() - 1); i++ )
	{
		for( int j = (i+1); j < people.length(); j++ )
		{
			if( people[i] < people[j] )
			{
				temp = people[i];
				people[i] = people[j];
				people[j] = temp;
			}
		}
	}


Both the people.length() say "Error: expression must have class type".
people is initialized as int people[10];

and the user fills it in. (Which happens before the above code runs).
Last edited on
Arrays don't have member functions. You know the length is 10 so why not use it in the code, or better, create a constant that has value 10.
Or define people as std::vector<int>
If he does make it a vector he also needs to change length() to size(). ; )
Okay I got it to work but it isnt working in the right way. Here is the code i think is relevant:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int temp;

	for( int i = 0; i < (11 - 1); i++ )
	{
		for( int j = (i+1); j < 11; j++ )
		{
			if( people[i] < people[j] )
			{
				temp = people[i];
				people[i] = people[j];
				people[j] = temp;
			}
		}
	}

	for( int i = 0; i < 11; i++ )
	{
		cout << "Person " << i << ": ate " << people[i] << " pancakes." << endl;
	}


Here is the input/output:
1
2
3
4
5
6
7
8
9
Enter how many pancakes each person ate.
Person 0 ate: 5
Person 4 ate: 2
Person 7 ate: 1

(here is the output...)
Person 0: ate 11 pancakes.
Person 4: ate 5 pancakes.
Person 7: ate 3 pancakes.


That isnt the output exactly i obviously skipped some but the point is I input that person 0 ate 5, and it outputted that 0 ate 11. The amount of pancakes is in order, but they arent with the right people.
bump
You are starting at the first element and comparing it to all other element and swapping it if it a smaller value.

Also,

for( int i = 0; i < (11 - 1); i++ ) = for( int i = 0; i < 10; i++ )

But which one makes more sense?
Last edited on
Topic archived. No new replies allowed.