Arrays?

Hello I heard you can simplify code by using arrays and down below I wrote code that figures out who ate the most pancakes. I was hoping someone could show me a different version with arrays, and list out how they did it or what the array does with comments //

Anyways, here is my code.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Requires:
 variables, data types, and numerical operators
 basic input/output
 logic (if statements, switch statements)
 loops (for, while, do-while)
 arrays

 Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
 Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

 ★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

 ★★★★ 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

*/#include <iostream>

using namespace std;

int main()
{


//variables
int person1=0;
int person2=0;
int person3=0;
int person4=0;
int person5=0;
int person6=0;
int person7=0;
int person8=0;
int person9=0;
int person10=0;
//output message
 cout<<"Enter the number of pancakes eaten by 10 different people."<<endl;

//create a list of the people
cin>>person1;
cin>>person2;
cin>>person3;
cin>>person4;
cin>>person5;
cin>>person6;
cin>>person7;
cin>>person8;
cin>>person9;
cin>>person10;

//make program analyze the data and output who ate the most pancakes
    if(person1>person2&&person3&&person4&&person5&&person6&&person7&&person8&&person9&&person10)
    {
    cout<<"Person 1 ate the most pancakes."<<endl;
    }
        else if (person2>person1&&person3&&person4&&person5&&person6&&person7&&person8&&person9&&person10)
        {
        cout<<"Person 2 ate the most pancakes."<<endl;
        }
            else if (person3>person1&&person2&&person4&&person5&&person6&&person7&&person8&&person9&&person10)
            {
                cout<<"Person 3 ate the most pancakes."<<endl;
            }
                else if (person4>person1&&person2&&person3&&person5&&person6&&person7&&person8&&person9&&person10)
                {
                    cout<<"Person 4 ate the most pancakes."<<endl;
                }
                    else if (person5>person1&&person2&&person3&&person4&&person6&person7&&person8&&person9&&person10)
                    {
                        cout<<"Person 5 ate the most pancakes."<<endl;
                    }
                        else if (person6>person1&&person2&&person3&&person4&&person5&person7&&person8&&person9&&person10)
                        {
                            cout<<"Person 6 ate the most pancakes."<<endl;
                        }
                             else if (person7>person1&&person2&&person3&&person4&&person5&person6&&person8&&person9&&person10)
                             {
                                 cout<<"Person 7 ate the most pancakes."<<endl;
                             }
                                else if (person8>person1&&person2&&person3&&person4&&person5&person6&&person7&&person9&&person10)
                                {
                                    cout<<"Person 8 ate the most pancakes."<<endl;
                                }
                                    else if (person9>person1&&person2&&person3&&person4&&person5&person6&&person7&&person8&&person10)
                                    {
                                        cout<<"Person 9 ate the most pancakes."<<endl;
                                    }
                                        else if (person10>person1&&person2&&person3&&person4&&person5&person6&&person7&&person8&&person9)
                                        {
                                            cout<<"Person 10 ate the most pancakes."<<endl;
                                        }
                                            else
                                            {
                                                cout<<"Invalid Try Again."<<endl;
                                            }
return 0;
}


I am sorry in advance if the code looks bad, I used the codeblocks IDE and it looked way better there.
Instead of having a bunch of variables named person1, person2, etc, have a single array like:

int person[10];

Thereafter, you can say person[0], person[1], etc.


Also, your if..else conditions don't do what you think they do.

Since you are going to use an array now, just loop over your array to find the index (0..9) of the largest value. Once found, you can say:

cout << "Person " << (found_index + 1) << " ate the most pancakes.\n";

Hope this helps.


[edit]
Um, reading your commentary up top, the above solution (finding the single person who ate the most pancakes) is not useful to you. You should reconsider how you store your data.

How about a structure, like this:

1
2
3
4
5
struct person
{
  int number;
  int pancakes_eaten;
};

Then you can have an array of them:

1
2
person people[10];
int number_of_people = 0;

Now you can ask for up to ten people:

1
2
3
4
5
6
7
8
9
10
cout << "How many people (1-10)? ";
cin >> number_of_people;

cout << "How many pancakes eaten for person\n";
for (int n = 0; n < number_of_people; n++)
{
  cout << (n+1) << "? ";
  people[n] = n+1;
  cin >> people[ n ].pancakes_eaten;
}

Next you need to sort your people by comparing the number of pancakes eaten. So, for example, if you are given four people:

(person number, number of pancakes eaten)
(1, 3)
(2, 12)
(3, 5)
(4, 9)

After sorting them you would have:

(2, 12)
(4, 9)
(3, 5)
(1, 3)

See how they are sorted from most to least pancakes eaten.

Use the std::sort() function (in <algorithm>) to sort. You'll have to write a function that takes two people and compares them:

1
2
3
4
5
bool compare_people( const person& a, const person& b )
{
  // greater than, since we are sorting from most to least pancakes eaten
  return a.pancakes_eaten > b.pancakes_eaten;
}


If you must write the sort function yourself, then I recommend one of the following:
Insertion Sort http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/
Selection Sort http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/

All this may seem like a lot, but it really is easier to do properly than what you have above (which doesn't do what you think it does).

Hope this helps.
Last edited on
@duoas

That is a lot to take in, what do you recommend I start studying? I have beginner level of C++ knowledge.

I think I have to learn: classes, arrays, and functions.

Can you tell me anything else I am missing to do this?
what did I do wrong in my code I put above?
I ran the code and it didn't print out what I wanted it to.
This is classwork. Where did you get this from? School? A book?

It didn't do what you wanted because the "if" conditions are incorrect.
Topic archived. No new replies allowed.