programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# array problem

johny doe
Ranch Hand
Posts: 78
we got an integer array of 4 cell
in which we have 3 cells equal
how do i write a loop that adds these 3 cells

another example to my problem may be an array of 6 cells
in which we get 3 cells equal to 7
and 3 cells equal to 8
and the two type are scrambled between each other
i want to calculate separatly the sum of each type of cell
so i get on integer x 24
and on integer y 21

i tried to solve this proble by a for loop
inside of a for loop

but it counts the same possibilities more than one time

how do i solve this problem??

Campbell Ritchie
Marshal
Posts: 56584
172
Looks like a beginner's topic . . .

Answer: Don't know. Please show us what you have so far. Do you mean you have to traverse the array and find out which values are equal to one another, or to add the 7s and then the 8s?

johny doe
Ranch Hand
Posts: 78
yes

Campbell Ritchie
Marshal
Posts: 56584
172
Try adding all the 7s first. Try setting up a temporary variable equal to 7 and a sum variable equal to 0. Then whenever you meet an array member equal to the temporary variable, add it to the sum. Then repeat for 8.

See how that works.

 Consider Paul's rocket mass heater.