0% found this document useful (0 votes)
33 views

C++ Material Lecture Note

Uploaded by

progiestudio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

C++ Material Lecture Note

Uploaded by

progiestudio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Algorithm

From programming point of view, an algorithm is a step-by-step


procedure to resolve any problem. An algorithm is an effective
method expressed as a finite set of well-defined instructions.

Thus, a computer programmer lists down all the steps required


to resolve a problem before writing the actual code.
Method of Specifying an Algorithm
Once you have designed an algorithm, you need to specify it in some
fashion. There are basically three method of specifying algorithm.
1. Natural language
2. Pseudo code
3. flowchart
4. Data flow diagram(DFD)
5. Decision table

Following is a simple example of an algorithm to add two numbers .


Pseudocode/Flowchart

Pseudocode: - is a mixture of a natural language and


programming language techniques .

Flowchart: - this is a representation technique use by collecting


and connecting geometric shapes containing descriptions of the
algorithm.
Algorithm cont’d
Example 1.
Natural Language Pseudocode

1. Start Begin process


2. Read num1 --------
3. Read num2 --------
4. Add both the numbers Sum Num1 + Num2
5. Print the result --------
6. Stop. End process
Algorithm cont’d
Algorithm to find the largest number

1.Get a list of numbers L1, L2, L3....LN


2. Assume L1 is the largest, Largest = L1
3. Take next number Li from the list and do the following
4. If Largest is less than Li
5. Largest Li
6. If Li is last number from the list then
7. Print value stored in Largest and come out
8. Else repeat same process starting from step 3
Basic Geometric Shapes for Algorithm
Symbol Name Meaning
1. Oval Start/Stop

2. Arrow Flow Line

3. parallelogram Input / Output

4. Process
Rectangle

5. Diamond Decision
Flowchart to Add two Numbers
Start

Num1=23
Num2=12

Sum =Num1+Num2

Print Sum

Start
Fig 1.2
Flowchart to Add twenty Numbers
Brief history of C++
C++ is a middle-level programming language developed by
Bjarne in 1979 at Bell Labs. C++ runs on a variety of platforms,
such as Windows, Mac OS, and the various versions of UNIX.
C++ is regarded as a middle-level language, as it comprises a
combination of both high-level and low-level language features.

As an enhancement to the C language and originally named C


with Classes but later it was renamed C++ in 1983.
Uses of C++

1. C++ is used by hundreds of thousands of programmers in


essentially every application domain.
2. C++ is being highly used to write device drivers and other
software that rely on direct manipulation of hardware.
3. C++ is widely used for teaching and research because it is
clean enough for successful teaching of basic concepts.
4. Anyone who has used either an Apple Macintosh or a PC
running Windows has indirectly used C++ because the
primary user interfaces of these systems are written in C++.
C++ Compiler

This is an actual C++ compiler, which will be used to compile your


source code into final executable program.
Most C++ compilers don't care what extension you give to your
source code, but if you don't specify otherwise, many will use
.cpp by default.
Most frequently used and free available compiler is GNU C/C++
compiler, otherwise you can have compilers either from HP or
Solaris if you have the respective Operating Systems.
Installing GNU C/C++ Compiler:
Compiler
The conversion from text program to binary file is done by
another software called Compiler and this process of conversion
from text program to binary format file is called program
compilation.
Finally, you can execute binary file to perform the programmed
task.
We are not going into the details of a compiler and the different
phases of compilation.
The following flow diagram gives an illustration of the process:
Compiler
Compiler
So, if you are going to write your program in any such language,
which needs compilation like C, C++, Java and Pascal, etc., then
you will need to install their compilers before you start
programming.
Interpreter
We just discussed about compilers and the compilation process.
Compilers are required in case you are going to write your
program in a programming language that needs to be compiled
into binary format before its execution.

There are other programming languages such as Python, PHP,


and Perl, which do not need any compilation into binary format,
rather an interpreter can be used to read such programs line by
line and execute them directly without any further conversion.
Interpreter
So, if you are going to write your programs in PHP, Python, Perl,
Ruby, etc., then you will need to install their interpreters before
you start programming.
C++ Program Structure
Let us look at a simple code that would print the words Hello
World.
1. #include <iostream>

1. using namespace std;

2. int main()
3. {
4. cout << "Hello World"; // prints Hello World
5. return 0;
6. }
C++ Program Structure
Let us look at the various parts of the above program:

1. The C++ language defines several headers, which contain


information that is either necessary or useful to your
program. For this program, the header <iostream> is needed.
2. The line using namespace std; tells the compiler to use the
std namespace. Namespaces are a relatively recent addition
to C++.
3. The next line ‘main() is where program execution begins.’
4. Every c++ program must have a main function.
Functions
Functions are small units of programs and they are used
to carry out a specific task. For example, the above
program makes use of two functions: (sub-routine
instead of function “in some prog”)
<iostream>
main().
using namespace std
Cout<<.
Return

C++ programming itself provides various built-in functions


Comments
A C program can have statements enclosed inside /*.....*/ for
multi line comment and // ………..for single line comment.. Such
statements are called comments and these comments are used
to make the programs user friendly and easy to understand. The
good thing about comments is that they are completely ignored
by compilers and interpreters. So you can use whatever language
you want to write your comments, reasons for comment are:
1. Documentation
2. Readability/Understandability
Types of Comment in Programming
1. Single line comment
2. Multiple line comment

Example: single line comment


// Program writing by Ayuba
// Data 25_01_2022

Example: Multiple line comment


/* Program writing by Ayuba,
Data 25_01_2022*/
Comments
1. #include <iostream>
2. using namespace std;
3. // main() is where program execution begins.
4. int main()
5. {
6. cout << "Hello World"; // prints Hello World
7. return 0;
8. }
Whitespaces
When we write a program using any programming language,
we use various printable characters to prepare programming
statements. These printable characters are a, b, c,......z, A, B,
C,.....Z, 1, 2, 3,...... 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |, {,
}, [, ], :, ;, <, >, ?, /, \, ~. `. ", '. Hope I'm not missing any
printable characters from your keyboard.
Apart from these characters, there are some characters which
we use very frequently but they are invisible in your program
and these characters are spaces, tabs (\t), new lines(\n).
These characters are called whitespaces.
These important whitespace characters are common in all the
programming languages and they remain invisible in your text
document:
Whitespace cont’d
Whitespace Explanation Representation

• New Line To create a new line \n


• Tab To create a tab \t
• Space To create a space. empty space
Example
1. #include <iostream>
2. using namespace std;
3. // main() is where program execution begins.
4. int main()
5. {
6. cout << "Hello World"; // prints Hello World
7. return 0;
8. }
Semicolons
Every individual statement in a C Program must be ended with a
semicolon (;), for example, if you want to write "Hello, World!"
twice, then it will be written as follows:
1. #include <iostream>
2. using namespace std;
3. // main() is where program execution begins.
4. int main() {
5. cout << "Hello World"; // prints Hello World
6. return 0;
7. }
Syntax Error
If you do not follow the rules defined by the programing
language, then at the time of compilation, you will get syntax
errors and the program will not be compiled.
IDE
An integrated development environment (IDE) is
software for building applications that combines
common developer tools into a single graphical
user interface (GUI).
IDE FOR PC
C++ IDE FOR ANDROID
THANK YOU

You might also like