720 likes | 891 Views
Program Development. 01204111 Computer and Programming. What is it now?. You have learn quite a few techniques in programming Basic calculation Input / output Dividing tasks into smaller ones using functions Basic control structures: Condition Iteration Basic data structure: lists.
E N D
Program Development 01204111 Computer and Programming
What is it now? • You have learn quite a few techniques in programming • Basic calculation • Input / output • Dividing tasks into smaller ones using functions • Basic control structures: • Condition • Iteration • Basic data structure: lists
Agenda • A broad overview on how to develop programs • Examples • GPA calculation • Numerical integration • Master Mind game
Programming is problem solving • Why do you want to program? • You have some idea in mind, and now you want a computer to do that for you. • You have a problem that you think a computer can help you solve it. • You are tired of doing something tedious, and you think that a computer can help you do that.
Problem solving steps • Understand the problem • Have a plan • Follow a plan • Check the result
Program development steps • Analysis • Try to understand the problem • Design • Figure out the plan • Implementation • Actually write the program • Testing • Verify that the program is correct
4 basic steps Analysis Design Implementation Testing
When things are wrong… Analysis Design Implementation Testing You may need to go back.
Example 1: Calculating GPA • Write a program that reads the grades and computes the GPA.
Analysis (1) • We have to ask: • What is a GPA? • How can we compute it?
Analysis (2) • To be able to compute the GPA, we need the grade and the credit for each course the user is taking. • We need to get it from the user.
Analysis (3) • Now we have to think about how our program would work… • It keeps asking for credits and grades • When the user finish, it outputs the GPA.
Analysis (4) • This is how we want our program to run: Enter credit (-1) to end: 3 Enter grade: 3.5 Enter credit (-1) to end: 1 Enter grade: 4 Enter credit (-1) to end: 3 Enter grade: 2.5 Enter credit (-1) to end: -1 Your GPA is 3.14
Design • A program should read the input, iteratively • A credit and a grade (as number) • To be able to compute GPA, it should keep • The total number of credits • The accumulated grade scores (i.e., the sum of products of grades and credits) • From the data that we keep, we should be able to apply the formula.
Implementation • We have 3 tasks to do. • It would be easy to start with smaller parts and building up to the bigger ones.
Thinking Corner • Write a program that ask the user for the credits and the grades, without doing anything with it. Stop when the user enters -1 as credit.
Thinking Corner: solution c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) c = int(input("Enter credit (-1) to end: "))
Testing • This little piece of the puzzle is crucial to the whole program we are writing, so we'd better make sure that it works. • Write program in small pieces and test them to be sure that each small pieces are correct.
Implementation • Now it is time to add more code to keep tracks of total credit and total grade score.
Where should we add the code? c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) c = int(input("Enter credit (-1) to end: "))
Thinking Corner • Add a program that keep tracks of • Total credit • Total grade score c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) c = int(input("Enter credit (-1) to end: "))
Thinking Corner: solution c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) tc += c tgs += c*g c = int(input("Enter credit (-1) to end: ")) This is easy! Opps… there's something wrong. Can you spot it?
Thinking Corner:correct solution tc = 0 tgs = 0 c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) tc += c tgs += c*g c = int(input("Enter credit (-1) to end: ")) Don't forget to initialize variables.
Implementation • For the last step, we have to compute the GPA. tc = 0 tgs = 0 c = int(input("Enter credit (-1) to end: ")) while c != -1: g = float(input("Enter grade: ")) tc += c tgs += c*g c = int(input("Enter credit (-1) to end: ")) print("Your GPA is %.2f" % (tgs/tc))
Are you sure? • After we implement the whole program, we will have to test the program. • We have to ask ourselves how to tell if the program is correct. • It is not always easy as in the lab where you are given an example data.
Test examples • Let's think about our own example: • Easy one, just 1 course: • Credit 3, grade 3.5 GPA = 3.5 • Harder one, 3 courses: • Credit 3, grade 3.5 • Credit 2, grade 4 • Credit 1, grade 2.5 • GPA (3*3.5 + 2*4 + 1*2.5)/(3+2+1) We can ask Python Shell for help… And it says 3.5
Testing time • We can test our program with the test examples that we prepare. • When all examples pass, we are pretty certain that our program is correct.
User test • In real world, we have to give the program to the users to try it and see if they like it. • Now, let's pretend to be a user… • Are there anything you don't like about this program?
Complains • It is confusing to enter just credits and grades, I may forget to enter some course or enter some course twice. • I don't want to enter the credit again and again. Can the program know what courses I am taking? After these complains, it is time to think about the program again. Let's go back to the analysis step.
Analysis again • Is it possible to help the user not to have to remember all the courses and grades? • If we know all the courses taken by the user, we can make the program a little better. • Who are our users? • Yes, we are…
Design (1) • We have to keep all the courses taken by the first year engineering student. • And it looks like this: How can we store this data in our program?
Design (2) • Alternative 1: numbers = ['204111','417167','420111', '420113','999021','175xxx']names = ['Computer and Programming', 'Engineering Math I','Physics I', 'Lab Physics I','Thai for Communication', 'Physical Education']credits = [3, 4, 3, 1, 3, 1] Use 3 lists.
Design (3) • Alternative 2: info = [ ['204111','Computer and Programming',3], ['417167','Engineering Math I',4], ['420111','Physics I',3], ['420113','Lab Physics I',1], ['999021','Thai for Communication',3], ['175xxx','Physical Education',1] ] Use 1 list, but each course isstored as a list as well.
Design (4) info = [ ['204111','Computer and Programming',3], ['417167','Engineering Math I',4], ['420111','Physics I',3], …] • How can we access first course? • info[0] • How can we access the name of that course? • info[0][1] or • c = info[0], and use c[1]
Design (5) • The goal of the design step is to explore various approaches. • Can you think of any pros and cons of both alternatives? • Let's think about some concrete scenario.
Thinking Example • When we want to ask for information for the 2nd course on the list and calculate the grade score. Course 417167 Engineering Math I Enter grade: 4 Alternative 1 Alternative 2 print("Course",numbers[2], names[2])g = float( input("Enter grade: "))score = g*credit[2] c = info[2] print("Course",c[0],c[1])g = float( input("Enter grade: "))score = g*c[2]
More on Design • Our program is more complex, so it is good to try to separate the program into smaller functions.
Tasks • Main subtask: • Show the course, read the grade, and calculate the grade score. • Main program: • Look at each course, call main subtask, and calculate GPA.
Thinking Corner • Write a function read_compute_score(num,name,credit)that • Shows the course number (num) and name, • Asks the user for the grade • And returns the grade score (grade x credit) of that course
Thinking Corner: solution def read_compute_score(num,name,credit): print("Course",num,name) g = float(input("Enter grade: ")) return g * credit
Complete main program numbers = […] names = […] credits = […] course_count = len(numbers) total_score = 0 total_credit = 0for i in range(course_count): score = read_compute_score(numbers[i], names[i], credits[i])total_score += scoretotal_credit += credits[i] print("Your GPA is %.2f" % (total_score / total_credit))
Example 2: Numerical Integration • You want to estimate an area under the curve of some function.
Analysis (1) • How to find an area? • Integration? That's too difficult. • Is simpler calculation possible?
Analysis (2) • Let's look at a simpler example of a curve Now, that's easy. It's just the height times the width.
Analysis (3) • Can we do that with the real curve? We can pretend that the area consists of small rectangle slices.
Design • What are the tasks? • Read input • Divide the area into smaller pieces • Calculate the area of each piece
Design • What about the function for the curve itself? • We leave it as a function func. def func(x): # since our program works with any function # we shall leave this detail out and just # call the function for the curve "func" return …
Thinking Corner • Write a function slice_area(x,width)that returns the area of the rectangle between position x and x+width, as shown below. Hint:what isthisheight? x x+width
Thinking Corner: solution • Recall that to get the function, we should call func. def slice_area(x, width): return width * func(x)
Main program: practice • What is the missing code? a = float(input("Enter a: ")) b = float(input("Enter b: ")) num = 100 # use 100 pieces area = 0 width = (b - a)/ num for i in range(num): area += slice_area(a + i*width, width) print("Area = %.3f" % area)