The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by j05stm21f504, 2022-10-01 02:55:45

Topic 4 - Basic Programming Codes (3)

Topic 4 - Basic Programming Codes (3)

1

Course Learning Outcome (CLO):

Upon completion of this course, students should be
able to:

1) Explain the basic computer and programming
fundamentals with appropriate examples of language
and technology.

2) Apply the different types of algorithm to solve problem
efficiently.

3) Solve problem effectively by applying related theories of
the basic programming language to a given particular
scenario using programming life cycle.

2

3

4

COMMENT

⚫ Comments are simply text that is ignored by the
compiler, but that may inform the reader of what you
are doing at any particular point in your program.

⚫ When you are writing a program, it is always clear and
self-evident what you are trying to do. Funny thing,
though--a month later, when you return to the program, it
can be quite confusing and unclear.

⚫ To fight the onset of confusion, and to help others
understand your code, use comments.

5

6

HELP.CPP demonstrates comments.

1: #include <iostream.h>
2:
3: int main()
4: {
5: /* this is a comment
6: and it extends until the closing
7: star-slash comment mark */
8: cout << "Hello World!\n";
9: // this comment ends at the end of the line
10: cout << "That comment ended!\n";
11:
12: // double slash comments can be alone on a line
13: /* as can slash-star comments */
14: return 0;
15: }

7

OUTPUT

Hello World!
That comment ended!

8

Other example of comment

/************************************************************
Program: Hello World
File: Hello.cpp
Function: Main (complete program listing in this file)
Description: Prints the words "Hello world" to the screen
Author: Jesse Liberty (jl)
Environment: Turbo C++ version 4, 486/66 32mb RAM, Windows 3.1
DOS 6.0. EasyWin module.
Notes: This is an introductory, sample program.
Revisions: 1.00 10/1/94 (jl) First release 1.01 10/2/94 (jl) Capitalized
"World"

************************************************************/

9

10

11

12

13

14

15

16

17

Identifiers (constant and variables)
cont..

3. Variables
In C++ a variable is a place to store information. A
variable is a location in your computer's memory in
which you can store a value and from which you can
later retrieve that value.

18

types

Type Size Values
unsigned short int 0 to 65,535
short int 2 bytes -32,768 to 32,767
unsigned long int 2 bytes 0 to 4,294,967,295
4 bytes -2,147,483,648 to
long int 2,147,483,647
4 bytes -32,768 to 32,767
int (16 bit) -2,147,483,648 to
2 bytes 2,147,483,647
int (32 bit) 0 to 65,535
4 bytes 0 to 4,294,967,295
unsigned int (16 bit) 256 character values
unsigned int (32 bit) 2 bytes 1.2e-38 to 3.4e38
char 2 bytes 2.2e-308 to 1.8e308
float 1 byte
double 4 bytes
8 bytes

19

Defining a Variable

Example 1
main()
{
int x;
int y;
int z;
z = x * y;
}

Example 2
main ()
{

int Width;
int Length;
float Area;
Area = Width * Length; }

20

A demonstration of the use of variables.

1: // Demonstration of variables 21
2: #include <iostream.h>
3:
4: int main()
5: {
6: int Width = 5, Length;
7: Length = 10;
8:
9: // create integer of area and initialize with result
10: // of multiplying Width by Length
11: int Area = Width * Length;
12:
13: cout << "Width:" << Width << "\n";
14: cout << "Length: " << Length << endl;
15: cout << "Area: " << Area << endl;
16: return 0;
17: }

OUTPUT

Width:5
Length: 10
Area: 50

22

23

24

25

26

QUIZ 1

Q. What is the difference between // comments and /*
style comments?

A. The double-slash comments (//) "expire" at the
end of the line.

Slash-star (/*) comments are in effect until a
closing comment (*/). Remember, not even the
end of the function terminates a slash-star
comment; you must put in the closing comment
mark, or you will get a compile-time error.

27

QUIZ 2

1. What is the only function all C programs must contain?
A. start()
B. system()
C. main()
D. program()

2. What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( and )

3. What punctuation ends most lines of C code?
A. .
B. ;
C. :
D. '

28

QUIZ 2 ANSWER

1. What is the only function all C programs must contain?
A. start()
B. system()
C. main()
D. program()

2. What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( and )

3. What punctuation ends most lines of C code?
A. .
B. ;
C. :
D. '

29

30

31

32

33

34

35

36

37

38

Algorithm

39

Pseudo code

40

5. Flowchart

41

42

43

Answer

⚫ Problem analysis

Input : m1, m2, m3, m4 and m5
Process
: Total = m1 + m2 + m3 + m4 + m5
Output Ave = Total / 5

: Total, Ave

44

⚫ Algorithm

1. input 5 test marks
2. Calculate the average of 5 test marks

2.1 Total = m1+m2+m3+m4+m5
2.2 Ave = total/5
3. Print Total and Ave

45

⚫ Pseudocode

Start:
Process:

1.Input 5 test mark, m1, m2, m3, m4, m5
2.Total = m1 + m2 + m3 + m4 + m5
3.Ave = Total / 5
4.Print Total, Ave
End:

46

⚫ Flowchart

Start

Input m1, m2,
m3, m4, m5

Total = m1 + m2 +
m3 + m4 + m5

Ave = Total / 5

Print Total,
Ave

End

47

⚫ Code Statement 48

#include <iostream.h>

void main ()
{

int m1,m2,m3,m4,m5;
int total;
float ave;

cout<<”Please enter your marks”<<endl;
cout<<”Mark 1:”<<endl;
cin>>m1;
cout<<”Mark 2:”<<endl;
cin>>m2;
cout<<”Mark 3:”<<endl;
cin>>m3;
cout<<”Mark 4:”<<endl;
cin>>m4;
cout<<”Mark 5”<<endl;;
cin>>m5;
total=m1+m2+m3+m4+m5;
Ave=total/5;
cout<<”Total:”<<total<<endl;
Cout<<“Average:”<<ave<<endl;
return 0;
}


Click to View FlipBook Version