Stuck, Need Help!

I am trying to make my code analyze the data and then output the info, but I am doing so with arrays because it is less time consuming. I keep running the code to make sure everything is fine, and when I finished the else if statement, my program ran everything instead of what I wanted to run. Please take a look at my code and help me out, thanks.

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
/*
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 person[10];
//output message
 cout<<"Enter the number of pancakes eaten by 10 different people."<<endl;
//create a list of the people
   for( int x=0;x<10;x++)
    cin>>person[x];
//make program analyze the data and output who ate the most pancakes
if (person[0]>person[1,2,3,4,5,6,7,8,9])
   {
       cout<<"Person 1 ate the most pancakes."<<endl;
   }
    else if (person[1]>person[0,3,4,5,6,7,8,9]);
    {
        cout<<"Person 2 ate the most pancakes."<<endl;
    }
return 0;
}
You want to figure out who ate the most pancakes try writing an algorithm
like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int number_of_persons=10; //a more flexible code
int person[number_of_persons];
//output message
cout << "Enter the number of pancakes eaten by "<< number_of_persons<< " different people." << endl;
//create a list of the people
for (int x = 0; x<number_of_persons; x++)
	cin >> person[x];
//make program analyze the data and output who ate the most pancakes
int highest = 0;
int Person_high;
int lowest = 5000;
int Person_low;
for (int n = 0; n<number_of_persons;n++){
	for (int i = 0; i<number_of_persons; i++){
		if (person[n] < person[i] && person[n]<lowest){ lowest = person[n]; Person_low = n+1; }
		else if (person[n]>person[i] && person[n] > highest){ highest = person[n]; Person_high = n+1; }
	}
}
cout << "Person "<<Person_high<<" ate the most pancakes: "<<highest << endl;
cout << "Person " << Person_low << " ate the least pancakes: "<<lowest << endl;
cin.get();// pause the code 
Last edited on
for ★★★★ you might want to take a look at sorting algorithms
Last edited on
@mjamesball9

Why are you using the comma operator in these lines?

if (person[0]>person[1,2,3,4,5,6,7,8,9])

else if (person[1]>person[0,3,4,5,6,7,8,9]);

Do you understand what the comma operator actually does in C++?
Topic archived. No new replies allowed.