01 Cec101
01 Cec101
2
Introduction
• Algorithm
• A set of explicit and unambiguous finite steps, which, when carried out for
a given set of initial conditions to produce the corresponding output and
terminate in finite time.
• Basically, they are a set of steps to be performed, which can be used to
perform a task/solve a problem.
• Program
• An implementation of an algorithm in a programming language that computer
can understand.
5
Programs
#include <iostream>
using namespace std;
6
High and low level languages
High-level Low-level
• Resemble human languages • It is difficult to write and debug
• Are designed to be easy to read and code in Low-Level Languages.
write • Assembly
• Use more complicated instructions ADD X Y Z
than the CPU can follow • Machine language
• Must be translated to zeros and – CPU can follow machine language
ones for the CPU to execute a – Assembly must be converted to
program machine language
• C, C++, Java, Visual Basic, etc.
0110 1001 1010 1011
• Source Code
Object Code
7
High and low-level languages
8
Programmers?
9
In this course
• C++
– Derived from C
– C++ designer: Bjarne Stroustrup (1979)
– Powerful and complex
10
C++ basics
• Comments:
– used to explain the code or increase the readability
– Comments are ignored by compiler
– Single/ multi-lined
// looping over the array
/* looping over
the array*/
11
Processing a C++ program
12
Processing a C++ program
• Compiler
– checks the program for syntax errors (set of rules which are
language specific)
– Compiler accepts almost any pattern of line breaks and indentation
– Translate into machine language (low-level language);
– Binary of “Hello World.”
01001000 01100101 01101100 01101100 01101111
00100000 01010111 01101111 01110010 01101100
01100100
• Execution
– from top to bottom
– from left to right
• Output
13
Program errors
• Syntax errors
– Violation of the grammar rules (syntax) of the language
– Discovered by the compiler
• Error messages may not always show the correct location of
errors
• Run-time errors
– Error conditions detected by the computer at run-time
• Logic errors
– Errors in the program’s algorithm
– Most difficult to diagnose
– Computer does not recognize an error
14
Hello World!
#include <iostream>
int main( ) {
15
Where to run the program?
• Offline:
– Dev-C++
– Visual Studio
– …
• Online:
– https://www.programiz.com/cpp-programming/online-compiler/
– https://www.tutorialspoint.com/compile_cpp_online.php
– https://www.w3schools.com/cpp/cpp_compiler.asp
– …
[[let’s run the first program]]
Try to write a few programs, which prints your name/ enrolment number/ branch/
institute, etc.
16
Where to run the program?
17
Why C++ (or programming) in Civil Engineering?
• AutoCAD C++
• SUMO (open-source): https://sumo.dlr.de/ C++
• 12 D solutions C++
• FreeFEM: https://freefem.org/ C++
• …
• Others (general) C++
– Amazon
– Google web search engine
– Apple OS X (a few important parts)
– Adobe systems
– Bloomberg
– Sun: Open Office
– …
• Many other languages…
18
Special symbols
• + • ?
• - • ,
• * • <=
• / • !=
• . • ==
• ; • >=
19
Reserved keywords
• int • catch
• float • enum
• double • public
All small-case
• char • false
• const • true
• void • break
• return • continue
• switch • namespace
• while • throw
• do • static
• for • private
• this • …
20
C++ basic input, output
• For cout, insertion (<<) operators are used, whereas for cin
extraction (>>) operators are used
• Let’s look on the Hello World example TODO: try clog and cerr
21
cout
header (remember Name of the function;
directives)!! return type;
every program must have
#include <iostream> exactly one main()
function
int main( ) {
Check cout, insertion
// write the comment here… operator, semi colon
std::cout << “Hello world”;
return 0;
What is this? Can we
} get rid of it?
22
cout
This means, all functions under std namespace
#include <iostream> are available in the scope of this program
using namespace std; without explicitly prefixing “std::”
int main( ) {
23
cout
#include <iostream>
using namespace std;
What will be the output of this?
int main( ) {
24
cin
#include <iostream>
using namespace std;
int main( ) {
int num ;
cout << “Enter an Integer”;
cin >> num; // taking input
cout << “Square of the number is “ << num*num;
return 0;
25
C++ Program
26
Data types
27
Data types
28
Primitive Data types: char
29
Primitive Data types
Data type Meaning Size (in bytes) Other details
int Integer 4 Range: -2147483648 to
2147483647; usually of 4
int age = 24; bytes
1 2 4 8
32
Other Data types
33
Variable names in C++
34
Variable names in C++
• Syntax
datatype variable_name;
• Though, it is a matter of personal preference, descriptive
names are recommended for better readability of the code
• Unlike reserved words, predefined identifiers (e.g., cout,
cin) can be redefined, but don’t do that.
• Cases:
– Camel ageOfPerson
– Pascle AgeOfPerson
– Snake age_of_person
Different style for local, instance, static, variables may be adopted for clarity.
35
Variable names in C++
#include <iostream>
using namespace std;
int main( ) {
37
Variable names in C++
#include <iostream>
using namespace std;
int main( ) {
int num1;
int num2;
num1=34;
num2=90;
cout << num1 << endl;
cout << num2;
return 0;
}
38
Variable names in C++
#include <iostream>
using namespace std;
int main( ) {
39
Variable names in C++
#include <iostream>
using namespace std;
int main( ) {
40
Variable names in C++
#include <iostream>
using namespace std;
int main( ) {
Three variables can be entered separated by space or one number in each line
(newline separator).
41
Operators
1. Arithmetic
2. Assignment
3. Relational
4. Logical
5. Other
42
Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
43
Arithmetic Operators
int main( ) {
int a =34, b=90;
cout << "a+b=" << (a+b) << endl;
cout << "a-b=" << (a-b) << endl;
cout << "a*b=" << (a*b) << endl;
cout << "a/b=" << (a/b) << endl;
cout << "a%b=" << (a%b) << endl;
return 0;
}
int main( ) {
int a =3;
Unary operators
int x = a++;
• Pre-increment ++x (also --x)
cout << x << endl;
• Post-increment x++ (also x-- )
cout << a << endl;
x = ++a;
cout << x << endl;
cout << a << endl;
return 0;
}
44
Assignment Operators
45
Assignment Operators
#include <iostream>
using namespace std;
int main( ) {
46
Order of Precedence
• All operations inside of () are evaluated first
• *, /, and % are at the same level of precedence and are evaluated next
• + and – have the same level of precedence and are evaluated last
47
Mixed expression
• Integral expression 2+3*6
48
Relational Operators
49
Logical Operators
Logical AND.
expression1 &&
&& True only if all the
expression2
operands are true.
Logical OR.
expression1 ||
|| True if at least one of
expression2
the operands is true.
Logical NOT.
! !expression True only if the
operand is false.
50
Logical Operators
#include <iostream>
using namespace std;
int main( ) {
51
other Operators
52
Mathematical Functions
#include<math.h>
• Examples:
– sin, cos, tan …
– asin, acos, atan …
– sqrt, abs, pow, floor, ceil …
– log, exp, …
– See https://www.cplusplus.com/reference/cmath/
53
Mathematical Functions
#include <iostream>
#include<cmath>
using namespace std;
int main( ) {
int a = 25;
cout << sqrt(a) << endl;
int b = 5;
int c = 3;
cout << pow(5,3);
return 0;
}
54
Conditional Statements
if (condition) {
// block of code to be executed if the condition is
true
}
if (35 >= 20) {
cout << “35 is greater than or equal to 20";
}
55
Conditional Statements
Truth Tables
56
Conditional Statements
57
Conditional Statements
58
Conditional Statements
59
Conditional Statements
int speed;
cin >> speed;
if (speed < 40) {
cout << “Good going…";
} else if (speed < 50) {
cout << “Just right …";
} else {
cout << “Too fast, slow down…";
}
60
Conditional Statements
61
Conditional Statements
62
Conditional Statements: nested
63
Conditional Statements: Ternary operator
Syntax
Assignment using ?:
64
Conditional Statements: Ternary operator
65
Conditional Statements: switch
66
Conditional Statements: switch
int day = 4; It breaks out of the switch
switch (day) {
case 1: block, i.e., no more case
cout << "Monday"; testing inside the switch
break; block.
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday"; When none of the
break; outlined cases match the
default: input argument
cout << “Enjoying the Weekend";
}
67
Conditional Statements: switch
Leaving an empty case in the switch block
68
Conditional Statements: switch
Pitfall of forgetting a break in the switch block
69
Conditional Statements: switch for menu
70
Loops
• Repetition
• To execute a block of code as long as a specific condition is met
• Loops are handy, saves time, and are more readable
71
Loops: Types
for (int i = 0; i < max; i++)
{
cout << i << "\n";
‘for’ loop
}
int i = 0; max;
cin >> max;
while (i < max)
{ ‘while’ loop
cout << i << "\n";
i++;
}
int i = 0;
int max;
cin >> max;
do
{
‘do-while’ loop
cout << i << "\n";
i++;
} while (i < max);
72
‘for’ LOOP: Syntax
int i = 0; max;
cin >> max;
while (i < max)
{
cout << i << "\n";
i++;
for (int i = 0; i < max; i++) }
{
cout << i << "\n"; int i = 0;
} int max;
cin >> max;
do
{
cout << i << "\n";
i++;
} while (i < max);
73
‘while’ LOOP: Syntax
while (Condition)
Runs every time
{
loop is repeated
Code body
}
Updation: Part of Initialization: Occurs
loop code before start of loop
int i = 0; max;
cin >> max;
while (i < max) for (int i = 0; i < max; i+
+)
{ {
cout << i << "\n"; cout << i << "\n";
}
i++; int i = 0;
} int max;
cin >> max;
do
{
cout << i << "\n";
i++;
} while (i < max);
74
‘do-while’ LOOP: Syntax
int i = 0;
int max;
cin >> max; for (int i = 0; i < max; i+
+)
do {
{ cout << i << "\n";
}
cout << i << "\n";
i++; int i = 0; max;
cin >> max;
} while (i < max); while (i < max)
{
cout << i << "\n";
i++;
}
75
Loops: Pitfalls in for loop
76
Loops: Multiple variables in for
77
Loops
• Infinite loop
78
Loops
79
Loops: nested
80
Jumps statements: break, continue, goto
• break:
• Exits from a switch or loop immediately.
• Jumps to the first statement that follows the switch or loop.
• continue:
• Has the opposite effect to break; the next loop begins immediately.
• In while or do-while loop, the program jumps to the test expression, whereas a for
loop is reinitialized.
• goto:
• C++ also offers a goto statement and labels.
• Allows to jump to any given point marked by a label within a function.
• For example, exiting a deeply embedded loop construction immediately.
• A label is a name followed by a colon.
81
break
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break;
}
}
82
break
83
continue
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
84
goto
85
goto
86
Control structure in C++
87
String in C++
88
String in C++
89
String Comparison in C++
90
String Comparison in C++
91
String Comparison in C++
#include <iostream>
92
String Comparison in C++
93
String Concatenation
94
String Concatenation
int x = 10;
int y = 20;
int z = x + y; 1020
string x = "10" ;
string y = "20" ;
string z = x + y;
95
String inserting and erasing
string s1("Miss Summer");
s1.insert(5, "Ashley "); // Insert at position: 5
96
String Access
97
String Access
• Taking a string as input with whitespace in it.
string name;
cin >> name; // Enter “Anjaneya Dixit”
cout << name;
string name;
cout << "Enter your name" <<endl;
getline (cin, name);
cout << name;
98
Thanks …
[email protected]
99