1 / 53

Computer Programming and Problem Solving

Computer Programming and Problem Solving. Ke shuwei. What is the meaning of problem solve? How to solve it? What is computer programming? What is basic knowledge for solving?. Computer Problem. the problem about computer itself Blue Screen of Death (BSoD). Computer Problem.

barb
Télécharger la présentation

Computer Programming and Problem Solving

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. Computer Programming and Problem Solving Ke shuwei

  2. What is the meaning of problem solve? • How to solve it? • What is computer programming? • What is basic knowledge for solving?

  3. Computer Problem • the problem about computer itself Blue Screen of Death (BSoD)

  4. Computer Problem • Utilize computer to solve the real world problem.The real world problem is called Computer Problem. This is the meaning of Computer problem in our class we have to solve.

  5. Computer Problem Problem: find the largest number between three number(12, 15, 18) Human:a primary student knows it is 18. Computer: zzzZZZZZ~~~~~~ NO IDEA

  6. Computer Problem The computer can not solve the problem which a primary student can solve.why we need it?

  7. Computer problem If we assume the computer can solve this problem. i.e,it can check a number is prime or composite. Problem: find all the prime number between 1 and 10000? Human: yes,i can. But i think it will take 5 hours or more. Computer: too easy,only 10 secs i need. #include <stdio.h> main () { ..... ..... }

  8. How to utilize computer to solve problem • I know C language ,i can write down C code to solve it. • This is the only way to solve it? Nope,I know Java. I can write down Java code to solve it. C++, Ruby .....

  9. What is the computer programming? Utilize any language code to solve the problem in computer.

  10. C or Java or ...? • Same Target:solve the problem. • There are thousands of Languages,which one shoud pick?

  11. How to become a good programmer? • Do i need to know every language? • Too much knowledge. • How can i learn all of them? • i only can live 100 years. ^-^ • What can we do?

  12. What can we do? • Language is nothing more than a tool to solve the problem. Learn basic knowledge for all languages

  13. Basic knowledge Problem: find the largest number between three numbers(12, 15, 18) 12 < 15, 15 < 18, 12 < 18 Using basci knowledge in different language to solve problem.

  14. C

  15. Java

  16. Connection • Syntax is different • if .. else if... else.. is same. • Operator is same ( >, (),{}) • Declaration is same(int a,...) • Algorithm(rules) is same • use folwchart to describe algorithm

  17. One more problem • find all the prime number between 1 and 100.

  18. C

  19. Java

  20. Connection • if control statements • for conrol statements • operator ( %, = , <= ..) • Syntax is different • Algorithm(logic) is different

  21. What we have to learn for all programming lanuage? 1.Algorithm (the method to slove problem) if you want to find all prime number,first, you should know what is prime number.Then,using different algorithm to solve it.

  22. What we have to learn for all programming lanuage? 2. Basic operator is almost same in all language (>, >=, !=, &&, & .. 1 > 2 ? 1 : 2) 3.Control statements if ... eles if ..else for ... while ... do ... While.. all nested control statements

  23. Why? If we have learned one language,we can easy to learn other languages.Because the logic of (method) solving problem is same. So far, we have taken more than one month to finish three assignments using C language.But I belive that all of you just spent one day to learn syntax of java,then you can finish those assignments less than one day easily.

  24. How? Be patient to learn basic knowledge,do not care about that i do not know java,ruby.. In our class,we using c. Let us C

  25. C_code_Convention • Code conventions are important to programmers for a number of reasons: • 80% of the lifetime cost of a piece of software goes to maintenance. • Hardly any software is maintained for its whole life by the original author. • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

  26. An example

  27. Code convention

  28. Basic convention 1.write down comments 2.one statement one line 3.indent every substatement 4.choose variable name close to its meaning ( a or isPrime?)

  29. If..else.. if (condition) { CS1; CS2; ... }/* end of if */ Next statement; Braces if (condition) CS; Next statement; if (condition) CS; Next statement;

  30. If ....else... if (condition) { CS1; CS2; ... } else { ES1; ES2; ... } /* end of if */ Next statement;

  31. Nested if.. else .. if (a > b) { if ( a > c) a largest; else c largest; } else { if ( b > c) b largest; else c laregest; } /* end of if */ Next statement;

  32. Nested if.. else .. int a = 5, b = 10, c = 9; if (a > b) if ( a > c) a largest; else c largest; else if ( b > c) b largest; else d laregest; Next statement; int a = 5, b = 10, c = 9; if (a > b) if ( a > c) a largest; else c largest; elseif ( b > c) b largest; else d laregest; Next statement; OUTPUT: d largest

  33. int a = 5, b = 8, c = 9, d = 10; if ( a > b) printf("1"); else if ( a > c) printf("2"); else if ( a > d) printf("3"); else printf("4"); int a = 5, b = 8, c = 9, d = 10; if ( a > b) printf("1"); elseif( a > c) printf("2"); elseif( a > d) printf("3"); else printf("4"); OUTPUT: 4

  34. Nested if (a > b) if ( a > c) a largest; else c largest; else if ( b > c) b largest; else c laregest; Next statement; Every else corresponds if which is cloest to it

  35. Nested int a = 5, b = 4, c = 6; if (a > b) { if ( a > c) printf(“1”); } else printf(“2”); Next statement; int a = 5, b = 4, c = 6; if (a > b) if ( a > c) printf(“1”); else printf(“2”); Next statement; OUTPUT: 2 OUTPUT:

  36. Tips In C,true is any nonzero value and false is zero. if (1) printf("1"); if (2) printf("2"); If (!2) printf("3"); if (0) printf("4"); If (!0) printf ("5"); Output:125

  37. isPrime

  38. Relational and logical operators •Operator Meaning < less than <= less than or equal to > grater than >= grater than or equal to == equal to != not equal to || or && and

  39. Relational Operator Meaning == equal to Assignment Operator Meaning = assign value • a == b means: a equal to b then return ture else return false • a = b means: assign the value of b to a • if ( a == b) correct  • if ( a = b) incorrect 

  40. compound condition int a = 5, b = 8, c = 9, d = 10; if ( a > b) printf("1"); else if ( a > c) printf("2"); else if ( a > d) printf("3"); else printf("4");

  41. compound condition int a = 5, b = 8, c = 9, d = 10; if ( a < b && a < c && a > 10) printf(“1”); else printf(“2”); if( true && true && false) = false if( true && true && true) = ture if( true || false|| false) = true if( false|| false || false) = false

  42. compound condition if ( 1 && 2 && 3) printf("1"); else printf("2"); if ( 1 && !2 && 3) printf("1"); else printf("2"); if ( 0 || 2 && 3 && 4) printf("1"); else printf("2"); if ( 0 || 2 && 3 && 0) printf("1"); else printf("2");

  43. Precedence of c operator

  44. if ( 10 - 9 -1 ) printf("1"); else printf("2"); Output: 2

  45. if ( 10 – (9 -1) ) printf("1"); else printf("2"); Output: 1

  46. Conditional Operator <relation expression> ? <value1> : <value2> if ( relation expression) return value1; else return value2; <relation expression> ? <statement1>:<statement2> if ( relation expression) statement1; else statement2;

More Related