Click here to Skip to main content
15,889,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm very new to C# and is having a bit of a loop problem.

At the moment I have a group of sellers that needs to be counted by the amount of articles sold. They are already assigned to their respective level by the following conditions:
C#
if (seller[i].articles < 50)
    seller[i].level = 1;

if (seller[i].articles >= 50 && seller[i].articles < 100)
    seller[i].level = 2;

if (seller[i].articles >= 100 && seller[i].articles < 200)
    seller[i].level = 3;

if (seller[i].articles >= 200)
    seller[i].level = 4;

However, a slight problem arises when I want to count how many of the sellers that actually are in respective level.
This might lend a clue to figure out what I'm trying to achieve:
C#
            int count = 0;
            for (int i = 0; i < 3; i++)
            {
                if (seller[i].level == i) 
                    count++;
Console.WriteLine("Numbers of sellers in level {0}: {1}", seller[i].level, count);

            }


Regards,
L
Posted
Updated 26-Oct-12 15:32pm
v2
Comments
leprechauny 29-Oct-12 16:41pm    
Well, as indicated and correctly assumed there are only four levels. So exactly as you suggest I think it's plausible to use an array to hold the different levels. However, the real number of sellers are six. But is that really an issue? That is, the numbers of sellers ought to be irrelevant when you determine which level has that been reached.

1 solution

I'd almost be tempted to do something like this ..

// store the count of sellers at each level
// see the discussion about the number of elements in this array
int[] levels = new int[5] {0, 0, 0, 0, 0};


and then when you do

if (seller[i].articles < 50)
        seller[i].level = 1;


that becomes something like :-

if (seller[i].articles < 50) 
(
    seller[i].level = 1;
    levels[seller[i].level] += 1;
    // or levels[1] += 1;
)


...and so forth so all the calcs are done in one pass, unlike what you suggest

the only issue I have even with my own approach, is that arrays are indexed by '0', therefore levels[0] is wasted in the above (but there's a correspondance to your levels 1-4)

I'd have to think if

int[] levels = new int[4] {0, 0, 0, 0};


and using

// note the -1 below to map levels 1-4 to array 0-3
levels[seller[i].level -1] += 1;


is better.

I don't know if anyone else agrees or not, but I hate traversing data sets multiple times where a single pass will do ...

Since you only indicate 4 levels, I think an array is justified - if you had an arbitrary number of levels I'd likely use a hash-map or such where seller[i].level -1 is the index in the hash-map and count is the value

I still worry (probably too much about what I dont see here - if ths is the only place in the code that modifies levels[], then ok, else I'd be thinking about checking that either seller[i].level or seller[i].level -1 was in the range of the array bounds, also depending on the decision or not to map your levels 1-4 to the array subscripts 0-3

thats my 2 cents worth
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900