0
How can I access the position of an array but not the value?
I am trying to match my output to this: Person 4: ate 10 pancakes Person 3: ate 7 pancakes Person 8: ate 4 pancakes ... Person 5: ate 0 pancakes How Can I match this? What I think is, it has to do with specifying the position of the value in the array. That way I can accurately display the value and the position. I am looking for hints as opposed to the answer. More of a explanation to get me thinking in the right direction so that I can figure it out. https://code.sololearn.com/cd6zR7VPd8Ie/?ref=app
14 Respostas
+ 7
Very interesting idea Ipang!
#include <iostream>
using namespace std;
int main() {
// Number of Pancakes eaten array
int pancake[10];
int person[10]; // <----
// Temporary value for sorting array
int temp;
// user input placeholder for populating array
int pancakeEaten;
// Populate the array based on user input for the length of the array.
for (int i = 0; i < sizeof(pancake) / sizeof(pancake[0]); i++) {
cout << "How many pancakes did person " << i + 1 << " eat?\n";
cin >> pancakeEaten;
pancake[i] = pancakeEaten;
person[i] = i + 1; // <----
}
// Compare the array based on who ate the most pancakes Greatest to Least
for (int i = 0; i < sizeof(pancake) / sizeof(pancake[0]); i++) {
for (int j = i + 1; j < sizeof(pancake) / sizeof(pancake[0]); j++) {
if (pancake[i] < pancake[j]) {
temp = pancake[i];
pancake[i] = pancake[j];
pancake[j] = temp;
// swaping... <-----
person[i] ^= person[j];
person[j] ^= person[i];
person[i] ^= person[j];
}
}
}
cout << "Greatest to least\n";
// Print all values of array
for (int i = 0; i < sizeof(pancake) / sizeof(pancake[0]); i++) {
cout << "Person " << person[i] << " ate " << pancake[i] << endl; // <----
}
return 0;
}
Sample output:
How many pancakes did person 1 eat?
4
How many pancakes did person 2 eat?
8
How many pancakes did person 3 eat?
3
How many pancakes did person 4 eat?
6
How many pancakes did person 5 eat?
4
How many pancakes did person 6 eat?
9
How many pancakes did person 7 eat?
7
How many pancakes did person 8 eat?
0
How many pancakes did person 9 eat?
1
How many pancakes did person 10 eat?
2
Greatest to least
Person 6 ate 9
Person 2 ate 8
Person 7 ate 7
Person 4 ate 6
Person 1 ate 4
Person 5 ate 4
Person 3 ate 3
Person 10 ate 2
Person 9 ate 1
Person 8 ate 0
+ 9
Just an idea, switch the array into a two dimensional array, so that it would be 10 rows with 2 columns each. The first column stores the person number (1~10), the second stores the amount of pancakes eaten.
The person number will be generated from input loop iterator incremented by one. When you sort the array, swap the person number along with the number of pancakes eaten by the person, this way the array is sorted by pancakes eaten, but the person number follows the row's repositioning. When you output the array you have the person number and respective pancakes eaten at hand.
Good luck, hope this helps a bit : )
+ 8
C++ Soldier (Babak) you nailed it real good bro, and the swapping with XOR was just awesome : )
+ 5
Very interesting compliment!
+ 5
Aqua Crow, as much as I appreciate the accepted mark, I hope you understand I was only giving an idea, the implementation was in C++ Soldier (Babak) post, which I believe deserved it better : )
+ 4
"Especially the swapping <--- part."
That was only a hack to get rid of the third variable (temp) to hold the intermediate value.
Example: Let `x` be 5 and `y` be 2. We wanna swap them without the third variable and only performing XOR operation in binary level. Let's go through it step-by-step
x = 5
y = 2
The binary representation of them in 4 bits
x = 0101
y = 0010
Xor operator indicated by ^ character and manipulate the bits like follow
a b ^
_____
0 0 0
0 1 1
1 0 1
1 1 0
And by applying that concept to `x` and `y` FOR THE FIRST OPERATION OF SWAP OUT OF THREE as `x ^= y` we would have this
0101
0010 ^
______
0111 == 7 is the current value of `x`
Next, we have `y ^= x` FOR THE SECOND OP OUT OF THREE
0010
0111 ^
_____
0101 == 5 is the current value of `y`
Finally, we again have the same thing as THE FIRST TIME like `x ^= y` FOR THE LAST OP
0111
0101 ^
____
0010 == 2 is the current value of `x`
So now we've successfully swapped them.
~~~~~~~~~~~~
As for "I also had the person array there, I just did not know how to reference it via per pancake array"
Well, I didn't see any `person` array! 8D
The process is easy enough to follow:
1. Declare `int person[10]`
2. While populating the `pancake` guy, populate the `person` as `person[i] = i + 1;`. After the loop termination, its content would be like so
1 2 3 ... 10
3. As I touched in my mini tutorial above, these three lines perfoming swap op everytime that `pancake` guy MUST BE SWAPPED that's why I put it in the same block where `pancake` guy swapping takes place.
person[i] ^= person[j];
person[j] ^= person[i];
person[i] ^= person[j];
4. Simply print out the contents of two guy like so
cout << "Person " << person[i] << " ate " << pancake[i] << endl;
Yeah yeah I know I could use the third variable instead of hack, but I preffered to do some old-school stuff, you know?! 8D
+ 4
No problem Aqua Crow : )
+ 3
I was thinking of a 2 dimensional array but I was displaying the idea on paper wrong. So I'll try that. Thanks for the idea.
+ 3
C++ Soldier (Babak) thanks for the additions to the code. I will take a look at it and see what is going on. Especially the swapping <--- part. I am still learning and have no idea what that segment is doing. I also had the person array there, I just did not know how to reference it via per pancake array. I still don't see the correlation. May you guys explain?
+ 3
Oh C'mon Ipang! Don't sweat it! 8D
+ 3
Very interest conclusion! 8D
+ 2
Great explanation. Will take a bit of re reading to actually understand it. I appreciate it.
+ 2
AHHHHHHHHHHHHHHHH I GET IT. Thanks bud. Super late but better late than never. I see why the execution of x swap y took place twice. I should have paid attention in my engineering course but as they say learn as you go. I appreciate it. And you were right, the version I had deleted the person array 8D but I swear it was there. I first thought was to use a 2 dimensional array when I read the exercise but could not execute it because my terminology was backwards. I thought of columns first and then rows, which would make every entry have 2 outcomes. Instead [2][10] is actually rows over columns. You guys have helped a ton and I hope you guys are there for the rest of my journey. Appreciate you both.
+ 1
I probably use the check mark wrong. I check mark when I have an understanding of what I read. That way I can tell myself I understand what was said.