• Post Reply Bookmark Topic Watch Topic
  • New Topic

array problem  RSS feed

 
johny doe
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!