### HELP PREASSSEEEEE

int heaviestCar(int arr[],int elements){

int heaviest=0;
int carWeight=0;
int temp=0;

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.
One problem is temp++ is not inside your while loop. Are you looking for max or the sum?

 ``12345`` ``````} temp++; return heaviest;``````

should be
 ``123`` ``````temp++; } return heaviest;``````
if i put temp++; inside while loop i get the ridiculously large incorrect number: 228063
itssupposed to be the max of each sum before 0
carWeight is the sum and then i compare it for max.
Are you sure your reading the file into the array correctly ?
I changed your while loop into a for loop and tested it and got 280

 ``1234567891011121314151617181920212223242526272829`` ``````int main( int argc, char* argv[] ) { int n[] = { 10, 20, 0, 10, 20, 30, 0, 10, 20, 30, 40, 50, 60, 70, 0, 10, 20, 30, 40, 0 }; heaviestCar( n, _countof( n ) ); } int heaviestCar(int arr[],int elements) { int heaviest=0; int carWeight=0; int temp=0; for( int temp = 0; temp < elements; temp++ ) { carWeight += arr[temp]; if (arr[temp] == 0 ) { if (carWeight > heaviest) { heaviest = carWeight; carWeight = 0; } } } return heaviest; }``````
yeah i tried this and yeah i have other functions that require the array data and the results are perfect. one sec let me try it again and ill let you know if it worked, maybe i did something wrong that time..
still getting the same number this was my code exactly (the for loop version) still doesnt work
problem solved.

heaviest didnt have a value in main...
i thought that if heaviest was declared by itself and you return heaviest it would refresh the value, but i had to set heaviest=heaviestCar(...) lol thanks for help though
Topic archived. No new replies allowed.