2d array issue yet again

these things is killing me, i am having an issue fnding the largest number from day 1 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
#include <iostream>
using namespace std;

int main()
{

int a,b=0;

int test[2][3];
float sum = 0.0;
float sum1=0.0;

for(a = 0; a< 2; a++)
{
	cout<<"please enter the number for the days of pass "<<a+1<<endl;
	
	for(b=0; b<3 ; b++)
	{
		cin>>test[a][b];
	}
}


for(b=0;b<3;b++)
{
	sum = sum + test[0][b];
}
cout<<"the total passengers for day one is "<<sum<<endl;




	


    




for(b=0;b<3;b++)
{

	sum1 = sum1 + test[1][b];
}
cout<<"the total passengers for day two is "<<sum1<<endl;
cout<<endl;


int larg;
for(int i =0;i < 3;i++)
{
	 if(test[0][i] > test[0][i+1])
        {
            larg = test[0][i];
        }
	
	 
	 
}
    cout << "Largest number of passengers in day 1 is :" << larg << endl;
    cout<<endl;
	return(0);
	
	

	
}
Well, look at the for loop that finds the largest value. When i is zero, it compares the first and second inputted values. When i is 1, it compares the second and third values, but when i is 2, it compares the third value with... what, exactly? A gibberish number.

What I would do in that case is assign larg to be the value of test[0][0], then replace the if conditional of test[0][i] with larg. Also, reduce the i<3 part to i<2.
1
2
3
4
5
6
7
8
9
10
11
int larg = test[0][0];
for(int i =1;i < 3;i++)
{
	 if(test[0][i] > larg)
        {
            larg = test[0][i];
        }
	
	 
	 
}
so if i did a part for day 2 would it be like this

1
2
3
4
5
6
7
8
9
10
11
int larg1=test[1][0];
	for(int j =2;j < 3;j++)
	{
		if(test[0][j] > larg1)
		{
			larg1 = test[0][j];
		}
	}
	cout<<"the largest number of passengers on day 2 is "<<larg1<<endl;
	cout<<endl;
	return(0);
No. Please understand the logic of every statement. I am re-writing my code with comments.

1
2
3
4
5
6
7
8
9
10
11
int larg = test[0][0];  // Initialize larg to be day 1's first set of passengers.
for(int i =1;i < 3;i++)  // Start checking for a larger number "2nd" set onwards.
{                                // This is because you have already assigned the 1st set to larg and you have to check for the remaining sets, if any is greater.
	 if(test[0][i] > larg)  // If the respective set of passengers is > larg
        {
            larg = test[0][i];  // make larg equal to that number.
        }
	
	 
	 
}
Last edited on
ahhh i see so it would be like this for day 2

1
2
3
4
5
6
7
8
9
10
11
int larg1=test[1][0];
	for(int j =1;j < 3;j++)
	{
		if(test[1][j] > larg1)
		{
			larg1 = test[1][j];
		}
	}
	cout<<"the largest number of passengers on day 2 is "<<larg1<<endl;
	cout<<endl;
	return(0);


because im initializing larg1 to be day 2's first set of pasangers then following out the rest right?
yes now it should work.
Topic archived. No new replies allowed.