CS 101 Oct 5 Computer problem solving Chapter

CS 101 – Oct. 5 Computer problem solving Chapter 6: read pp. 148 -174 • Problem-solving procedure • Structure of a solution • Examples!

Review: what is CS ? • The study of how we … – Solve problems – Represent information • In problem solving, we’d like: – Find systematic ways of going about solution – Correct, quick and informative solutions. • Information can be: – Different types: numbers, text, images, sound, the software itself – The input and output to a computer program

Why create software? • Computational power – – Excel is limited to so many rows/columns Can convert data to images Can create games and useful applications With one “language”, can solve virtually any problem. • Built-in features of languages – Many common calculations are pre-defined for you such as sorting, opening files, surfing the Web, creating a button, etc.

Software • Powerful! – We get to tell the machine exactly what we want. – Sometimes, existing programs like Excel or Photoshop are not enough. • Program = sequence of instructions for CPU to obey. – Works like a recipe. – A recipe has: ingredients, steps, and result is food! – A program has: input, calculations, output. When we start to look at programs, be on the lookout for these 3 parts.

Recipes • Cooking may be a good analogy, because it’s solving a specific problem “I’m hungry. ” • What do we see in recipes? Here’s one: – – – – Brown the beef 15 min. Drain grease. Dice carrot, celery, onion (aka “mirepoix”) Cut up and boil 6 potatoes until soft. Mash potatoes Add flour, spices, sauce, mirepoix to beef. Put meat mixture into casserole, top with potatoes. Bake in oven at 400 for 30 minutes.

Recipes (2) • A computer program has some of the same elements as a recipe… • In recipes we see: – Ingredients (the “nouns” of the problem) – Steps to perform (the “verbs”) – In some steps, we continue/wait for something – Although it’s not obvious, sometimes we check things: • Are potatoes fully mashed? • Should I add more _____ to the mixture?

Recipes (3) • But we don’t eat the same stuff every day. Once we know a few recipes, we can put together a menu for choices. if if (have all ingredients), make Shepherd’s pie. (no potatoes), just make soup instead. (no veggies), make hamburger. (no beef), make pasta. • When you view a whole menu as a program, then “making soup” becomes a sub-program. – A large program is composed of several parts. – In industry, sometimes each part implemented by different people. A kitchen may have many chefs.

Problem-solving 1. Understand problem; inputs and outputs 2. Write solution in English “pseudocode” 3. Write code in a programming language 4. Compile 5. Run and test • When program works, can refine.

Problems • The earliest problems given to a computer were mathematical. • Sometimes there is no clean formula – Many equations can’t be solved analytically. For example, cos(x) = x. Need to solve numerically. – Ex. Heat equation is a partial differential equation (PDE). Most PDEs have to be solved numerically. – Ex. Calculating a square root. • And even if there is a clean formula, a computer can help you automate the calculations.

Problems (2) • “Mathematical” problems may at first sound boring. But they include many useful applications – Ex. Finding directions • Other kinds of problems for the computer – – – Games Record keeping, managing investments, … Networking, communication Multimedia (e. g. image processing) Of course, much more!
- Slides: 10