#include <iostream>
usingnamespace 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.
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.
}
}
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?