Computer Programming and Problem Solving Ke shuwei.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 3 - Structured Program Development
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
4 Control Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
C++ for Engineers and Scientists Third Edition
Structured Program Development in C
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Lecture 3 Structured Program Development in C
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
AP Computer Science Programming Conventions. Why coding conventions? 80% of the lifetime cost of a piece of software goes to maintenance. Hardly any.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Code Conventions Tonga Institute of Higher Education.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Decision making If.. else statement.
The if…else Selection Statement
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Week 3 - Friday CS222.
- Standard C Statements
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
SELECTION STATEMENTS (1)
Lecture 2: Logical Problems with Choices
Introduction to C++ Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Decision making If statement.
Chapter 3 - Structured Program Development
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Relational and Logical Operators
Branching statements Kingdom of Saudi Arabia
Relational and Logical Operators
Dale Roberts, Lecturer IUPUI
Relational and Logical Operators
Controlling Program Flow
Lecture 9: Implementing Complex Logic
Structural Program Development: If, If-Else
Presentation transcript:

Computer Programming and Problem Solving Ke shuwei

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?

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

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.

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

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

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 main () {..... }

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

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

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

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?

Language is nothing more than a tool to solve the problem. Learn basic knowledge for all languages

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.

C

Java

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

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

C

Java

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

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.

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

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.

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

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.

An example

Code convention

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?)

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

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

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; 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;

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; 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; 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; else if ( b > c) b largest; else d laregest; Next statement; OUTPUT: d largest

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

Nested if (a > b) if ( a > c) a largest;else c largest; else if ( b > c) b largest; else c laregest; Next statement; 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

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 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:

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

isPrime

Relational and logical operators OperatorMeaning <less than <=less than or equal to >grater than >=grater than or equal to ==equal to !=not equal to ||or &&and

Relational OperatorMeaning ==equal to Assignment OperatorMeaning = 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 

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");

compound condition int a = 5, b = 8, c = 9, d = 10; if ( 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

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");

Precedence of c operator Operator categoryOperatorsPrecedence 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

if ( ) printf("1"); else printf("2"); Output: 2

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

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

Example C = 5 < 4 ? 5 : 4; precedence operator Return value 5 > 4 ? printf("1") : printf("2"); statemment max = a > b ? ( a > c ? a : c) : ( b > c ? b :c);

Thank you!

Assignment 1.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, spring 5, 6, summer 8, 9, autumn 4.Enter the marks,printout grade A 80 – 90 B 70 – 80 C 60 – 70 D below 60 fail