Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
Algorithms Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Invitation to Computer Science, Java Version, Second Edition.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Algorithms and DeMorgan’s Laws Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
1 Running Experiments for Your Term Projects Dana S. Nau CMSC 722, AI Planning University of Maryland Lecture slides for Automated Planning: Theory and.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
Mental Arithmetic Strategies Scotts Primary School. Mental Arithmetic Scheme.
CSC 108H: Introduction to Computer Programming
Few More Math Operators
Algorithms.
Thinking about programming
Introduction to Algorithms
Component 1.6.
AP CSP: Cleaning Data & Creating Summary Tables
Algorithms and Problem Solving
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Mental Arithmetic Strategies
Repetition Structures
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
CMPT 120 Topic: Searching – Part 1
Testing UW CSE 160 Winter 2017.
Lecture 3 of Computer Science II
CS1371 Introduction to Computing for Engineers
Thinking about programming
Algorithm Analysis CSE 2011 Winter September 2018.
Recursion CSE 2320 – Algorithms and Data Structures
Testing UW CSE 160 Spring 2018.
The Scientific Method: How to solve just about anything
Lesson 2: Building Blocks of Programming
Computer Science and an introduction to Pascal
Learning to Program in Python
More Nested Loops and Lab 5
Intro to Nested Looping
Testing UW CSE 160 Winter 2016.
Writing Methods AP Computer Science A.
Fundamentals of Programming
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
Intro to Nested Looping
The Scientific Method.
Programming We have seen various examples of programming languages
Problem Solving Designing Algorithms.
Intro to Nested Looping
Debugging EECS150 Fall Lab Lecture #4 Sarah Swisher
The Study of Computer Science
Algorithms and DeMorgan’s Laws
Algorithms and Problem Solving
Introduction to Algorithms
Debugging EECS150 Fall Lab Lecture #4 Sarah Swisher
Flowcharts and Pseudo Code
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
Tonga Institute of Higher Education IT 141: Information Systems
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Tonga Institute of Higher Education IT 141: Information Systems
Intro to Nested Looping
Basic circuit analysis and design
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Lecture 6 - Recursion.
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Algorithms Intro to Computer Science CS1510 Dr. Sarah Diesburg

What is an Algorithm? Process or a set of rules to be followed in calculations or other problem-solving operations Or, more informally… A recipe for solving a problem

Algorithm vs. Program An algorithm is a description of how to solve a problem A program is an implementation of an algorithm in a particular language to run on a computer (usually a particular kind of computer) Difference between description of what we want to do and what we actually did

Algorithm vs. Program So which do we write first? Algorithm? Program?

What’s the Difference, Really? We can analyze the algorithm independent of its implementation. Is this the most efficient way to solve a problem? We can examine how easily, or with what difficulty, a language allows us to realize an algorithm Some languages allow us to more easily solve certain types of problems than others

Prime Number Program Write a program to compute if a number is a prime number. Prime numbers are positive numbers > 1 that are divisible only by themselves and 1

What are the steps to solve it?

What are the steps to solve it? For each divisor from 2 to number-1: Try to divide number by divisor If it is evenly divisible Print “not prime” and quit the loop Otherwise, if it isn’t evenly divisible Go on and try the next divisor If we never found a divisor that divides into the number evenly Print “is prime”

Aspects of an Algorithm Detailed: Provide enough detail to be implementable. Can be tricky to define completely, relies on “common sense” Effective: the algorithm should eventually halt, and halt in a “reasonable” amount of time. “reasonable” might change under different circumstances (faster computer, more computers, etc.)

Aspects of an Algorithm (2) Specify Behavior: the algorithm should be specific about the information that goes in (quantity, type, etc.) and the information that comes out. General Purpose: algorithms should be idealized and therefore general purpose. A sorting algorithm should be able to sort anything (numbers, letters, patient records, etc.)

Aspects of a Program Readability Robustness Correctness

Aspects of a Program: Readability We will emphasize, over and over, that a program is an essay on problem solving intended to be read by other people, even if “other people” is you in the future! Write a program so that you can read it, because it is likely that sometime in the future you will have to read it! So what makes a program readable…

Readability(2): Naming The easiest thing to do that affects readability is good naming use names for the items you create that reflect their purpose to help keep straight the types used, include that as part of the name. Python does not care about the type stored, but you do!

What Does this Do? a = int(input("give a number: ")) b,c = 1,0 while b <= a : c = c + b b = b + 1 print (a,b,c) print( "Result: ", c/(b – 1))

What Does this Do? limit_str = input(“range is 1 to input:”) limit_int = int(limit_str) count_int = 1 sum_int = 0 while count_int <= limit_int: sum_int = sum_int + count_int count_int = count_int + 1 average = sum_int/(count_int – 1) print(“Average of sum of integers from 1 to”,\ limit_int,”is”,average)

Readability(3): Comments info at the top, the goal of the code purpose of variables (if not obvious by the name) purpose of other functions being used above blocks of code that accomplish one thing anything “tricky”. If it took you time to write, it probably is hard to read and needs a comment

Readability(4): Indenting indenting is a visual cue to say what code is “part of” other code. This is not always required as it is in Python, but Python forces you to indent. This aids readability greatly.

Aspects of Programming (2) Robust: As much as possible, the program should account for inputs that are not what is expected. More on this with error handling in Chapter 14

Aspects of Programming (2) Correct: Our programs should produce correct results. Much harder to ensure than it looks!

The Problem is “Problem-Solving” Remember, two parts to our goal: Understand the problems to be solved Encode the solution in a programming language, e.g. Python

Mix of Both The goal in each class is to do a little of both: problem solving and Python Terribly important that we impress on you to try and understand how to solve the problem first before you try and code it. Develop the algorithm first! Then develop the program.

So which is harder? Writing the algorithm? Writing the program? It’s a lot like French poetry when you don’t know much about French or poetry…

Bonus Slides If I don’t get to these in class, please read these on your own!

Steps to Problem Solving From “PProblem SSSolving”, DeFranco & Vinsonhaler Be Proactive See it Simplify it Stir it up Pause and Reflect Actual: Be Proactive See it Simplify it Stir it up Pause and Reflect

Steps to Problem Solving Engage/Commit Visualize/See Try it/Experiment Simplify Analyze/Think Relax Actual: Be Proactive See it Simplify it Stir it up Pause and Reflect

Engage You need to commit yourself to addressing the problem. Don’t give up easily Try different approaches Set the “mood” Just putting in time does not mean you put in a real effort!!!

Visualize/See the Problem Find a way that works for you, some way to make the problem tangible. draw pictures layout tables literally “see” the problem somehow Everyone has a different way, find yours!

Try It/Experiment For some reason, people are afraid to just “try” some solution. Perhaps they fear failure, but experiments, done just for you, are the best way to figure out problems. Be willing to try, and fail, to solve a problem. Get started, don’t wait for enlightenment!

Simplify Simplifying the problem so you can get a handle on it is one of the most powerful problem solving tools. Given a hard problem, make is simplier (smaller, clearer, easier), figure that out, then ramp up to the harder problem.

Think it Over/Analyze If your solution isn’t working: stop evaluate how you are doing analyze and keep going, or start over. People can be amazingly “stiff”, banging their heads against the same wall over and over again. Loosen up, find another way!

One More Thing: Relax Take your time. Not getting an answer right away is not the end of the world. Put it away and come back to it. You’d be surprised how easy it is to solve if you let it go for awhile. That’s why starting early is a luxury you should afford yourself.