Programming Logic and Design
Programming Logic and Design
Programming Logic and Design
SECOND ASSIGNMENT
MAKING DECISIONS AND LOOPING
Submitted by
Gurram Kaladhar Reddy(C0634430) Nishtala Sandeep(C0640316)
EXERCISE 1:
1. You can use parentheses to override the default order of operations. Ans: True 2. The loop control variable is initialized after entering the loop. Ans: False 3. An indefinite loop is a loop that never stops. Ans: True 4. You can either increment or decrement the loop control variable. Ans: True
EXERCISE 2:
1. Many loop control variable values are altered by ____, or adding to them. Ans: C 2. Usually, when you create nested loops, each loop has its own ____. Ans: D
EXERCISE 3:
1. How can you improve the efficiency of a nested decision? Ans: By asking questions in the proper order, performance time can be improved 2. Explain the purpose and use of the AND operator. Ans: In And decision first ask the questions that is less likely to be true
Eliminates as many instances of the possible, It speeds up processing time 3. In an OR decision, why is it more efficient to first ask the question that is more likely to be true? Ans: Because if the first question is true, no need to ask the second
4. What are the three steps that should occur in every loop? 1. Test the loop control variable to determine whether the loop body executes 2. Alter the loop control variable 3. Provide a starting value for the variable that will control the loop 5. How might user data be validated? Make sure data falls in acceptable ranges (month values between 1 and 12 )
EXERCISE 4:
FLOWCHART:
start
DECLARATIONS num orderNo num deskLength num deskWidth num drawers num surface num totalCharge num drawerCost num MIN_CHARGE = 200 string custName string woodType string ORDER_PROMPT = enter order number or -1 to QUIT: string CUST_PROMPT = enter customer name: string LENGTH_PROMPT = enter desk length: string WIDTH_PROMPT = enter desk width: string WOOD_PROMPT = enter wood type: string DRAWER_PROMPT = enter number of drawers:
housekeeping( )
yes
detailLoop( )
stop
housekeeping( )
Finishup( )
Output ORDER_PROMPT
Input ordernum
return
return
detailLoop( )
totalCharge = 0
output CUST_PROMPT input custName output LENGTH_PROMPT input deskLength output WIDTH_PROMPT input deskWidth output WOOD_PROMPT input woodType output DRAWER_PROMPT
input drawers
yes
totalCharge = totalCharge + 50
output Order: , orderNo output Customer: , custName output Length: , deskLength output Width: , deskWidth output Drawers: , drawers output Wood Type: , woodType output Price of Desk: , totalCharge
output ORDER_PROMPT
Input orderno
return
PSEUDOCODE:
Start Declarations num orderNo num deskLength num deskWidth num drawers num surface num totalCharge num drawerCost num MIN_CHARGE = 200 string custName string woodType string ORDER_PROMPT = enter order number or -1 to QUIT: string CUST_PROMPT = enter customer name: string LENGTH_PROMPT = enter desk length: string WIDTH_PROMPT = enter desk width: string WOOD_PROMPT = enter wood type: string DRAWER_PROMPT = enter number of drawers: houseKeeping( ) while orderNo > 0 detailLoop( ) endwhile finishup( ) Stop
finishup( ) output End of Program return detailLoop( ) totalCharge = 0 output CUST_PROMPT input custName output LENGTH_PROMPT input deskLength output WIDTH_PROMPT
input deskWidth output WOOD_PROMPT input woodType output DRAWER_PROMPT input drawers surface = deskLength * deskWidth drawerCost = drawers * 30 if woodType = mahogany then totalCharge = totalCharge + 150 else if woodType = oak AND deskLength = 36 then totalCharge = totalCharge + 125 endif endif if surface > 750 then totalCharge = totalCharge + 50 endif totalCharge = totalCharge + drawerCost + MIN_CHARGE output Order: , orderNo output Customer: , custName output Length: , deskLength output Width: , deskWidth output Drawers: , drawers output Wood Type: , woodType output Price of Desk: , totalCharge output ORDER_PROMPT input orderNo return
EXERCISE 5:
FLOWCHART:
start
Declarations Num itemnum Num price Num day Num TERM=7 Num REDUCTION=0.10 String Prompt= Enter item price or 999 to quit>>
housekeeping( )
itemnumber =999?
yes
detailLoop( )
no finishup( )
stop
housekeeping( )
Output PROMPT
Input itemnumber
return
Finishup( )
return
detailLoop( )
days=0
day<TERM ? no
yes
Output PROMPT
day=day+1
Price=price*(1-REDUCTION)
return
PSEUDOCODE:
start Declarations num itemNumber num price num day string description num TERM = 7 num REDUCTION = 0.10 string PROMPT1 = Enter an item number or 999 to exit housekeeping() while itemNumber not equal to 999 detailLoop() endwhile finishUp() stop
detailLoop( ) day = 0 while day < TERM day = day + 1 output day, price price = price * (1 REDUCTION) endwhile output PROMPT1 input itemNumber return finishUp( ) output End of program return