[go: up one dir, main page]

0% found this document useful (0 votes)
16 views49 pages

L02 C++

Uploaded by

kavindupunsara02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views49 pages

L02 C++

Uploaded by

kavindupunsara02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Computer Programming

FC12323
Lecture 02

P.B.Shehani Ariyathilake|RSGIS|Faculty of Geomatics|shehani@geo.sab.ac.lk


Algorithms
• De inition: A step by step process to solve a particular problem

• we can represent algorithms by two ways,


1. Flow chart : The diagram representation

2. Pseudo codes : Representation or algorithm


2
f
Symbols of low chart
Start or stop

Input or output

Process

Decision or condition

Flow

3
f
Problem solving
• In a programming environment the problem solving process requires the
following steps.
1. Analyse the problem, outline the problem and its solution
requirement and design algorithm to solve the problem.
2. Implement the algorithm in a programming language such as C++
and verify that the algorithm works.
3. Maintain the program by using and modifying it if the problem
domain changes.

4
Exercise
• Find the sum of two numbers by using a low chart and pseudo code.

5
f
Exercise Start

Pseudo code
Read 2 num x, y
Input x, y
Find sum S as x+y
Print S
S = x+ y

Print S

End

6
Exercise
• Find the average of three numbers by using a low chart and pseudo
code.

7
f
Exercise Start

Pseudo code
Input x, y, z
Read 3 num x, y, z
Find Average as (x+y+z)/3
Print Average
Average = (x+ y + z)/3

Print
Average

End

8
Exercise
• Find the given number is either odd or even by using a low chart and
pseudo code
• Hint : The modulo or mod is the remainder after dividing one number by
another.
Ex: 11 mod 4 = 3
2 mod 2 = 0

f
Exercise Start

Pseudo code
Read num x
Find Result as x mod 2 Input x
If result = 0
Print Even
No
Else Result = x mod 2 Result = 0 Print Odd
Print Odd
Yes

End
Print Even

10
Exercise
• Find the given character is either vowel or not by using a low chart and
pseudo code
• Hint : OR represent as || in low charts

11
f
f
Exercise Start

Pseudo code
Read character b
Input b
If b = “A” || “E” || “I” || “O” || “U” || “a” || “e” ||
“I” || “o” || “u”
Print b is a Vowel
Else b = “A” || “E” || “I” No
Print b is
Print b is not a vowel || “O” || “U” || “a” || “e” || “I” ||
not a vowel
“o” || “u”

Yes

Print b is a
End
vowel

12
Exercise
1. Develop a program to input three marks of a student and calculate the
average mark of the student.

2. Those who have achieved the average mark greater than 60 are selected
to special degree program.Modify the earlier problem to check whether
the student is selected to Special degree program.

3. Modify the earlier problem to input three marks of three students.

13
Exercise

14
Exercise

15
Exercise

16
First C++ Program
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. cout << "Welcome to C++!\n";
7. return 0;
8.
9. }

17
#include preprocessing directive
#include <iostream>
• Line 1, #include <iostream>

• Preprocessing directive, which is a message to the C++ preprocessor

• Lines that begin with # are processed by the preprocessor before the
program is compiled.

18
#include preprocessing directive
#include <iostream>
• Line 1, #include <iostream>

• This line noti ies the preprocessor to include in the program the contents
of the input/output stream header <iostream>

• This header is a ile containing information used by the compiler when


compiling any program that outputs data to the screen or inputs data
from the keyboard using C++’s stream input/output.
Forgetting to include the <iostream> header in a program that inputs data from the key- board or outputs
data to the screen causes the compiler to issue an error message.

19
f
f
using namespace std
using namespace std;
• Line 2, using namespace std;

• Means that we can use names for objects and variables from the standard
library.

20
Blank Lines and White Space
• Line 3 : Simply a blank line.

• Use blank lines, space characters and tab characters (i.e., “tabs”) to make
programs easier to read.

• Together, these characters are known as white space.

• White-space characters are normally ignored by the compiler.


21
The main function
int main()
• Line 4 : int main() is a part of every C++ program.

• C++ programs begin executing at function main.

• The parentheses after main indicate that main is a program building block
called a function.

• C++ programs typically consist of one or more functions and classes.


Exactly one function in every program must be named main.

22
int main()
The main function {

• Line 4 : int main() }

• The keyword int to the left of main indicates that main “returns” an integer
(whole number) value.

• The left brace, {, (line 5) must begin the body of every function.

• A corresponding right brace, }, (line 9) must end each function’s body.

23
The output statement cout<< “Welcome to C++!\n“ ;

cout : to indicate the computer to output something on screen

<< : is the stream insertion operator used to send information to cout

“Welcome to C++!” :String / String Literal. What you need to display on


screen

; : statement terminator

24
Escape sequence cout<< “Welcome to C++! “\n ;

• The backslash (\) is called an escape character.

• Indicates that a “special” character is to be output.

• When a backslash is encountered in a string of characters, the next


character is combined with the backslash to form an escape sequence.

• The escape sequence \n means newline. It causes the cursor (i.e., the
current screen-position indicator) to move to the beginning of the next line
on the screen.

25
The return statement {

return 0;
• Line 7 : return 0 }

• One of several means use to exit a function.

• When the return statement is used at the end of main, as shown here, the
value 0 indicates that the program has terminated successfully.

• If program execution reaches the end of main without encountering a


return statement, it’s assumed that the program terminated successfully

26
Omitting Namespace
• The using namespace std line can be omitted and replaced with
the std keyword, followed by the :: operator for some objects

#include <iostream>
using namespace std; #include <iostream>

int main() {
int main() {
cout << “HelloWorld!”; std::cout << “HelloWorld!”;
return 0;
return 0;
}
}
27
Common errors
Omitting the semicolon at the end of a C++ statement is a syntax error. The syntax of a
programming language speci ies the rules for creating proper programs in that language.
A syntax error occurs when the compiler encounters code that violates C++’s language
rules (i.e., its syntax). The compiler normally issues an error message to help you locate
and ix the incorrect code. Syntax errors are also called compiler errors, compile-time
errors or compilation errors, because the compiler detects them during the compilation
phase. You cannot execute your program until you correct all the syntax errors in it.

28
f
f
C++ Case Sensitivity

C++ is a case sensitive language.

Hense uppercase and lowercase letters are considered to be di erent.

Example:
Variable declaration - age vs AGE

29

ff
Errors in C++
• Types of Errors
1. Compilation errors
2. Link time errors
3. Runtime errors
4. Logical errors

30
Errors in C++
1. Compilation errors

Compilation errors are problems raised by the compiler, generally resulting


from violations of the syntax rules or misuse of types.
Missing Parenthesis (})
Printing the value of variable without declaring it
Missing semicolon etc

31
Errors in C++
2. Link time errors

These error occurs when after compilation we link the di erent object iles
with main’s object using RUN command. These are errors generated when
the executable of the program cannot be generated.
incorrect header iles
Writing Main() instead of main()

32
f
ff
f
Errors in C++
3. Run time errors
Errors which occur during program execution(run-time) after successful
compilation are called run-time errors. These types of error are hard to
ind as the compiler doesn’t point to the line at which the error occurs.
division by zero also known as Division error

33
f
Errors in C++
4. Logical errors
On compilation and execution of a program, desired output is not
obtained when certain input values are given. These types of errors which
provide incorrect output but appears to be error free are called logical
errors.
Wrong logic de ining

34
f
Debugging
• Process of detecting and removing of existing and potential errors (also
called as ‘bugs’) in a software code that can cause it to behave
unexpectedly or crash.

• To prevent incorrect operation of a software or system, debugging is used


to ind and resolve bugs or defects.

35
f
Example (Find Errors)
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout < "C++ Programming";
6. cout << "C++! First lesson;
7. cout << "Welcome to Class"
8. return 0
9.
10. }

36
Example
1. #include <iostream>
2. using namespace std;

4. int main()
5. {
6. cout << "C++ Programming";
7. cout << "C++! First lesson\n";
8. cout << "Welcome to Class\n";
9. return 0;
10.
11. }

37
Comments //
//Text printing

• You insert comments to document your programs and to help other


people read and understand them.

• Comments do not cause the computer to perform any action when


the program is run—they’re ignored by the C++ compiler and do not
cause any machine language object code to be generated.

38
Comments //
//Text printing

• The comment describes the purpose of the program.

• A comment beginning with // is called a single-line comment


because it terminates at the end of the current line.

• You also may use comments containing one or more lines enclosed
in /* and */ . It is called Multiline comment.

39
Exercise
1. #include <iostream>
2. using namespace std;

3. int main()
4. {
5. cout << "Hello" << "World" << "\n";
6. return 0;
7.
8. }

40
Exercise
1. #include <iostream>
2. using namespace std;

3. int main()
4. {
5. cout << "Hello" <<"\n"<< "World" << "\n";
6. return 0;
7.
8. }

41
Exercise
1. #include <iostream>
2. using namespace std;

3. int Main()
4. {
5. cout << "Hello All, Welcome to our Module" << "\n";
6.
7. return 0;
8.
9. }

42
Tokens
• Tokens are the minimal chunk of program that have meaning to the
compiler – the smallest meaningful symbols in the language.

43
Basic language features
• Statement : A statement is a unit of code that does something –a basic
building block of a program.

• Expression : An expression is a statement that has a value


• for instance, a number, a string, the sum of two numbers, etc.
• 4+2, x-1, and "Hello, world!\n" are all expressions.

• Not every statement is an expression.

44
C++ Data Types and Variables
Variables in C++
• Variables are containers for storing data values.

• We might want to give a value a name so we can refer to it later.

• A variable is a named location in memory.

46
Identi iers (Variable names)
• A variable name (such as number1) is any valid identi ier that is not a
keyword.

• An identi ier is a series of characters consisting of letters, digits and


underscores ( _ ) that does not begin with a digit.

• C++ is case sensitive—uppercase and lowercase letters are di erent, so a1


and A1 are di erent identi iers.

47
f
f
ff
f
f
ff
Identi iers (Variable names)
• Spaces, punctuation marks, and symbols cannot be part of an identi ier.

• In addition, identi iers shall always begin with a letter.

• They can also begin with an underline character (_).

• C++ uses a number of keywords to identify operations and data descriptions;


therefore, identi iers created by a programmer cannot match these keywords.
ex: bool, break, case, catch, char, char16_t, char32_t, class, compl, const,
constexpr, const_cast, continue

48
f
f
f
f
Variable name or not

49

You might also like