bda 24202 computer programming

Post on 10-Jan-2022

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

BDA 24202COMPUTER PROGRAMMING

Ts. Dr. Norfazillah Talib

JKP, FKMP

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

Structure C programming

Preprocessor directive (processing directive)

Global declaration

Local declaration

Main function

Greeting program

• Task: Display “Hello world!”

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)

Preprocessor directive

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

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:

• 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

10

When a variable is defined, it is not initialized.

We must initialize any variable requiring

prescribed data when the function starts.

Note

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.

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

Reserved word

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

Computer Science: A Structured Programming Approach Using C

15

Constant/ Escape Character

Computer Science: A Structured Programming Approach Using C

16

Table 2-10 Format Codes for Output

Computer Science: A Structured Programming Approach Using C

17

Streams

Formatting Input/Output

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C

18

FIGURE 2-15 Stream Physical Devices

Computer Science: A Structured Programming Approach Using C

19

FIGURE 2-18 Conversion Specification

Computer Science: A Structured Programming Approach Using C

20

Table 2-11 Flag Formatting Options

Computer Science: A Structured Programming Approach Using C

21

FIGURE 2-17 Output Stream Formatting Example

Computer Science: A Structured Programming Approach Using C

22

FIGURE 2-20 Input Stream Formatting Example

scanf

Computer Science: A Structured Programming Approach Using C

23

scanf requires variable addresses in the address list.

Note

Computer Science: A Structured Programming Approach Using C

24

PROGRAM Write A Program That Prints “Nothing!”

#include<conio.h>

getchar();

\a\a\a

Output

Computer Science: A Structured Programming Approach Using C

26

PROGRAM Print Sum of Three Numbers

Computer Science: A Structured Programming Approach Using C

27

PROGRAM Print Sum of Three Numbers (continued)

Computer Science: A Structured Programming Approach Using C

28

PROGRAM Print Sum of Three Numbers (continued)

Write a program to add two numbers

EXERCISE

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:

Computer Science: A Structured Programming Approach Using C

31

Memory ConstantsPROGRAM

Computer Science: A Structured Programming Approach Using C

32

PROGRAM Memory Constants (continued)

ASCII Table

(American Standard

Code for Information

Interchange)

Computer Science: A Structured Programming Approach Using C

34

PROGRAM Print Value of Selected Characters

Computer Science: A Structured Programming Approach Using C

35

PROGRAM Print Value of Selected Characters (continued)

Computer Science: A Structured Programming Approach Using C

36

PROGRAM Print Value of Selected Characters (continued)

Computer Science: A Structured Programming Approach Using C

37

PROGRAM Print Value of Selected Characters (continued)

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

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.

Computer Science: A Structured Programming Approach Using C

41

PROGRAM Calculate a Circle’s Area and Circumference

Computer Science: A Structured Programming Approach Using C

42

PROGRAM Calculate a Circle’s Area and Circumference

Computer Science: A Structured Programming Approach Using C

43

PROGRAM A Sample Inventory Report

Computer Science: A Structured Programming Approach Using C

44

PROGRAM A Sample Inventory Report (continued)

Computer Science: A Structured Programming Approach Using C

45

FIGURE 2-22 Output Specifications for Inventory Report

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.

top related