Sub Menu contents

Unit 2

Introduction to Mobile Apps & Pair Programming

2.1 Unit Overview

2.2 I Have a Dream Tutorial

2.3 The Internet & The Cloud

2.4 I Have a Dream Part 2

2.5 Mobile Apps & Devices

2.6 Algorithm Basics

2.7 I Have a Dream Projects

2.8 Abstraction

2.9 Binary Numbers

2.10 Hardware Abstractions: Logic Gates

2.11 The Digitial Explosion

2.12 Wrap Up

 

Algorithm Basics

In Lesson 1.2 we introduced the term algorithm and defined it as a step-by-step procedure of precise instructions that performs some calculation or computation.

Algorithms are at the heart of computer science. Algorithms, expressed in computer code and interpreted by the computer, are what make our computers such powerful and adaptable machines.

As we saw in the maze problems in Lesson 1.1, algorithms are constructed out of basic building blocks called control structures. There are three basic control structures:

  • Sequence– a sequence of instructions or statements.
  • Selection– a conditional instruction that lets the program branch between two or more alternatives.
  • Repetition (or Iteration)– a structure that repeats one or more instructions.
  • An amazing fact that has been proved by computer scientists is that all algorithms can be constructed by using just these three control structures. In other words, any algorithm that you would like to write to solve a problem can be built by a combination of sequence, selection, and repetition.



Make sure to view the PowerPoint about Algorithms in the Assignments pane, and complete your Portfolio items.

Writing Pseudocode: Critical Thinking Questions

Pseudocode is a cross between computer code and everyday English. There are many different varieties. Pseudocode is less precise than actual computer code, such as Java or Python or App Inventor, but more precise and less wordy than everyday English.

Suppose we have a list of numbers -- e.g., 5, 10, -2, -3, 7, 8, 12 Here's an algorithm that uses sequence, selection, and iteration (repetition) to add all the even numbers in the list and print out their sum. Notice how indentation is used to clarifiy the different parts of the algorithm.

  1. Set the running total to 0.
  2. For each number in the list:
  3.       If the number is even
  4.            Add the number to the running total.
  5. Print the running total.

This algorithm contains five lines. They are numbered for convenience.

  1. Which line(s) of the algorithm contain a repetition statement?
  2. Which line(s) of the algorithm contain a selection statement?
  3. If you ran this algorithm on the above list of numbers, what would it print?
  4. (Portfolio) Suppose you had a list of positive numbers such as 5, 10, 12, 13, 6, 7, 1, 3, 2, 1. And suppose for each of the numbers in the list you added the number to a running total if it is even and subtracted it if it is odd. What result would you get for this list of numbers?
  5. (Portfolio) Write a pseudocode algorithm that implements the algorithm you used to calculate this total.