Upload
kaladher-reddy
View
90
Download
0
Embed Size (px)
DESCRIPTION
program logic
Citation preview
PROGRAMMING LOGIC AND DESIGNSECOND ASSIGNMENTMAKING 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
3. The keyword that is NOT included in a singlealternative selection is ____. 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:
startFLOWCHART:
DECLARATIONSnum orderNonum deskLengthnum deskWidthnum drawersnum surfacenum totalChargenum drawerCostnum MIN_CHARGE = 200string custNamestring woodTypestring 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( )
detailLoop( )orderNo >0? yes
finishup( ) no
stop
housekeeping( )Finishup( )
Output End of prgmOutput ORDER_PROMPT
returnInput ordernum
return
detailLoop( )
totalCharge = 0
output CUST_PROMPTinput custNameoutput LENGTH_PROMPTinput deskLengthoutput WIDTH_PROMPTinput deskWidthoutput WOOD_PROMPTinput woodTypeoutput DRAWER_PROMPTinput drawers
surface = deskLength * deskWidthdrawerCost = drawers * 30
woodType =mahogany? no yes
totalCharge = totalCharge + 150
woodType =oak ANDdeskLength > 36?
yesyes
totalCharge = totalCharge + 125no
surface> 750? no yes
totalCharge = totalCharge + 50
totalCharge = totalCharge + drawerCost + 200
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:
StartDeclarationsnum orderNonum deskLengthnum deskWidthnum drawersnum surfacenum totalChargenum drawerCostnum MIN_CHARGE = 200string custNamestring woodTypestring 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 > 0detailLoop( )endwhilefinishup( )Stop
housekeeping( )output ORDER_PROMPTinput orderNoreturn
finishup( )output End of Programreturn
detailLoop( )totalCharge = 0output CUST_PROMPTinput custNameoutput LENGTH_PROMPTinput deskLengthoutput WIDTH_PROMPTinput deskWidthoutput WOOD_PROMPTinput woodTypeoutput DRAWER_PROMPTinput drawerssurface = deskLength * deskWidthdrawerCost = drawers * 30if woodType = mahogany thentotalCharge = totalCharge + 150elseif woodType = oak AND deskLength = 36 thentotalCharge = totalCharge + 125endifendifif surface > 750 thentotalCharge = totalCharge + 50endiftotalCharge = totalCharge + drawerCost + MIN_CHARGEoutput Order: , orderNooutput Customer: , custNameoutput Length: , deskLengthoutput Width: , deskWidthoutput Drawers: , drawersoutput Wood Type: , woodTypeoutput Price of Desk: , totalChargeoutput ORDER_PROMPTinput orderNoreturn
EXERCISE 5:
FLOWCHART:
start
DeclarationsNum itemnumNum priceNum dayNum TERM=7Num REDUCTION=0.10String Prompt= Enter item price or 999 to quit>>
housekeeping( )
itemnumber=999?
detailLoop( )yes
no
finishup( )
stop
housekeeping( )
Output PROMPT
Input itemnumber
return
Finishup( )
Output End of prgm
return
detailLoop( )
days=0
day