Computer Programming and Problem Solving
description
Transcript of Computer Programming and Problem Solving
![Page 1: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/1.jpg)
Computer Programming and Problem Solving
Ke shuwei
![Page 2: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/2.jpg)
1. What is the meaning of problem solve?
2. How to solve it?
3. What is computer programming?
4. What is basic knowledge for solving?
![Page 3: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/3.jpg)
Computer Problem
1. the problem about computer itself
Blue Screen of Death (BSoD)
![Page 4: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/4.jpg)
![Page 5: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/5.jpg)
Computer Problem
2. 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.
![Page 6: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/6.jpg)
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
![Page 7: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/7.jpg)
Computer Problem
The computer can not solve the problem which a primary student can solve.why we need it?
![Page 8: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/8.jpg)
Computer problemIf 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 () { .....
..... }
![Page 9: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/9.jpg)
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 .....
![Page 10: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/10.jpg)
What is the computer programming?
Utilize any language code to solve the problem in computer.
![Page 11: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/11.jpg)
C or Java or ...?
1. Same Target:solve the problem.
2. There are thousands of Languages,which one shoud pick?
![Page 12: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/12.jpg)
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?
![Page 13: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/13.jpg)
What can we do?
• Language is nothing more than a tool to solve the problem.
Learn basic knowledge for all languages
![Page 14: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/14.jpg)
Basic knowledgeProblem: 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.
![Page 15: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/15.jpg)
C
![Page 16: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/16.jpg)
Java
![Page 17: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/17.jpg)
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
![Page 18: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/18.jpg)
One more problem
• find all the prime number between 1 and 100.
![Page 19: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/19.jpg)
C
![Page 20: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/20.jpg)
Java
![Page 21: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/21.jpg)
Connection
• if control statements• for conrol statements• operator ( %, = , <= ..)
• Syntax is different• Algorithm(logic) is different
![Page 22: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/22.jpg)
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.
![Page 23: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/23.jpg)
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
![Page 24: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/24.jpg)
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.
![Page 25: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/25.jpg)
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
![Page 26: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/26.jpg)
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.
![Page 27: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/27.jpg)
An example
![Page 28: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/28.jpg)
Code convention
![Page 29: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/29.jpg)
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?)
![Page 30: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/30.jpg)
If..else..
if (condition){
CS1;CS2;...
}/* end of if */
Next statement;
if (condition) CS;
Next statement;
if (condition) CS;
Next statement;
Braces
![Page 31: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/31.jpg)
If ....else...
if (condition){
CS1;CS2;...
}else{
ES1;ES2;...
} /* end of if */
Next statement;
![Page 32: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/32.jpg)
Nested if.. else ..if (a > b){
if ( a > c)a largest; elsec largest;
}else{
if ( b > c)b largest;
elsec laregest;
} /* end of if */
Next statement;
![Page 33: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/33.jpg)
Nested if.. else ..int a = 5, b = 10, c = 9;if (a > b)
if ( a > c)a largest; elsec 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; elsec largest;
else if ( b > c)b largest;
elsed laregest;
Next statement;
OUTPUT: d largest
![Page 34: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/34.jpg)
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"); else if ( a > c) printf("2"); else if ( a > d) printf("3"); else printf("4");
OUTPUT: 4
![Page 35: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/35.jpg)
![Page 36: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/36.jpg)
Nested
if (a > b)if ( a > c)
a largest; elsec largest;
elseif ( b > c)
b largest;else
c laregest;
Next statement;
Every else corresponds if which is cloest to it
![Page 37: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/37.jpg)
Nestedint a = 5, b = 4, c = 6;if (a > b)
if ( a > c)printf(“1”);
elseprintf(“2”);
Next statement;
OUTPUT: 2
int a = 5, b = 4, c = 6;if (a > b){
if ( a > c)printf(“1”);
}elseprintf(“2”);
Next statement;
OUTPUT:
![Page 38: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/38.jpg)
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
![Page 39: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/39.jpg)
isPrime
![Page 40: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/40.jpg)
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
![Page 41: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/41.jpg)
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
![Page 42: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/42.jpg)
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");
![Page 43: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/43.jpg)
compound conditionint a = 5, b = 8, c = 9, d = 10;
if ( a < b && a < c && a > 10) printf(“1”);
elseprintf(“2”);
if( true && true && false) = false
if( true && true && true) = ture
if( true || false|| false) = true
if( false|| false || false) = false
![Page 44: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/44.jpg)
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");
![Page 45: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/45.jpg)
Precedence of c operatorOperator category Operators Precedence
Parentheses,brace ( ), [ ] 1
Unary operator -,++, --, !, `, & 2
Multiplicative operators *, /, % 3
Additive operator +, - 4
Shift operators <<, >> 5
Relation operators <, <=, >, >= 6
Equality operator ==, != 7
Bitwise operator &, ^, | 8
Logical operators &&,|| 9
Conditional operators ?, : 10
Assignment operators =,+=,-=,*=, /=,%= &=, ^=, |=, <<=,>>=
11
Comma operator , 12
![Page 46: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/46.jpg)
if ( 10 - 9 -1 ) printf("1"); else printf("2");
Output: 2
![Page 47: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/47.jpg)
if ( 10 – (9 -1) ) printf("1"); else printf("2");
Output: 1
![Page 48: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/48.jpg)
Conditional Operator
<relation expression> ? <value1> : <value2>
if ( relation expression) return value1;
else return value2;
if ( relation expression)statement1;
else statement2;
<relation expression> ? <statement1>:<statement2>
![Page 49: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/49.jpg)
![Page 50: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/50.jpg)
![Page 51: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/51.jpg)
ExampleC = 5 < 4 ? 5 : 4;
3 1 2 2 precedence operator
Return value
5 > 4 ? printf("1") : printf("2");
statemment
max = a > b ? ( a > c ? a : c) : ( b > c ? b :c);
![Page 53: Computer Programming and Problem Solving](https://reader035.fdocuments.in/reader035/viewer/2022062217/56814b8d550346895db87353/html5/thumbnails/53.jpg)
Assignment1.Find the largest number in four numbers.Draw flawchart.(10,2,3,20)
2.Find the largest number in four numbers.(using conditional operator)
3.Enter the month,printout the season 11,12,1 -- winter 2, 3, 4 --- spring 5, 6, 7 --- summer 8, 9, 10 -- autumn
4.Enter the marks,printout grade.
90 - 100 A 80 – 90 B 70 – 80 C 60 – 70 D below 60 fail