0% found this document useful (0 votes)
208 views

Algorithms and Control Structures

This is a little description about the algorithms of problem solving and control structure of C++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
208 views

Algorithms and Control Structures

This is a little description about the algorithms of problem solving and control structure of C++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
Control/Logic Structures Dykstra proposed that all computer programs could be written using just three basic control or logic structures. Sequence, selection, and repetition. Prior to this a construct called a branch (associated with the word GOTO) was used extensively. The use of the branch resulted in programs that were poorly written, difficult to follow, even more difficult to debug and virtually impossible to modify. Modern languages such as Pascal, Algol, ADA, C, C++ and Java are built around these three structures. Sequence — executing instructions one after another as they appear in the program. Selection — choosing between two or more alternative actions. Repetition — repeating a block of code until a condition is met. 3-10 A Simple Sequence Problem We want to develop an algorithm (a step by step process) for a program to calculate the sum and average of two numbers entered by the user of the program. Step 1: Structure Chart Cale & output sum & average of 2 numbers Get 2 numbers Cale sum & avg Output sum & avg Get I" Get 2" [Cale sum Cale avg] [Output Output number, number sum avg The structure chart shows the relationship among the various components of the program. The top level is a general description of the problem to be solved where the lower levels show all the detail or specifics of the solution. NOTE: The second level of a structure chart attempts to break the solution into three basic parts — an input module, a processing module and an output module. Step 2: Flowchart ‘A flowchart provides a detailed picture of the algorithm using special symbols to represent various program statements. A flowchart will always be drawn from top to bottom showing the exact order of the steps. Before we can proceed with the flowchart we need to think about things we will want to computer to store as the program runs. We need it to keep a place in memory for each of the two numbers, the sum of the numbers and the average of the numbers. The term used for these memory locations is variable. A variable is a place in memory that contains a data value that may be changed during program execution. We do not know exactly where in memory these values will be placed so we will reference them by name. Giving a name to a memory location is called symbolic addressing. We need names for the locations we are requesting. NOTE: The names that appear on the flowchart must match exactly the names actuall used in your program. We will use the following names: secondNum, sum, and average. There are three flowchart symbols necessary for programs involving simple sequence. a) Begin/End of a block | Process 3.12 Note that the flowchart begins with the processes outlined on the bottom left of the structure chart — the lowest level where the specific steps have been outlined, Ceegin > L/ tputtestNum —// / / = secondNum ,” / ( ‘sum = firstNum + secondNum oF avg = sum /2 J outputsum —/ / / ) outputavg _/ end Simple Sequence Exercises 1. Design the algorithm for a program that calculates the total of a retail sale. The program should ask the user for the following: the retail price of the item being purchased and the sales tax rate. Once the information has been entered the program should calculate and display the following: the sales tax for the purchase and the total sale. Draw the flowchart for this algorithm. 2. Design the algorithm for a program that calculates the current balance in a savings account. The program should obtain from the user the following information: the starting balance, the total amount of deposits made, the total amount of withdrawals made, and the monthly interest rate. After the program has calculated the current balance, it should be displayed on the screen. Assume one input for deposits and one input for withdrawals. Draw the flowchart for this algorithm. 3. Draw a flowchart to match the following pseudocode. - Give variable num1 a starting value of 5 - Give variable num2 a starting value of 10 - Add 7 to num2 = Store the value num| times num2 in variable num3 = Store the value num2 minus num| in num2 - Output num1, num? and num3 4, Trace the steps in your flowchart from question 3 and show the output, produced by this program, 5. Obtain from the user an hourly pay rate and the number of hours worked for the week. Calculate their pay for the week (no overtime, and no taxes). Output the result. 3.14 Name Due Date Simple Sequence Problems 1. Draw a structure chart for a program that will obtain from the user a length and width for a rectangle and the radius of a circle. Calculate and output the area and perimeter of the rectangle and the area and circumference of the circle. List the necessary variables, draw the flowchart (using the same names as your variable list), and perform a desk check using inputs of 7 and 10 for the rectangle and 4 for the circle, 2, Draw the structure chart for a program that will obtain from the user a current annual salary and a percent increase due on that salary. Assume that the new pay rate should have been in effect on January 1, and it is now July 1. Calculate, store, and output the new annual salary, the new monthly salary, and the retroactive pay due. Make a list of variables, draw the flowchart (using the same names as your variable list), and perform a desk check using a starting salary of $60,000 and a pay increase of 5%. 3. Draw a structure chart for a program that obtains from the user, an annual interest rate, the term of the loan (in years), and the amount borrowed. Your program should use the following formula: rate(I + rate) X loan amount payment (1+ ratey"™~ 1 You will need to convert the interest rate to a monthly rate in decimal format, and the term into months. Make a list of necessary variables for this program. Draw the flowchart (using the same names in your variable list), and perform a desk check using test data of $120,000 for the loan amount, 30 years for the term, and an annual interest. rate of 7% TURN IN THIS SHEET ON TOP OF YOUR NEATLY DRAWN STRUCTURE CHARTS AND FLOWCHARTS. A Selection Problem Following is the design for an algorithm to select the larger of two input values. We will make the assumption that the two numbers are not equal. Step 1: Structure Chart Find the larger of 2 input values Get 2 values Compare & select Output larger value larger value i Get 1" Get 2" value value ep 2: Flowchart The following variable names will be used: firstNum, secondNum and larger. We will need one more flowcharting symbol: C > Decision / input frstNum lager = secondNum / output larger yy Selection Exercises if 1. Obtain the length and width of a rectangle from the user. Calculate and output the area. If the length and width are equal, output a message indicating that the figure is a square. Make a list of variables, draw the flowchart, and perform a desk check using the following: 4, 8, 5,5 2. Obtain three test scores from a student. Calculate their average test score and output this value. If their average score is 75% or more output a message indicating that they may proceed to the next class. Make a list of variables, draw the flowchart, and perform a desk check using the following: 45, 55, 75, 80, 75, 88 318 Selection Exercises iffelse 1, Obtain a name and age from the user. If the user is 16 or older, output a message indicating they are old enough to drive. For people under 16, output a message indicating how many years they must wait before they can drive legally. 2. Obtain from the user an hourly pay rate and the number of hours worked for the week. Calculate and output their weekly pay according to the following: ~ Regular pay is the pay up to 40 hours. Overtime pay is pay for the hours over 40. Overtime is paid at a rate of 1.5 times the hourly rate. ~ Gross pay is the sum of the regular pay and the overtime pay. Hint: This calculation is not conditional. 3. Obtain a temperature in degrees Fahrenheit from the user. If the temperature is 80 degrees or more, display a message that says "Go play golf" otherwise display a message stating "It's too cold to be outside." Make a variable list, flowchart, and perform a desk check using the following values: 95, 70 4, Obtain a temperature in degrees Fahrenheit from the user. If the temperature is 80 degrees or more display the message "Go play golf” otherwise, if the temperature is 70 - 79 degrees display the message "Put on a jacket", otherwise display the message "It is way too cold." Make a variable list, flowchart, and perform a desk check using the following values: 95, 72, 50 5. Obtain three numbers from the user. We will assume that the numbers are not the same. Find the largest of these three numbers and store that value in a variable. Output this variable with an appropriate message. Make a variable list, flowchart, and perform a desk check using the following values: 3, 1, 2, 1, 2,3, 2,3, 1 A Repetition Problem Following is an algorithm to average and output five sets of input values. Step 1: Structure chart ‘Average & output 5 sets of input values For 5 times Get 2 values Cale the average ‘Output the average Gett"| | Geer" value value Step 2: Flowchart Variable names: first control the loop) Yum, secondNum, average, and i (the variable to New flowchart symbol: <> Cateriop Note that as the algorithms become a little more complex less of the actual detail is visible in the structure chart. The flowchart will always give the most detailed representation and is what should be used when you code the program. 3-20 Cegn input frstwum //Aput seconanum / —,“ verage = (firstNum + secondNumn} 2 / outputaverage / —— Name Due Date Repetition Exercises while Loop 1. You are to flowchart a program for Aunt Ellen's Egg Ranch. The user will enter the number of eggs gathered and the program will output the number of dozens as well as the number of excess eggs. Continue this process until a negative number is entered. Example: Enter the number of eggs gathered: 43 You have 3 dozen and 7 eggs, List the necessary variables for this program. Perform a desk check with the following input values: 24, 8, 15, and -999. 2. Draw the flowchart for a program that accepts an unknown number of ages from the keyboard until zero is entered. If the age is greater than or equal to 100 output "You have lived a century" otherwise if age is greater than or equal to 55 output "AARP here I come" otherwise output "You are still a spring chicken." List the necessary variables for this program, Perform a desk check with the following input values: 100, 21, 555, 0. 3. Draw the flowchart for a program that will obtain exam scores from the user as long as the score is greater than or equal to zero. Determine whether the score is passing (60 or above) o failing and output an appropriate message. List the ne this program. Perform a desk check with the following input value 999. 4. Draw the flowchart for a program that will dispense money from an ATM machine. Continue to request a withdrawal amount from the user until they enter -999. If the amount entered is not evenly divisible by twenty, output the message "You must enter multiples of twenty only" otherwise calculate and output the number of twenty dollar bills you will be dispensing. List the necessary variables for this program. Perform a desk check with the following input values: 35, 60, 61, 360, and -999. TURN IN (IN THIS ORDER & STAPLED IN THE UPPER LEFT CORNER) - this sheet - flowcharts (DONE ON THE COMPUTER ONLY) 3.23

You might also like