arrays

closed account (4y64izwU)
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
main(){

int person[10]={1,2,3,4,5,6,7,8,9,10};
int number[10];
int temp = 0;
int value = 0;

cout<<"Enter the number of pancakes eaten by person "<< person[0] <<"."<<endl;
cin >> number[0];

cout<<"Enter the number of pancakes eaten by person "<< person[1] <<"."<<endl;
cin >> number[1];

cout<<"Enter the number of pancakes eaten by person "<< person[2] <<"."<<endl;
cin >> number[2];

cout<<"Enter the number of pancakes eaten by person "<< person[3] <<"."<<endl;
cin >> number[3];

cout<<"Enter the number of pancakes eaten by person "<< person[4] <<"."<<endl;
cin >> number[4];

cout<<"Enter the number of pancakes eaten by person "<< person[5] <<"."<<endl;
cin >> number[5];

cout<<"Enter the number of pancakes eaten by person "<< person[6] <<"."<<endl;
cin >> number[6];

cout<<"Enter the number of pancakes eaten by person "<< person[7] <<"."<<endl;
cin >> number[7];

cout<<"Enter the number of pancakes eaten by person "<< person[8] <<"."<<endl;
cin >> number[8];

cout<<"Enter the number of pancakes eaten by person "<< person[9] <<"."<<endl;
cin >> number[9];

for(int i=0;i<10;i++)
{
    if(number[i]>temp)
    temp=number[i]
}
cout << "The person that ate the most pancakes for breakfast is person " << ****** << " who ate " << temp << " pancakes." << endl;



How do i do the part where it is ****** meaning I want to include which person ate the most pancakes.
Use the index of your for loop.

BTW, You can optimize your program by using another loop and ditching the person array such that:

1
2
3
4
5
for (int i = 0; i < 10; ++i)
{
    cout << "Enter the number of ... by person " << i + 1 << endl;
    cin >> number[i];
}


You can even compute the other variables in the loop above
closed account (4y64izwU)
Thank you so much for optimizing the program, but I'm still confused how I can put in which person ate the most pancakes (like person 1....person 10)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main(){

int number[10];
int temp = 0;

for (int i=0; i<10; i++)
{
    cout << "Enter the number of pancakes eaten by person " << i + 1 << endl;
    cin >> number[i];
    
    if(number[i]>temp){
        temp=number[i];
   }
}
 cout << "The person that ate the most pancakes for breakfast is person " << ****** << " who ate " << temp << " pancakes." << endl;
U need to use another variable, and use the index of your for loop.

I'm sure u will get it.

(you need to add another statement in the if condition)
Last edited on
closed account (4y64izwU)
hmmmm still not getting it.
You could just make a variable like int index = 0; or use value since you don't seem to be using it yet. Then inside your for loop do:
1
2
3
4
if (number[i] > temp){
      temp = number[i];
      index = i;
}


You could use a for loop for the question and input too. Like so:
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
#include <iostream>

using namespace std;

int main()
{

	int person[10]={1,2,3,4,5,6,7,8,9,10};
	int number[10];
	int temp = 0;
	int value = 0;  
	int index = 0;
	
	for (int i = 0; i < 10; i++)
	{
		cout << "Enter the number of pancakes eaten by person " 
                       << person[i] << "." << endl;
		cin >> number[i];
	}
	
	for(int i=0;i<10;i++)
	{
		if(number[i]>temp){
			temp=number[i];
			index = i;
		}
	}
	cout << "The person that ate the most pancakes for breakfast is person " 
                << person[index] << " who ate " << temp << " pancakes." 
                << endl;
	
	return 0;
}
Enter the number of pancakes eaten by person 1.
1
Enter the number of pancakes eaten by person 2.
2
Enter the number of pancakes eaten by person 3.
3
Enter the number of pancakes eaten by person 4.
4
Enter the number of pancakes eaten by person 5.
50
Enter the number of pancakes eaten by person 6.
6
Enter the number of pancakes eaten by person 7.
7
Enter the number of pancakes eaten by person 8.
8
Enter the number of pancakes eaten by person 9.
9
Enter the number of pancakes eaten by person 10.
10
The person that ate the most pancakes for breakfast is person 5 who ate 50 pancakes.
Topic archived. No new replies allowed.