The problem I am facing is that I cant get the number of pancakes eaten to be arranged with respective person. I only sort the pancakes. In other words, I have to modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes
My code is here:
Hello, I'm new to C++ as well, so... Anyway, I hope this helps.
You can store the person number and pancake number in a 2D array like a 2xN matrix.
1 2 3 4 5 6
int personpancake[2][N];
int i;
for(i=1;i<=N;i++){
cout<<"Number of pancakes eaten by person"<<personpancake[1][i-1]<<endl;
cin>>personpancake[2][i-1];
}
This will store the number of pancakes eaten by person x as personpancake[2][x-1].
Note the array starts from 0.
Then you can link your person number to pancake number.
You only need a single dimension array. The n-th element of the array is the integer j.
In this example you have an array of 10 person's with each person element, person[k], k = 0 ... 9 inclusive, storing the number of pancakes p which varies for each person.
So person[k] = p pancakes. k is like a person's ID.
person[] = {9,3,7,2,8,0,4,1,6,5} means person[0] eats 9 pancakes, person[1] eats 3 etc.
What he means is that the index of the array can be used as the person #, while the contents of the array (i.e. the number at that index) can be the number of pancakes eaten by that person.
So pancakes[2] = 15; can be interpreted as "Person #2 ate 15 pancakes".
You can just use a single array this way or if you want you can use parallel arrays as Thomas has done and store the person"s name. The key here is the index number must be kept in sync and is effectively the person # or ID
The problem I am facing is that I cant get the number of pancakes eaten to be arranged with respective person. I only sort the pancakes. In other words, I have to modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
You guys are telling the OP that his problem is a problem.
To the OP, you can create an array of indexes and sort that array on the basis of the content in the array tracking how many pancakes a person at, then use that sorted array of indexes to access the original array and display them in sorted order.
Alternatively, you can make the original array an array of structs which contain properties indicating the person number and the number of pancakes eaten and sort that based on the number of pancakes eaten.
That question mark was the only one one In the OP and his follow up question a couple of posts further down.
So you misread. I shrug in your general direction.
Please avoid taking shots at other people's positive contributions Cire.
When you make such contributions I will avoid "taking shots" at them. In the meantime, if someone points out that you are not addressing the issue that needs addressing, perhaps instead of taking offense, you can consider that a constructive criticism and focus your input accordingly where such is warranted.
The only person who's made a relevant contribution in this thread is Thomas1965. Sorting one array with pancake-eaten data and one array with ids in parallel is a valid solution to the problem. Unfortunately, you intimated to the OP that it was not. Maybe you were the one taking a shot? ;)
ADHD is another one of your socio-pathologies by the look of it Cire?
Once again you have been led by your negative demeanour and incorrectly attribute my comment to Thomas time-overlapped response in an attempt to curry favor.
Everyone of the contributions to this thread are relevant and positive except for your 'par for the course' bullying behavior.
So pancakes[2] = 15; can be interpreted as "Person #2 ate 15 pancakes".
No error at all. Absolutely accurate as shown below.
The alleged problem arises because #2 (or [2] if you like) is the third element of the array and not the second, but that is not the point originally being addressed. The point being addressed at the time was, using a single dimension array, how each persons eating habits can be (uniquely) identified.
There is no need to subtract 1, the common pitfall in using arrays elsewhere. There are 10 elements (people) but the indices run from 0..9 inclusive as most of us know. It is quite right and commendable to mention it but not right to say the interpretation given by Arslan was wrong.
(The supplementary follow on issue separately posted about is how to solve the sorting problem but that wasn't the point at the time either as it wasn't what the OP raised. Unless the OP asks it is not unreasonable to expect OP wants to solve the tutorial problem under his/her own steam.)
:)
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
int person[] = {9,3,15,2,8,0,4,1,6,5}; //<-- note third element vs previous
for (int i = 0; i < sizeof(person)/sizeof(int); i++)
{
std::cout << "Person ID#" << i << " ate " << person[i] << " pancakes.\n";
}
}
Person ID#0 ate 9 pancakes.
Person ID#1 ate 3 pancakes.
Person ID#2 ate 15 pancakes.
Person ID#3 ate 2 pancakes.
Person ID#4 ate 8 pancakes.
Person ID#5 ate 0 pancakes.
Person ID#6 ate 4 pancakes.
Person ID#7 ate 1 pancakes.
Person ID#8 ate 6 pancakes.
Person ID#9 ate 5 pancakes.
Program ended with exit code: 0
After reading all the posts, I get further confused. So how can I assign pancakes eaten (pancakes[i]) to the respective person (person[x])?
Up to now, I have that pancakes are sorted in descending order, but person is in ascending order without taking into account which one (x) ate i pancakes. Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main() {
int pancakes[11];
int i;
int Person[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i=1; i<=10; i++) {
cout << "Enter the number of pancakes eaten by a person " << Person[i] << ": ";
cin >> pancakes[i];
}
sort(pancakes + 1, pancakes + 11, std::greater<int>());
cout << "Sorted Array looks like this:" << endl;
for (i = 1; i <= 10; i++) {
cout << "Person " << Person[i] << ": ate " << pancakes[i] << " pancakes" << endl;
}
}
Enter the number of pancakes eaten by a person 1: 1
Enter the number of pancakes eaten by a person 2: 22
Enter the number of pancakes eaten by a person 3: 12
Enter the number of pancakes eaten by a person 4: 13
Enter the number of pancakes eaten by a person 5: 15
Enter the number of pancakes eaten by a person 6: 14
Enter the number of pancakes eaten by a person 7: 13
Enter the number of pancakes eaten by a person 8: 5
Enter the number of pancakes eaten by a person 9: 7
Enter the number of pancakes eaten by a person 10: 8
Sorted Array looks like this:
Person 1: ate 22 pancakes
Person 2: ate 15 pancakes
Person 3: ate 14 pancakes
Person 4: ate 13 pancakes
Person 5: ate 13 pancakes
Person 6: ate 12 pancakes
Person 7: ate 8 pancakes
Person 8: ate 7 pancakes
Person 9: ate 5 pancakes
Person 10: ate 1 pancakes
Since you are using parallel arrays, you need to sort both in the same way. I would suggest using bubble sort. Every time you swap( pancakes[i], pancakes[i+1] ) you should also swap( person[i], person[i+1] )
#include <iostream>
usingnamespace::std;
constint SIZE = 4; // <-- change this here to suit
int main()
{
int Person[SIZE];
int List[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the number of pancakes eaten by a person " << i << ": ";
cin >> Person[i];
}
//FIRST ITERATION
int least_pancakes = Person[0];
int least_i = 0;
for (int i = 1; i < SIZE; i++)
{
if ( Person[i] < least_pancakes)
{
least_pancakes = Person[i];
least_i = i;
}
}
List[0] = least_i;
std::cout << "ID of least " << List[0] << " no. eaten: " << Person[List[0]] << '\n';
}
This is a similar way of doing it from first principles. But I have shown only the first iteration. List keeps track of the person ID's not the number of pancakes and the full list is built up progressively on each iteration.
person[] = {9,3,7,2,8,0,4,1,6,5} means person[0] eats 9 pancakes, person[1] eats 3 etc.
The problem with this is how do you get from this to printing the people in order of the number of pancakes written. You can't sort the array because you'd lose the connection between ID and pancakes. Kemort shows how to handle this in a later post.
I think it makes more sense to store the person number and number of pancakes eaten in a struct. Then you can sort by number of pancakes:
This particular pancake problem has a number of traditional permutations and any advice relies on the actual/original question as follows:
Alternative Q1: If all the pancake eaters and their eating habits are to be sorted on the quantity eaten then it can only be practically solved with a struct (or multidimensional array). Multiple single dimension parallel arrays make this job very tedious and impractical/impossible. Lines 10 - 12 of dhayden solution are critical.
Alternative Q2: If the job is to find the least (or most) then two single dimension arrays do the job with just one iteration required
Alternative Q1 covers all bases, alternative Q2 doesn't.