bda 24202 computer programming

46
BDA 24202 COMPUTER PROGRAMMING Ts. Dr. Norfazillah Talib JKP, FKMP

Upload: others

Post on 10-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BDA 24202 COMPUTER PROGRAMMING

BDA 24202COMPUTER PROGRAMMING

Ts. Dr. Norfazillah Talib

JKP, FKMP

Page 2: BDA 24202 COMPUTER PROGRAMMING

C language

- Structure programming language

- Programmer able to focus on problem rather than machine that program will be execute

- Able to run at various hardware platform

Page 3: BDA 24202 COMPUTER PROGRAMMING

Structure C programming

Preprocessor directive (processing directive)

Global declaration

Local declaration

Main function

Page 4: BDA 24202 COMPUTER PROGRAMMING

Greeting program

• Task: Display “Hello world!”

Page 5: BDA 24202 COMPUTER PROGRAMMING

Comments

• Character that are not executed as part of the program.

• Use for mark or debug the code

• Syntax:

/* …. Comments…..

Comment………….*/ block comments

//…. Comment line comments (for 1 line only)

Page 6: BDA 24202 COMPUTER PROGRAMMING

Preprocessor directive

• Not the part of the compiler but it is directive to preprocessor

Page 7: BDA 24202 COMPUTER PROGRAMMING

Variables

Variables are named memory locations that have a type,such as integer or character, which is inherited from theirtype. The type determines the values that a variable maycontain and the operations that may be used with itsvalues.

Variable Declaration

Variable Initialization

Topics discussed in this section:

Page 8: BDA 24202 COMPUTER PROGRAMMING
Page 9: BDA 24202 COMPUTER PROGRAMMING

• int new;

• float maxItems; //word separator: capital

• double max_items; // word separator: underscor

• int new, old, great; //combination of same type variable

• Int loadA=30, loadB=15; //initialize the variable

Examples of Variable Declarations and Definitions

Page 10: BDA 24202 COMPUTER PROGRAMMING

10

When a variable is defined, it is not initialized.

We must initialize any variable requiring

prescribed data when the function starts.

Note

Page 11: BDA 24202 COMPUTER PROGRAMMING

Identifiers

One feature present in all computer languages is theidentifier. Identifiers allow us to name data and otherobjects in the program. Each identified object in thecomputer is stored at a unique address.

Page 12: BDA 24202 COMPUTER PROGRAMMING

Rules for Identifiers1. First character must be alphabetic character or underscore

2. Must consist only of alphabetic characters, digits or underscores

3. Commas or blank spaces are not allowed within an identifier.

4. First 63 characters of an identifier are significant

5. Cannot duplicate a keyword (reserved word).

6. Identifiers are also case sensitive in C (e.g name and Name are two different identifiers in C).

7. No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier

Page 13: BDA 24202 COMPUTER PROGRAMMING

Reserved word

Page 14: BDA 24202 COMPUTER PROGRAMMING

ExerciseDetermine whether the following identifiers is valid or invalid. Give reason for invalid cases.1) int Parit Raja 2) int 20thCentury 3) int char4) long float INTEGER5) int _BMW2003 6) int Reservedword7) float BDA24202 8) long int markah_pelajar 9) char jam*kredit 10) double printf

Page 15: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

15

Constant/ Escape Character

Page 16: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

16

Table 2-10 Format Codes for Output

Page 17: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

17

Streams

Formatting Input/Output

Topics discussed in this section:

Page 18: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

18

FIGURE 2-15 Stream Physical Devices

Page 19: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

19

FIGURE 2-18 Conversion Specification

Page 20: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

20

Table 2-11 Flag Formatting Options

Page 21: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

21

FIGURE 2-17 Output Stream Formatting Example

Page 22: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

22

FIGURE 2-20 Input Stream Formatting Example

scanf

Page 23: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

23

scanf requires variable addresses in the address list.

Note

Page 24: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

24

PROGRAM Write A Program That Prints “Nothing!”

#include<conio.h>

getchar();

\a\a\a

Page 25: BDA 24202 COMPUTER PROGRAMMING

Output

Page 26: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

26

PROGRAM Print Sum of Three Numbers

Page 27: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

27

PROGRAM Print Sum of Three Numbers (continued)

Page 28: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

28

PROGRAM Print Sum of Three Numbers (continued)

Page 29: BDA 24202 COMPUTER PROGRAMMING

Write a program to add two numbers

EXERCISE

Page 30: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

30

Constants

Constants are data values that cannot be changed duringthe execution of a program. Like variables, constantshave a type. In this section, we discuss Boolean,character, integer, real, complex, and string constants.

Constant Representation

Coding Constants

Topics discussed in this section:

Page 31: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

31

Memory ConstantsPROGRAM

Page 32: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

32

PROGRAM Memory Constants (continued)

Page 33: BDA 24202 COMPUTER PROGRAMMING

ASCII Table

(American Standard

Code for Information

Interchange)

Page 34: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

34

PROGRAM Print Value of Selected Characters

Page 35: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

35

PROGRAM Print Value of Selected Characters (continued)

Page 36: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

36

PROGRAM Print Value of Selected Characters (continued)

Page 37: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

37

PROGRAM Print Value of Selected Characters (continued)

Page 38: BDA 24202 COMPUTER PROGRAMMING

OUTPUT#include <stdio.h>int main(){char A =‘A’;char a =‘a’;char zed =‘z’;char eight =‘8’;char dblQuote =‘”’;printf (“ASCII for char ‘A’ is: %d\n”, A);printf (“ASCII for char ‘a’ is: %d\n”, a);printf (“ASCII for char ‘z’ is: %d\n”, zed);printf (“ASCII for char ‘8’ is: %d\n”, eight);printf (“ASCII for char ‘\”’ is: %d\n”, dblQuote);return 0;}

EXAMPLE

Page 39: BDA 24202 COMPUTER PROGRAMMING
Page 40: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

40

OUTPUT

Write a program that can calculate the circumference and area

of circle once radius of the circle is filled.

Page 41: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

41

PROGRAM Calculate a Circle’s Area and Circumference

Page 42: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

42

PROGRAM Calculate a Circle’s Area and Circumference

Page 43: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

43

PROGRAM A Sample Inventory Report

Page 44: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

44

PROGRAM A Sample Inventory Report (continued)

Page 45: BDA 24202 COMPUTER PROGRAMMING

Computer Science: A Structured Programming Approach Using C

45

FIGURE 2-22 Output Specifications for Inventory Report

Page 46: BDA 24202 COMPUTER PROGRAMMING

Price: RM _______

Quantity: ______

Total price: RM_________

Thanks for shopping here!

Output

Develop a program that can calculate the total price of goods

once the price and quantity of goods are filled.