For Loop and Arrays : Displaying a list from greatest to least
Oct 13, 2013 at 6:55am UTC
I seem to be having some trouble of understanding the concept of switching around array values and displaying them in greatest to least with a for loop.
Update: For some reason this code works for two people but nothing more than two people??
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
#include <iostream>
using namespace std;
int main() {
int personNumber = 1;
int pancakeAmount;
int person[3] = {};
int greatestToLeast[3] = {};
int listOfPeople[3] = {};
cout << "Pancake Glutton 1.0 \n\n" ; //State program's title
cout << "10 Different people ate pancakes for breakfast.. \n\n" ;
while (personNumber !=4) {
cout << "How many did Person " << personNumber << " eat? > " ;
cin >> pancakeAmount;
if (personNumber == 1) {
person[personNumber] = pancakeAmount;
greatestToLeast[personNumber] = pancakeAmount;
listOfPeople[personNumber] = personNumber;
}
else if (pancakeAmount > person[personNumber - 1]) {
person[personNumber] = pancakeAmount; //Next person gets assigned the number of pancakes
listOfPeople[personNumber] = personNumber;
greatestToLeast[personNumber] = pancakeAmount;
int storePancakeValue = person[(personNumber - 1)]; //Variable used to store the amount of pancakes of the person before
greatestToLeast[(personNumber - 1)] = pancakeAmount;
greatestToLeast[personNumber] = storePancakeValue;
int storePersonNumber = listOfPeople[(personNumber - 1)];
listOfPeople[(personNumber - 1)] = personNumber;
listOfPeople[personNumber] = storePersonNumber;
}
else {
person[personNumber] = pancakeAmount;
greatestToLeast[personNumber] = pancakeAmount;
listOfPeople[personNumber] = personNumber;
}
personNumber++;
}
int x;
cout << "\n\n" ;
for (x=1; x<4; x++) {
cout << "Person " << listOfPeople[x] << " ate " << greatestToLeast[x] << " pancakes! \n" ;
}
}
Last edited on Oct 13, 2013 at 7:19am UTC
Oct 13, 2013 at 8:13am UTC
int personNumber = 1;
needs to be
int personNumber = 0;
arrays start at array element 0 so
1 2
if (personNumber == 1) {
person[personNumber] = pancakeAmount;
is actually storing pancakeAmount in the second array element
Oct 13, 2013 at 8:16am UTC
The array indexing starts from 0. Therefore, valid indices of three-element array are 0, 1, and 2. You use indices 1, 2, and 3 in your program. That is an error.
Oct 13, 2013 at 8:31am UTC
I'm unfortunately still not getting this to work. :/
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
#include <iostream>
using namespace std;
int main() {
int personNumber = 1;
int greatestToLeastPancakeAmount[9] = {};
int greatestToLeastPersonNumber[9] = {};
int pancakeAmount;
cout << "Pancake Glutton 1.0 \n\n" ; //State program's title
cout << "10 Different people ate pancakes for breakfast.. \n\n" ;
int x;
for (x=0;x<10;x++) {
cout << "How many pancakes did person " << (x + 1) << " eat? > " ;
cin >> pancakeAmount;
greatestToLeastPersonNumber[x] = x;
greatestToLeastPancakeAmount[x] = pancakeAmount;
if (greatestToLeastPersonNumber[x] > greatestToLeastPersonNumber[(x-1)]) {
int storeGreatestToLeastPersonNumber = greatestToLeastPersonNumber[(x-1)];
greatestToLeastPersonNumber[(x-1)] = x;
greatestToLeastPersonNumber[x] = storeGreatestToLeastPersonNumber;
}
if (pancakeAmount > greatestToLeastPancakeAmount[(x - 1)]) {
int storeGreatestToLeastPancakeAmount = greatestToLeastPancakeAmount[(x-1)];
greatestToLeastPancakeAmount[(x-1)] = pancakeAmount;
greatestToLeastPancakeAmount[x] = storeGreatestToLeastPancakeAmount;
}
}
cout << "\n\n" ;
for (x=0;x<11;x++) {
cout << "Person " << greatestToLeastPersonNumber[x] << " ate " << greatestToLeastPancakeAmount << " pancakes!\n" ;
}
}
Oct 13, 2013 at 8:49am UTC
This is what this part looks like if you replace x with the value of x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int x;
for (x=0;x<10;x++) {
cout << "How many pancakes did person " << (0 + 1) << " eat? > " ;
cin >> pancakeAmount;
greatestToLeastPersonNumber[0] = 0;
greatestToLeastPancakeAmount[0] = pancakeAmount;
if (greatestToLeastPersonNumber[0] > greatestToLeastPersonNumber[(0-1)]) {
int storeGreatestToLeastPersonNumber = greatestToLeastPersonNumber[(0-1)];
greatestToLeastPersonNumber[(0-1)] = 0;
greatestToLeastPersonNumber[0] = storeGreatestToLeastPersonNumber;
}
if (pancakeAmount > greatestToLeastPancakeAmount[(0 - 1)]) {
int storeGreatestToLeastPancakeAmount = greatestToLeastPancakeAmount[(0-1)];
greatestToLeastPancakeAmount[(0-1)] = pancakeAmount;
greatestToLeastPancakeAmount[0] = storeGreatestToLeastPancakeAmount;
}
}
I should say just on the first pass.
Last edited on Oct 13, 2013 at 8:52am UTC
Oct 13, 2013 at 9:00am UTC
personNumber on line 5 is an unused variable. Not an error.
Lines 6 and 7 reserve arrays for 9 elements, but loop on line 12 iterates over 10 elements (plus that -1 when x==0) and loop on line 29 accesses 11 elements.
Line 30 prints value of element and address of an array.
Oct 13, 2013 at 4:24pm UTC
I restarted, and working on getting it to just display the order of people I input, for some reason instead of saying "Person 1 ate..." it says "Person 10 ate.." so it comes out as;
1 2 3 4 5 6 7 8 9 10
Person 10 ate..
Person 2 ate..
Person 3 ate..
Person 4 ate..
Person 5 ate..
Person 6 ate..
Person 7 ate..
Person 8 ate..
Person 9 ate..
Person 10 ate..
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
#include <iostream>
using namespace std;
int main() {
int greatestToLeastPancakeAmount[9] = {};
int greatestToLeastPersonNumber[9] = {};
int pancakeAmount;
int x;
cout << "Pancake Glutton 1.0 \n\n" ; //State program's title
cout << "10 Different people ate pancakes for breakfast.. \n\n" ;
for (x=0;x<10;x++) {
cout << "How many pancakes did person " << (x + 1) << " eat? > " ;
cin >> pancakeAmount;
greatestToLeastPersonNumber[x] = (x + 1);
greatestToLeastPancakeAmount[x] = pancakeAmount;
/*if(pancakeAmount > greatestToLeastPancakeAmount[(x - 1)]) {
int storeGreatestToLeastPancakeAmount = greatestToLeastPancakeAmount[(x-1)];
int storeGreatestToLeastPersonNumber = greatestToLeastPersonNumber[(x-1)];
greatestToLeastPancakeAmount[(x-1)] = pancakeAmount;
greatestToLeastPersonNumber[(x-1)] = x;
greatestToLeastPancakeAmount[x] = storeGreatestToLeastPancakeAmount;
greatestToLeastPersonNumber[x] = storeGreatestToLeastPersonNumber;
}*/
}
cout << "\n\n" ;
for (x=0;x<10;x++) {
cout << "Person " << greatestToLeastPersonNumber[x] << " ate " << greatestToLeastPancakeAmount[x] << " pancakes!\n" ;
}
}
Last edited on Oct 13, 2013 at 4:53pm UTC
Topic archived. No new replies allowed.