while (temp<elements){
carWeight+=arr[temp];
if (arr[temp]==0){
if (carWeight>heaviest){
heaviest=carWeight;
carWeight=0;
}
}
}
temp++;
return heaviest;
}
DATA FILE AS FOLLOWS
(elements will equal the number of integers in the data(this code is correct up to this function))
10
20
0
10
20
30
0
10
20
30
40
50
60
70
0
10
20
30
40
0
ive tried this code numerous ways getting the same results, either im stuck in an infinite loop or i get some arbitrarily large number that is not correct at all. heaviest should return an integer value of 280.
its a simple maximum function that reads through my array, but somewhere im getting a logic error i guess? ive moved temp++; and carWeight=0 to every possible layer of the loops but i still get infinite loops or an entirely wrong number.
its a simple maximum function that reads through my array,
If it's a simple maximum function, whats with the addition to car weight every iteration of the loop? The weight is only reset when it's greater than heaviest.
The variables tested in the loop condition also don't change anywhere in the loop. That's not exactly a recipe for success.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int heaviestCar(int arr[],int elements)
{
int heaviest = 0 ;
int index = 0 ;
while ( index < elements )
{
if ( arr[index] > heaviest )
heaviest = arr[index] ;
++index ;
}
return heaviest ;
}