Pseudo-Code zu überprüfen. Muss die Validierung für die Zuordnung

stimmen
5

Ich habe gedreht dies bereits in so werden Sie mich nicht betrügen werden zu helfen. Nur frage mich, ob dies gut aussieht:

Die Zuordnung: Eingabe der Liste der Mitarbeiternamen und die Gehälter, und das mittlere (durchschnittliche) Gehalt sowie die Anzahl der Gehälter über und unter dem Mittelwert.

Der Plan: Gewähren Eingabe von Namen und Gehälter berechnen Mittelwert Sortierwerte Zählwerte über dem mittleren Zählwerte unter Mittlere

//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
Veröffentlicht am 12/06/2009 um 00:02
quelle vom benutzer
In anderen Sprachen...                            


3 antworten

stimmen
5

Schaut OK aus. Das einzige, was ich vorschlagen haben, ist eine do-while-Struktur zu verwenden, während Eingabe Name / Salery zu lesen. Wie Sie sehen können haben Sie die gleiche Logik, bevor die Schleife beginnt, und in der Schleife:

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

Außerdem wird der Pseudo-Code nicht kompiliert werden, da Sie berechnen fordern aber die Routine aufgerufen Berechnung;)

Vielen Dank für die Anregungen. Nicht wirklich vertraut noch mit Do-While. Was würde das aussehen? Ich dachte, vielleicht etwas über die Eingabe sollte in der Schleife ändern, war aber nicht sicher, wie.

Es könnte wie folgt aussehen:

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)

Es ist nicht wirklich ein großer Unterschied, aber mehr eine Frage des Geschmacks wirklich. Ich persönlich denke, es ist einfacher zu lesen, da der Code so geschrieben ist, dass man leicht erkennen, dass die Eingabe etwas soll, beim Check-Eingang und etwas tut, am Eingang abhängig.

In Ihrer while-Schleife kommt die Sets CountM etc nach (im Textfluss) der ersten Eingang, aber vor dem Rest des Eingangs, der bedeutet, dass Sie wieder in den Anfang der Schleife suchen, zu verstehen, dass es etwas nach dem vorherigen tut „rund“ in der Schleife. Nun ist dies nur eine kleine Schleife, aber wenn es 30 Zeilen lang ist (Gott bewahre) Sie müßten, um nach oben, um zu sehen, was los ist. Wenn du weißt, was ich meine :)

Beantwortet am 12/06/2009 um 00:12
quelle vom benutzer

stimmen
1

Eine Anmerkung über die Berechnung CountDwn:

Ihre „1 subtrahieren in Einklang zu bringen“ wird, je nachdem , wie genau die ForSchleife arbeitete in der Umsetzung der Sprache, (a) ein „nicht angemeldeten Variable“ -Typ - Fehler erzeugen, (b) Erzeugen einen Fehler „out of range Index“ ist , oder (c ) subtrahieren IFF das letzte Gehalt genau gleich dem Durchschnitt. (Auch dann , wenn Ihr Code keine umfassen End Forin Calculation, aber ich nehme an, es sollte unmittelbar nach der ersten sein End Ifin dieser Funktion.)

Statt der Berechnung CountDwnvon CountUp(immerhin jedes einzelne Gehalt könnte auf die durchschnittliche gleich sein), würde ich es in der Schleife einschließlich vorschlagen:

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

Beachten Sie, dass CountUp + CountDwnnicht unbedingt gleich ist CountM.

Beantwortet am 12/06/2009 um 00:50
quelle vom benutzer

stimmen
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
Beantwortet am 13/11/2016 um 04:29
quelle vom benutzer

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