dimanche 30 octobre 2016

Sum the lowest and highest integer in 2d array

Learning 2d array and and i try to find highest length and lowest length of bricks that possibly can be build. And here's my code:

for(i=0;i<brick;i++){
    for(j=0;j<brick_sides;j++){
        cin>>a[i][j];
    }
}
//Highest and lowest length of each row 
for(i=0;i<brick;i++){
    highest=a[i][0];
    lowest=a[i][0];
    H=0;L=0;
    for(j=0;j<brick_sides;j++){
        if(highest<a[i][j])
        highest=a[i][j];
        if(lowest>a[i][j])
        lowest=a[i][j];
    }
    //display it
    cout << "The highest length in row " << i+1 << "=" << highest << endl;
    cout << "The lowest length in row " << i+1 << "=" << lowest << endl;
    cout << endl;
}

//process to sum the lowest length and highest length that possibly can builded
for(j=0;j<brick_sides;j++){
        H+=highest;
        L+=lowest;
}
cout << L << " " << H<< endl;
return 0;
}

From this code input, it's looks like what i expect

3
1 2 3
4 5 6
7 8 9

But, for the output, there's a problem, it's not like i expected

12 18 // expectation

21 27 // from the code that i build

Is there any 'mistake' in my code that i build?

Aucun commentaire:

Enregistrer un commentaire