Thread: Array Problems
View Single Post
Old Jan 31st, 2008, 4:24 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Array Problems

Yes, you could combine all 4 of those repetitions into 1 loop, by using an array to store each individual type's count. Where each type corresponds to a location in the array.

seats = new array of size 4, all set to 0
money = new array of size 4, all set to 0

for each item in transactionclubSale

    if item.typeString is Orchestra
        index = 0
    else if item.typeString is Mezzanine
        index = 1
    else if item.typeString is General
        index = 2
    else if item.typeString is Balcony
        index = 3

    seats[index] += 1
    money[index] += item.priceDecimal

Orchestra Seats and Orchestra Money is seats[0] and money[0]
Mezzanine Seats and Mezzanine Money is seats[1] and money[1]
General Seats   and General Money   is seats[2] and money[2]
Balcony Seats   and Balcony Money   is seats[3] and money[3]

But I would advise you try the first method before moving onto something more complicated, if you're a novice just starting to learn how to program.
Sane is offline   Reply With Quote