1 / 72

Program Development

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.

rolanda
Télécharger la présentation

Program Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Development 01204111 Computer and Programming

  2. 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

  3. Agenda • A broad overview on how to develop programs • Examples • GPA calculation • Numerical integration • Master Mind game

  4. 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.

  5. Problem solving steps • Understand the problem • Have a plan • Follow a plan • Check the result

  6. 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

  7. 4 basic steps Analysis Design Implementation Testing

  8. When things are wrong… Analysis Design Implementation Testing You may need to go back.

  9. Example 1: Calculating GPA • Write a program that reads the grades and computes the GPA.

  10. Analysis (1) • We have to ask: • What is a GPA? • How can we compute it?

  11. 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.

  12. 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.

  13. 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

  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. 

  15. Implementation • We have 3 tasks to do. • It would be easy to start with smaller parts and building up to the bigger ones.

  16. 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.

  17. 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: "))

  18. 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.

  19. Implementation • Now it is time to add more code to keep tracks of total credit and total grade score.

  20. 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: "))

  21. 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: "))

  22. 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?

  23. 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.

  24. 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))

  25. 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.

  26. 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

  27. 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.

  28. 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?

  29. 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.

  30. 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… 

  31. 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?

  32. 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.

  33. 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.

  34. 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]

  35. 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.

  36. 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] 

  37. More on Design • Our program is more complex, so it is good to try to separate the program into smaller functions.

  38. 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.

  39. 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

  40. Thinking Corner: solution def read_compute_score(num,name,credit): print("Course",num,name) g = float(input("Enter grade: ")) return g * credit

  41. 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))

  42. Example 2: Numerical Integration • You want to estimate an area under the curve of some function.

  43. Analysis (1) • How to find an area? • Integration? That's too difficult. • Is simpler calculation possible?

  44. Analysis (2) • Let's look at a simpler example of a curve Now, that's easy. It's just the height times the width.

  45. Analysis (3) • Can we do that with the real curve? We can pretend that the area consists of small rectangle slices.

  46. Design • What are the tasks? • Read input • Divide the area into smaller pieces • Calculate the area of each piece

  47. 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 …

  48. 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

  49. Thinking Corner: solution • Recall that to get the function, we should call func. def slice_area(x, width): return width * func(x)

  50. 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)

More Related