controllo pseudocodice. Ha bisogno di convalida per l'assegnazione
-
13-09-2019 - |
Domanda
ho già trasformato questo in modo da non aiuterete me barare. Basta chiedersi se questo appare a destra:
L'assegnazione: Input un elenco di nomi di dipendenti e stipendi, e determinare la medio salario (medio) nonché il numero di salari superiori e al di sotto della media.
Il Piano: Consentire di ingresso di nomi e stipendi Calcolare media I valori di ordinamento Contare valori sopra media Contare valori sotto la media
//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
Soluzione
Una nota su CountDwn
calcolo:
Il tuo "sottrarre 1 a conciliare" sarà, a seconda di come esattamente il ciclo For
lavora nel linguaggio di implementazione, (a) genera un errore di tipo "variabile non dichiarata", (b) generare un "indice di fuori gamma" Errore o (c) sottrarre un IFF ultima stipendio era esattamente pari alla media. (Inoltre, il codice non include un End For
in Calculation
, ma suppongo che dovrebbe essere immediatamente dopo la prima End If
in quella funzione.)
Invece di calcolare CountDwn
da CountUp
(dopo tutto, ogni singolo stipendio potrebbero essere pari alla media), suggerirei compreso nel ciclo:
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
Si noti che CountUp + CountDwn
non è necessariamente uguale a CountM
.
Altri suggerimenti
sembra ok. L'unica cosa che ho da suggerire, è quello di utilizzare una struttura di do-while durante la lettura di ingresso per nome / Salery. Come si può vedere si ha la stessa logica prima dell'inizio del ciclo, e nel circuito:
Write, "Enter Employee name and Salary."
Write, "Enter *,0 when done."
Input Name(K), Salary(K)
Inoltre, il codice pseudo non viene compilato, dal momento che si sta chiamando Calcolare ma la routine si chiama Calcolo;)
Grazie per i suggerimenti. Non proprio familiare eppure con Do-While. Cosa sarebbe che sembrano? Ho anche se forse qualcosa circa l'ingresso dovrebbe cambiare nel circuito ma non era sicuro come.
Si potrebbe essere simile a questa:
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)
Non è davvero una grande differenza, ma più una questione di gusto davvero. Personalmente penso che sia più facile da leggere, dato che il codice è scritto in modo tale che ci si rende conto facilmente che si suppone di inserire qualcosa, controllare in ingresso e fare qualcosa a seconda dell'ingresso.
Nel suo ciclo con il Set COUNT min etc verranno dopo (nel flusso di testo) al primo ingresso, ma prima che il resto dell'ingresso, il che significa che si deve guardare indietro nella parte superiore del ciclo di capire che fa qualcosa dopo il precedente "rotondo" nel ciclo. Ora, questo è solo un piccolo anello, ma se fosse lungo 30 righe (Dio non voglia) che avrebbe dovuto scorrere verso l'alto per vedere cosa sta succedendo. Se sai quello che voglio dire:)
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