Vous êtes sur la page 1sur 48

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.

COMME NT
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.

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: }

OUTP UT

Hello World! That comment ended!

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" ************************************************************/

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.

types
Type
unsigned short int short int unsigned long int long int int (16 bit) int (32 bit) unsigned int (16 bit) unsigned int (32 bit) char float double 2 bytes 2 bytes 4 bytes 4 bytes 2 bytes 4 bytes 2 bytes 2 bytes 1 byte 4 bytes 8 bytes

Size

Values
0 to 65,535 -32,768 to 32,767 0 to 4,294,967,295 -2,147,483,648 to 2,147,483,647 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 0 to 65,535 0 to 4,294,967,295 256 character values 1.2e-38 to 3.4e38 2.2e-308 to 1.8e308

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; }

A demonstration of the use of variables.


1: // Demonstration of variables 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: }

OUTP UT
Width:5 Length: 10 Area: 50

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.

QUIZ 2
1.

What is the only function all C programs must contain? A. start() B. system() C. main() D. program() What punctuation is used to signal the beginning and end of code blocks? A. { } B. -> and <C. BEGIN and END D. ( and )

2.

3.

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

QUIZ 2 ANSWER
1.

What is the only function all C programs must contain? A. start() B. system() C. main() D. program() What punctuation is used to signal the beginning and end of code blocks? A. { } B. -> and <C. BEGIN and END D. ( and )

2.

3.

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

5. Flowchart

Answer
Problem analysis

Input Process

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

Output

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

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:

Flowchart

Start

Input m1, m2, m3, m4, m5

Total = m1 + m2 + m3 + m4 + m5

Ave = Total / 5

Print Total, Ave

End

Code Statement
#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; }