Pseudokod check. Wymaga walidacji dla przypisania

głosy
5

Już w ten odwrócił się, więc nie będzie pomagać mnie oszukać. Po prostu zastanawiasz się, jeśli wygląda to prawo:

Przypisanie: Wejście lista nazwisk pracowników i wynagrodzenia, i określić średnią pensję (średnia), a także liczbę płac powyżej i poniżej średniej.

Plan: umożliwia wprowadzanie nazw i wynagrodzenia sortowania wartości Obliczyć średnie zliczenie wartości powyżej średniej liczby wartości poniżej 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
Utwórz 12/06/2009 o 00:02
źródło użytkownik
W innych językach...                            


3 odpowiedzi

głosy
5

Wygląda ok. Jedyną rzeczą, którą muszę sugerować, jest użycie struktury do-while podczas czytania wejście do nazwy / salery. Jak widać masz tę samą logikę przed rozpoczęciem pętli oraz w pętli:

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

Również pseudo kod nie będzie kompilować, ponieważ dzwonisz Oblicz ale procedura jest nazywana Obliczanie;)

Dzięki za sugestie. Naprawdę nie zna jeszcze z do-while. Jakby to wyglądało? I choć może coś o wejściu powinno zmieniać się w pętli, ale nie był pewien, w jaki sposób.

Może to wyglądać mniej więcej tak:

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)

To naprawdę nie jest wielka różnica, ale to kwestia gustu naprawdę. Osobiście uważam, że jest to łatwiejsze do odczytania, ponieważ kod jest napisany w taki sposób, że łatwo sobie sprawę, że mają coś wejściowego sprawdzić wejście i zrobić coś w zależności od wejścia.

W swojej pętli while Set COUNT min etc przyjść po (w tekście) pierwszego wejścia, ale przed resztą wejścia, co oznacza, trzeba spojrzeć wstecz na początek pętli, aby zrozumieć, że robi coś po poprzednim „okrągłe” w pętli. Teraz jest to tylko mała pętla, ale jeśli to był długi 30 wierszy (broń Boże) trzeba było przewijać w górę, aby zobaczyć co się dzieje. Jeśli wiesz co mam na myśli :)

Odpowiedział 12/06/2009 o 00:12
źródło użytkownik

głosy
1

Uwaga na temat obliczania CountDwn:

Twój „odjąć 1 do pogodzenia” będzie, w zależności od tego, jak dokładnie to Forpętla działa w języku realizacji, (a) wygeneruje „nielegalna zmienna” błąd -type, (b) generowanie indeksu „poza zasięgiem” błędu, lub (c ) odjąć jedną IFF ostatnia pensja była dokładnie równa średniej. (Również, Twój kod nie zawiera End Forin Calculation, ale zakładam, że powinno to być od razu po pierwszym End Ifw tej funkcji).

Zamiast obliczania CountDwnz CountUp(wszak każda płaca mogła być równa średniej), sugeruję włączenie go w pętli:

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

Należy pamiętać, że CountUp + CountDwnnie zawsze jest równy CountM.

Odpowiedział 12/06/2009 o 00:50
źródło użytkownik

głosy
0
FINAL ALGORITHM

START
OUTPUT "Enter the number of parcels"
INPUT NUMBEROFPARCELS
INTEGER PRICE = 0
INTEGER PARCELWEIGHT [1:NUMBEROFPARCELS]
INTEGER TOTALPRICE = 0

FOR PARCELLOOP = 1 TO NUMBEROFPARCELS
    INTEGER REJECT = 0
    INTEGER ACCEPT = 0
    INTEGER ACCEPTWEIGHT = 0
    INTEGER REJECTEDPARCELS = 0

    OUTPUT "Enter the weight of the parcel in kg"
    INPUT WEIGHT
    IF (WEIGHT < 1) THEN
        REJECT = REJECT + 1
        OUTPUT "The weight of the parcel should be atleast 1kg"
    ELSE
        IF (WEIGHT > 10) THEN
            REJECT = REJECT + 1
            OUTPUT "The weight of the parcel should be less than 10kg"
    ENDIF
    IF (WEIGHT > 1) THEN
        IF (WEIGHT < 10) THEN
            PARCELWEIGHT[PARCELLOOP] = WEIGHT
        ENDIF
    ENDIF


    OUTPUT "Enter the first dimension of the parcel in cm"
    INPUT DIMENSION1
    IF (DIMENSION1 > 80 ) THEN
        REJECT = REJECT + 1
        OUTPUT "Each dimension of the parcel should be less than 80"
    ENDIF

    OUTPUT "Enter the second dimension of the parcel in cm"
    INPUT DIMENSION2
    IF (DIMENSION2 > 80 ) THEN
        REJECT = REJECT + 1
        OUTPUT "Each dimension of the parcel should be less than 80"
    ENDIF

    OUTPUT "Enter the third dimension of the parcel in cm"
    INPUT DIMENSION3
    IF (DIMENSION3 > 80 ) THEN
        REJECT = REJECT + 1
        OUTPUT "Each dimension of the parcel should be less than 80"
    ENDIF

    TOTALDIMENSION = DIMENSION1 + DIMENSION2 + DIMENSION3
    IF (TOTALDIMENSION > 200 ) THEN
        REJECT = REJECT + 1
        OUTPUT "The size of the parcel should be less than 200cm"
    ENDIF

    IF (REJECT > 0 ) THEN
        OUTPUT "Your parcel has been rejected for the reasons above"
        REJECTEDPARCELS = REJECTEDPARCELS + 1
    ENDIF

    IF (REJECT = 0)THEN
        OUTPUT "Your parcel has been accepted"
        ACCEPT = ACCEPT + 1 
        ACCEPTWEIGHT = ACCEPTWEIGHT + WEIGHT
    END IF

    INTEGER PARCELSACCEPTED = ACCEPT
    INTEGER TOTALWEIGHT = ACCEPTWEIGHT
    INTEGER PARCELSREJECTED = REJECTEDPARCELS

    OUTPUT "The number of parcels accepted is " PARCELSACCEPTED " and the total weight of the parcels is " TOTALWEIGHT
    OUTPUT "The number of parcels rejected is " PARCELSREJECTED
NEXT PARCELLOOP

FOR PRICELOOP = 1 TO NUMBEROFPARCELS
    IF (PARCELWEIGHT[PARCELLOOP] < 5) THEN
        PRICE = PRICE + 10
        TOTALPRICE = TOTALPRICE +PRICE
    END IF

    IF (PARCELWEIGHT[PARCELLOOP] > 5) THEN
        PRICE = ((PARCELWEIGHT[PARCELLOOP] - 5)*0.10)/100
        TOTALPRICE = TOTALPRICE +PRICE
    END IF

    OUTPUT "The price of the parcel is " PRICE
NEXT PRICELOOP

OUTPUT "The total price of all the parcels is " TOTALPRICE
STOP
Odpowiedział 13/11/2016 o 04:29
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more