views:

1223

answers:

3

I have already turned this in so you won't be helping me cheat. Just wondering if this looks right:

The assignment: Input a list of employee names and salaries, and determine the mean (average) salary as well as the number of salaries above and below the mean.

The Plan: Allow input of names and salaries Calculate Mean Sort values Count values above Mean Count values below Mean

//This program will allow a user to input an employee name and salary
//The output will contain the mean salary 
//as well as the number of salaries above and below the mean
//
//Arrays Used:
//Name(K) = Array for employee names
//Salary(K) = Array for salaries
//
//Variables Used:
//Mean = Mean of all employees Salaries
//UpMean = Number of Employees making more than the mean
//DwnMean = Number of Employees making less than the mean
//Sum = Sum of all salaries
//CountM = Counter for Mean
//CountUp = Counter for # of salaries above mean
//CountDwn = Counter for # of salaries below mean

Main
    Call WelcomeMessage
    Call InputData
    Call Calculate
    Call OutputData
End Program

WelcomeMessage
    Write, “Beginning the Salary Program” 
End WelcomeMessage

InputData
    Declare Name(100) Of Strings
    Declare Salary(100) Of Real
    Declare Mean, UpMean, DwnMean As Real
    Set Sum = 0
    Set CountM = 0
    Set CountUp = 0
    Set CountDwn = 0
    Write, "Enter Employee name and Salary."
    Write, "Enter *,0 when done."
    Input Name(K), Salary(K)
    While Name(K) <> "*"
     Set CountM = CountM + 1
     Set Sum = Sum + Salary
     Write, "Enter Employee name and Salary."
     Write, "Enter *,0 when done."
     Input Name(K), Salary(K)
    End While
End InputData

Calculation
    //Here Mean is found
    Set Mean = Sum / CountM
    //Here Number of Employees making more than the mean is found
    For K = Step 1 to CountM
     If Salary(K) > Mean Then
      Set CountUp = CountUp + 1
     End If
    //Here Number of Employees making more than the mean is found
    Set CountDwn = CountM - CountUp
    //The above algorythm doesn't account for the possibility 
    //of someone making exactly the average so subtract 1 to reconcile
    If Salary(K) = Mean Then
      Set CountDwn = CountDwn - 1
    End If
End Calculation

OutputData
    Write, "There were,"  CountM, "salaries entered."
    Write, "The mean salary is:", Mean
    Write, "There are", CountUp, "employees who make more than the average"
    Write, "There are", CountDwn, "employees who make less than the average"
End OutputData
+5  A: 

Looks ok. The only thing I have to suggest, is to use a do-while structure while reading input to name/salery. As you can see you have the same logic before the loop begins, and in the loop:

Write, "Enter Employee name and Salary."
Write, "Enter *,0 when done."
Input Name(K), Salary(K)

Also, the pseudo code will not compile, since you are calling Calculate but the routine is called Calculation ;)

Thanks for the suggestions. Not really familiar yet with Do-While. What would that look like? I though maybe something about the input should change in the loop but wasn't sure how.

It could look something like this:

Do 
    Write, "Enter Employee name and Salary."
    Write, "Enter *,0 when done."
    Input Name(K), Salary(K)
    If Name(K) <> "*"
        Set CountM = CountM + 1
        Set Sum = Sum + Salary
    Else
        BreakLoop
    End If
End While (true)

It's not really a big difference, but more a matter of taste really. Personally I think it's easier to read, since the code is written in such a way that you easily realize that you are supposed to input something, check input and do something depending on the input.

In your while loop the Set CountM etc come after (in the text flow) the first input, but before the rest of the input, which means you have to look back into the top of the loop to understand that it does something after the previous "round" in the loop. Now this is just a small loop, but if it was 30 rows long (god forbid) you'd have to scroll up to see what's going on. If you know what I mean :)

Magnus Skog
I don't really think giving instructions like do ... while (true), with a break statement in the code, is good style to teach a new person. Code should be self documenting, and the execution flow here is hard to follow.
Hooked
+1  A: 

This looks very nice! A "clean start".

Andrew Siemer
+1  A: 

A note about calculating CountDwn:

Your "subtract 1 to reconcile" will, depending on how exactly the For loop works in the implementation language, (a) generate an "undeclared variable"-type error, (b) generate an "index out of range" error, or (c) subtract one IFF the last salary was exactly equal to the average. (Also, your code doesn't include an End For in Calculation, but I assume it should be immediately after the first End If in that function.)

Instead of calculating CountDwn from CountUp (after all, every single salary could be equal to the average), I'd suggest including it in the loop:

Calculation
    //Here Mean is found
    Set Mean = Sum / CountM

    For K = Step 1 to CountM
        //Here Number of Employees making more than the mean is found
        If Salary(K) > Mean Then
            Set CountUp = CountUp + 1
        End If

        //Here Number of Employees making less than the mean is found
        If Salary(K) < Mean Then
            Set CountDwn = CountDwn + 1
        End If
    End For
End Calculation

Note that CountUp + CountDwn is not necessarily equal to CountM.

Ben Blank