Tải bản đầy đủ (.ppt) (33 trang)

Programming Concepts (Part A) ENGR 10 Introduction to Engineering pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (336.16 KB, 33 trang )

Programming Concepts
(Part A)
ENGR 10
Introduction to Engineering
What is a program?
WHITE CAKE RECIPE
1. Preheat oven to 350 degrees F (175 degrees C).
2. Grease and flour a 9x9 inch pan.
3. In a medium bowl, cream together the sugar and butter .
4. Beat in the eggs.
5. Stir in the vanilla.
6. Combine flour and baking powder, add to the creamed mixture and mix well.
7. Stir in the milk until batter is smooth.
8. Pour or spoon batter into the prepared pan.
9. Bake for 30 to 40 minutes in the preheated oven.
A computer program is an ordered
list of instructions.

It’s like a recipe!
In Summary…
A math example…
y = [ 3 × ( a × a + 7) ] / b + 4
a
2
a
2
+ 7
3 × (a
2
+ 7) = 3a
2


+ 21
(3a
2
+ 21) / b
(3a
2
+ 21) / b + 4
Sequential Solving
following Math
conventions for correct
answer.
Consider the sequential execution of the
above expressions to find the value of y:

p = a x a;

q = p + 7;

r = 3 x q;

s = r / b;

y = s + 4;
y = [ 3 × ( a × a + 7) ] / b + 4
An Empty EasyC program
C program
Flow chart
Your
program
goes

here.
Your
program
goes
here.
C program
Logic flow
An instruction
in C
Variables

A “variable” is a place where we keep a value.
A = 10 ; // The value 10 is stored in A.

Any statement after the sign “//” is NOT part of the
program. It is a comment made by the programmer.
WSalary = 800 ; // Any statement can go here.

A semicolon is required at the end of each C instruction.

Variables, cont.
Every variable in the program needs to be
declared in the beginning of the program.
int speed ;

Declaration of the variable tells
the program its name and its type
The word “int” indicates that
the variable ‘speed’ is an
integer variable.

Commonly Used Variable Types
Variable
Type
Description Range
Int
Stores integer values
EX: 5, -3200
-32,768 to +32,767
Long
Stores integer values with
extended range
EX: 56, 6000, -4,234,128
-2,147,483,648 to
+2,147,483,647
float
Stores values with decimal
point
EX: 1.245, -4.2341
[-10^+38, -10^-38]
0
[10^-38, 10^+38]
char
Stores characters*
EX: A, B, @, #
_

Global Variable:
This variable is accessible from
anywhere within your program.


Local Variable:
This variable is only accessible from a
“local area” within your program (“functions”
will be discussed later).

Global Variable Declaration
To declare a global variable, click on the tab
“Global” in the Main program.
Global Variable Declaration
(A close-up view)
Assignment Operator

In C language “ = ” is an assignment operator,
not an “ is equal to ” statement.

A = 10; // means assign 10 to A.

A = B; // means assign the value of B to A

A = A+1; //means assign the value A+1
// back to A, i.e., increase A by 1.
Assignment Operator: Examples:
int A; // a variable named A of type integer
int B; // a variable named B of type integer
A = 10; // value 10 is assigned to variable A
B = (24+16)/2; // 20 is assigned to variable B
A = A + 15; // value of (A+15) is first evaluated
and then assigned to A.
So now A=(10+15)=25
B = A + B ; // Now A = 25, B = (25+20)=45

A = B – 40; // Now A=(45-40)=5, B=45
A = A * B; // Now A=(5*45)=225, B=45
B = A / 9; // Now A=225, B=25
Q1: What is the value of B at the
end of this program?
int A;
int B;
A = 12;
B = 15;
A = A + (B/3) ;
B = A + B – 7 ;
(A)12, (B)15, (C)17, (D)20, (E)25
Decision Making

The ability to make decision is the most basic
form of intelligence.

A linear (sequential) execution of instructions
can only perform a simple task that does not
involve decision making.

The IF instruction gives a C program decision
making ability
IF Statement: Simple Example
if (Number == 0)
{
PrintToScreen (“The Number is Zero”);
}
if (Number < 0)
{

PrintToScreen (“The Number is negative”);
}
This == symbol is used for
checking ‘equal’ condition,
not for value assignment.
Only if this condition is true,
this instruction is executed.
IF-ELSE Statement
Consider the following program:
If (score <60)
{ PrintToScreen(“You failed the test”);}
If (score == 60) // ‘==‘ means ‘if equal to’
{ PrintToScreen(“You passed the test”);}
If (score > 60)
{ PrintToScreen(“You passed the test”);}
IF-ELSE Statement

IF-ELSE statement should be used where there
are only two possible cases.
If (score <60)
{ PrintToScreen(“You failed the test”);}
else
{ PrintToScreen(“You passed the test”);}
Note: ( ) not { }
Q2: What is the value of A and B
at end of this program?
A = 9 ;
B = 12 ;
if ((A + B) > 22)
{ A = B ;

B = A; }
else
{ B = A;
A = B; }
(A) A=12, B=21
(B) A=9, B=12
(C) A= 9, B=9
(D) A=12, B=12
(E) A=12, B=9
WHILE Statement

In C, the WHILE statement is useful for
repeating a set of instructions

Suppose we have to add the first 50 positive
integers
1+2+3+4+……………………+48+49+50
First Approach:

Use a single statement:
int SUM ; // integer variable for the result
SUM = 1+2+3+……………… +48+49+50;
Second Approach:

int SUM ; //variable SUM is declared as integer
SUM = 0 ; //SUM is assigned the value 0
SUM = SUM + 1 ; // Now SUM = (0 + 1) = 1
SUM = SUM + 2 ; // SUM = (1 + 2) = 3
SUM = SUM + 3 ; // SUM = (3 + 3) = 6
SUM = SUM + 49 ; //SUM = (1176 + 49) = 1225

SUM = SUM + 50; //SUM = (1225 + 50) = 1275
Third approach:
Use “while” statement
This condition is checked
first. If it is true, the block
of instructions enclosed
by the curly brackets { } is
executed.
This block of instructions
is executed repeatedly
until the condition is not
true.
While Statement - Example

To find factorial of a number N.
What is a factorial?
Factorial of a number N is
N! = 1x2x3x………………x(N-1)xN

1! = Factorial (1) = 1

5! = Factorial (5) = 1x2x3x4x5 = 120

7! = Factorial (7) = 1x2x3x4x5x6x7 = 5040

×