Algorithms HOW CAN COMPUTER PROGRAMS SOLVE PROBLEMS Execute
Algorithms HOW CAN COMPUTER PROGRAMS SOLVE PROBLEMS?
Execute a Task Have you ever stopped to think about how many tasks you complete throughout the day? Often we execute the same tasks every day, like brushing our teeth, feeding the dog, washing the dishes, or setting the table. Computer scientists program devices to complete or execute tasks. Before you begin creating a program, you must ask yourself what exactly you want to have happen. What will the device do? What will make the device do it? When will it do it? Think about a device that needs human input to work—for example, a microwave. Then answer these questions: 1. What will the device do? 2. What will make the device do it? 3. When will the device do it?
What is an algorithm? - A repeatable process for determining the solution to a problem. - An ordered set of instructions that are used to carry out a task Often times, a computer scientist will create an algorithm to help define the tasks or processes that the device must perform. Algorithms are found everywhere—for example, a recipe to make cookies, the process of setting the table, and the method you use to solve math equations. Each of these tasks requires you to follow a series of steps. For example, an algorithm for setting the table might look like this: - At 6: 00 pm - Ask how many will be eating dinner - Repeat the following for the number of diners: - Place a plate on the table - Place a cup - Place a napkin
Reflect on Learning 1. List three examples of common processes or tasks that you do or see done on a regular basis. 2. Create an algorithm (list of steps) you would complete to brush your teeth. 3. Compare your list of steps with a partner. ◦ Did you forget any steps? ◦ Could you add more detail?
Writing an Algorithms can be represented in multiple ways: Natural language, like English Visually, using flowcharts Actual executable code (a program that runs on the computer) Before they write an algorithm using the actual code, programmers plan out their algorithms using natural language or a flowchart. The advantages of first using these two methods are: - They’re easier to understand. When algorithms are written in your speaking language (like English), they are easier to share with the client. - They’re flexible. Going from the natural language or flowchart, the algorithm can be translated as code into any programming language, such as C, Python®, or Java®. - They’re less complex. The algorithm focuses on solving the problem instead of concentrating on a particular programming language. So before you start to solve your computing problems, it’s important to plan by identifying the series of steps that the code needs to follow. This is called algorithmic thinking.
Reflect on Learning Select a basic task of your choice that you do often, such as feed the dog, get dressed, pack backpack, or wash dishes. 1. Write an algorithm for the task as a list of numbered steps. 2. Share your algorithm with a partner. 3. Critique one another student’s algorithm. Could you complete your partner’s task by only the steps your partner wrote? Why or why not? Could your partner complete your process by doing the steps you wrote? Why or why not? 4. Discuss how to improve each of your algorithms. Why is it important that the steps of an algorithm be clear, in order, and easy to understand? 5. Fix your algorithm based on the suggestions of your partner.
Flowcharts Computer scientists often use a type of algorithm, called a flowchart, to assist with decomposing, or breaking down, a large task into smaller tasks to create a logical sequence of steps to follow. A flowchart uses geometric shapes to show the sequence of operations performed—what happens first, next, and so on. This makes it easier for programmers to share their idea with others before writing the code. Once a flowchart is created, it can be used as a guide to develop a program. Flowcharts normally use standard symbols to represent the different types of instructions as shown in the following chart. These symbols are used to construct the flowchart and show the step-by-step solution to the problem.
Flowchart symbols
Flowcharts linear algorithms
Flowcharts – conditional statements But what if you had to make a decision? For example, should the cake be chocolate? Should the party start at 2 p. m. ? When we create algorithms that use decisions, such as in the cake example, we use conditional statements. Conditional statements use logic, which means the answer to the statement or question is either Yes or No or True or False. Algorithms that contain conditions can be written using numbered lists or represented in a flowchart.
Flowcharts conditional statements The step that includes the conditional question is represented by a diamond-shaped symbol, called a rhombus. Arrows from the rhombus direct the person or computer to the next step based on the answer to the question. In this flowchart there are two conditions or decisions. In a flowchart, the rhombus or diamond-shaped component, is used to indicate when a decision or choice needs to be made. Let’s look at the decisions that were made for setting the table. Is it 6 o’clock? If yes, then it’s time to set the table, otherwise continue doing homework. Is the table set for everyone who will be eating dinner? If yes, then you’re done, and if not, keep adding plates, cups, and silverware.
Reflect on Learning 1. Create a flowchart for the same algorithm you wrote for Writing an Algorithm. 2. Share your flowchart with your partner and discuss the following: ◦ Is your algorithm linear or conditional? How do you know the difference? ◦ Describe the similarities and differences between flowcharting and algorithms. 3. How can flowcharting or writing an algorithm help you solve a problem, think about a task, or define the purpose of a program? 4. How can algorithmic thinking skills, like decomposition and effective communication, be useful in your other classes, at home, and in your hobbies? 5. What is the difference between a linear and a conditional flowchart? Give examples of each type. (Don’t use the examples from this activity. )
- Slides: 12