CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

62
CLO 1, CLO 2 1.Explain various programming problem using design tools (C2) 2. Apply knowledge of basic concepts of fundamental programming to solving a given problem(C3) 2.0 INTRODUCTORY CONCEPT

description

2.0 INTRODUCTORY CONCEPT. CLO 1, CLO 2 1.Explain various programming problem using design tools (C2) 2. Apply knowledge of basic concepts of fundamental programming to solving a given problem(C3). 2.0 INTRODUCTORY CONCEPT. At the end of this chapter, student will be able to : - PowerPoint PPT Presentation

Transcript of CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Page 1: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

CLO 1, CLO 2

1.Explain various programming problem using design tools (C2)

2. Apply knowledge of basic concepts of fundamental programming to solving a given problem(C3)

2.0 INTRODUCTORY CONCEPT

Page 2: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

At the end of this chapter, student will be able to :1. Define a variable and a constant 2. Identify the rules for naming variables and

constant,3. Declare constants and variables4. Build constant and variables 5. Explain keywords and operators in

programming6. Use keywords and operators

2.0 INTRODUCTORY CONCEPT

Page 3: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

At the end of this chapter, student will be able to :

7. Convert formula into C expression

2.0 INTRODUCTORY CONCEPT

Page 4: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1 Constant And Variables(Identifier) 2.1.1 Variable And Constant Variable

• Refer to the memory location where the data is stored

• This value keeps changing during the program execution

Page 5: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Figure 2.1(i) : Representation of Variable in Memory

Page 6: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.1 Constant• a location in the memory that stores

data that never changes during the execution of the program

• constant can either be: A numbers, like 15 or 10.5 A single character, like 'x' or '#‘ A group of characters (string),

like “Beautiful Malaysia”

Page 7: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.2 Rules For Naming Constants And Variables

• Can be a combination of alphabets and numbers but must start with an alphabet

• Comprise maximum of 40 characters• No commas or blank space is allowed• No special characters can be used

except underscore (_)

Page 8: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.3 Declare Constants And Variables

• Constant and variable used in a program must be declared in the beginning

• To specify the variable and constant data type to the compiler

Page 9: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Declare Constant

const data_type name=value ;

e. g :const float pie=3.14 ;

const int Days_of_Week=7;

const char Colour=“RED”;

Page 10: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Declare Variable

data_type name ;

e. g : float mark_1 ;

int Quantity;

char StudentID;

Page 11: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.4 Scope of Constants And Variables

DIY

Page 12: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.0 INTRODUCTORY CONCEPT

2.1 Understand Constant And Variables(Identifier) 2.1.5 Build Constants and variables

in Programmes

TotalPiePayment

Page 13: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.6 Identify and Explain Keywords in C Programmes

Are reserved words for which the meaning is already defined to the compiler.

an identifier cannot have the same spelling and case as a C keyword

Page 14: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Types Keywords

Data types, modifiers and storage class specifiers

void, int, char, float, double, signed, unsigned, long, short, auto, const, extern, static, volatile, register and typedef

User defined data types and type related

struct, union, enum and sizeof

Conditional if, else, switch, case and default

Flow control for, while, do, break, continue, goto and return

Page 15: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.7 Use Keywords In Programmes

#include<stdio.h> int main(){      printf("Hello world\n");      return 0;}

Page 16: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.7 Use Keywords In Programmes

#include<stdio.h> main(){    int a;     printf("Enter an integer\n");    scanf("%d", &a);     printf("Integer that you have entered is %d\n", a);     return 0;

Page 17: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.1.7 Use Keywords In Programmes

#include<stdio.h> main(){      char ch;       printf("Enter a character\n");      scanf("%c",&ch);       if ( ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')         printf("%c is a vowel.\n", ch);      else          printf("%c is not a vowel.\n", ch);       return 0;}  

Page 18: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.0 INTRODUCTORY CONCEPT

2.2 Understand Data Types Data Types

Data types are used to store various types of data that is processed by program. Data type attaches with variable to determine the number of

bytes to be allocate to variable and valid operations which can be performed on that variable

Page 19: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.1 Basic Data Types in CThe C language provides a lot of basic

types. Most of them are formed from one of the four basic arithmetic type identifiers in C (char, int, float and double), and optional specifiers (signed, unsigned, short, long). All available basic arithmetic types are listed below:

Page 20: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Char signed longShort signed long intShort int long longSigned short long long intSigned short int signed long longInt signed long long intSigned int floatLong doublelong int

Page 21: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.2 Basic Data Types Numeric

Page 22: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Numeric

Keyword            Variable Type                Rangechar               Character (or string) -128 to 127int                integer -32,768 to 32,767short              Short integer -32,768 to 32,767short int          Short integer -32,768 to 32,767long               Long integer

2,147,483,648 to 2,147,483,647unsigned char     Unsigned character 0 to 255unsigned int       Unsigned integer 0 to 65,535unsigned short    Unsigned short integer 0 to 65,535unsigned long      Unsigned long integer 0 to 4,294,967,295float             Single-precision +/-3.4E10^38 to +/-3.4E10^38

floating-point (accurate to 7 digits)

double             Double-precision +/-1.7E10^308 to +/1.7E10^308 floating-point (accurate to 15 digits)

Page 23: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

CharactersC stores character type internally as an integer. Each character has 8 bits so we can have 256 different characters

values (0- 255).Character set is used to map between an integer value and a character. The most common character set is ASCII.

The char type store only one symbol of data (letter, digit, space, tab and so on).

Page 24: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

String

The string type is able to keep virtually data of any length. Also string type is

based on arrays and uses reference type working with memory.

Page 25: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.3 Explain The Basic Data Types in Ca. int int is used to define integer numbers.

{ int Count; Count = 5;

}

Page 26: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

b. Char

char defines characters.{

char Letter; Letter = 'x';

}

Page 27: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

c. floatfloat is used to define floating point numbers.

{ float Miles; Miles = 5.6;

}

Page 28: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

d. Doubledouble is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.

{ double Atoms; Atoms = 2500000;

}

Page 29: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.4 Use data types in C programmes#include<stdio.h> main(){    int a;     printf("Enter an integer\n");    scanf("%d", &a);    printf("Integer that you have entered is %d\n", a);     return 0;}

Page 30: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.4 Use data types in C programmes#include<stdio.h>

main(){

 char  Name;

 printf("Enter your name\n"); scanf("%c", &Name);

 printf("Your name is  %c\n", Name);

 return 0;}

Page 31: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.4 Use data types in C programmes#include<stdio.h> main(){

 float Mark;

 printf("Enter your mark\n"); scanf("%f", &Mark);

  printf("Mark that you have entered is %f\n", Mark);

return 0;}

Page 32: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.2.4 Use data types in C programmes#include <stdio.h>#define PI 3.14159

int main(void){

 double radius,area,parameter;

 radius = 1.0; area = PI * radius * radius; parameter = 2 * PI * radius;

 printf("Area %f\n", area); printf("Parameter %f", parameter);

 return 0;}

Page 33: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3 Understand Operators And Expression

Operator • Operators are symbols which take one or

more operands or expressions and perform arithmetic or logical computations.

Page 34: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.1 Types Of OperatorTypes of operators available in C are as follows:

ArithmeticMathematical operators are used for performing simple

mathematical calculations such as addition, subtraction,

multiplication and division.

Page 35: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Relational

Relational operators are used to compare twooperands.

Page 36: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Logical

• Logical operators are used to combine two simple statements into a compound statement.

• Using logical operators, you can simulate Boolean algebra in C.

Page 37: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Boolean

Boolean operators define the relationshipsbetween words or groups of words.

True=1

False=0

Page 38: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.2 Operators used in programming3 types :a. Mathematic operatorsb. Relational operatorsc. Logical operators

Page 39: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.3 a.ArithmeticOperator Action- Substraction+ Addition/ Divide* Multiplication% Modulus Division++ Increment-- Decrement

Page 40: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Operator precedence :

i)bracket ()ii) *,/,% (left to right)iii)+,- (left to right)

Page 41: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Example 1

Page 42: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Example 2

Page 43: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

b.Relational

Operator Meaning< Less than

<= Less than or equal> Greater than

>= Greater than or equal== Equal!= Not equal

Page 44: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

c. Logical

Operator Meaning&& AND|| OR! NOT

Page 45: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

The AND (&&) operator will evaluate to trueonly if all the conditions in the expressionreturns a true value.

Example You will pass the exam only when you get more than 50 in subject 1, subject 2 and subject3.

 

Page 46: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

The OR (||) operator will evaluate to true even if one of the conditions is true.

ExampleTo get a medal, you should either be 1st or

2nd in the race.

Page 47: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

The NOT (!) operator will evaluate to true if the condition fails and vice versa.  Example

If your sex is not M, you are girl

Page 48: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.4 Use Operators in Expression Statements

Page 49: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Mathematical Notation C Expression

xy x * y

a+ba-b

(a+b)/(a-b)

(pq)-(rt) (p*q)-(r*t)

Page 50: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Description C Expressionx is greater than y x> ym is less than n m<np is less than or equal to 50 p <=50q is greater than or equal to 80 q>=80(x+y) is greater than or equal to z (x+y) >=za is not equal to 5 a!=5(a+b) is greater than (c+d) (a+b) > (c+d)q is equal to 10 q==10c is not equal to (a+b) c != (a+b)i is less than 100 i > 100

Page 51: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Statement C ExpressionYou will pass the exam only when you get more than 50 in subject 1, subject 2 and subject3.

if  ((subject1>=50)&&(subject2>=50)&&(subject3>=50))     printf("PASS")   ;

To get a medal, you should either be 1st or 2nd in the race.

if  ((position==1)||  (position==2))printf("You got medal")   ;

If your sex is not M, you are girl

if (!(sex =='M') ){printf("You are girl");

Page 52: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.5 Formula VS ExpressionFormula

• Equation or a set of instructions that solves a certain type of problems in a prescribed manner.

• In a formula, the same set of inputs always produces the same output(s).

Page 53: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

e.g:

Net Pay = Gross Pay – DeductionsCircle Area = ¶R²Voltage = Current + Resistant  R = ρ x L/A

Page 54: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Expression• Expression in C++ are formed by

properly combining operators, variables and constants.

• We have already seen some examples of simple arithmetic and logical expression

• As algebra, C++ expression can be complex.

• Parentheses can be used to force the order of evaluation. An expression may also contain spaces for readability.

Page 55: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

e.g:

Gross_pay – deductions(basic_pay + hours * rate) – (socso + premium + loan)

(b * b – 4 * a * c) > 0(gender = = ‘m’) && (age > 20)(gender = = ‘m’ || gender = = ‘f’ ) && age >= 21

Page 56: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.6 Convert Formula Into C ExpressionFormula C Expression

Net Pay = Gross Pay – Deductions NetPay = GrossPay – Deductions

 Area = ¶R² Area = Pie x Radius * Radius

V = IR V = Current  x Resistant  

R = L L _                A

Resistivity= Rho x(Length/Area)

Page 57: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.7 Operand And Operator

An operation is an action performed on one or more values either to modify the value held by one or both of the variables or to produce a new value by combining variables. Therefore, an operation is performed using at least one symbol and one value. The symbol used in an operation is called an operator. A variable or a value involved in an operation is called an operand.

Page 58: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

Area = Pie x Radius * Radius

Operator

Operand

Assignment (=) Arithmetic(+,-…..)Relational(<,>…)Logical(&&,||,!)

Variable or Value

Page 59: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.8 Expressions In Application

first + secondfirst – secondfirst * secondfirst / second

Page 60: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.8 Expressions In Application

diam/2PI * r * r

2 * PI * r

Page 61: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

2.3.8 Expressions In Application

diam/2area=PI * r * r

circ = 2 * PI * r

Page 62: CLO 1, CLO 2 1.Explain various programming problem using design tools (C2)

CONSTANTS AND

VARIABLES

OPERATORS AND

EXPRESSIONS

2.INTRODUCTORY

CONCEPT

DATA TYPES

Definition

Rules For Naming

Declaration

Scope

Build

Keywords

Basic Data Types

Types of Operators

Formula VS Expression

Convert Formula into C Expression

Operand and Operator