INDIAN INSTITUTE OF TECHNOLOGY ROORKEE
CEC-101: Module 1
Computer Programming
Lecture Outline
• A brief introduction
• Course overview
• Getting to know each other
• Set the tone for the coming 4 months
2
About you home for the next 4 years
•Converted to an Engineering University
•First of its kind in Independent India
1837-47 1949 2001
•Declared as an IIT
•Major famine in the Gangetic plain
•Need for a massive canal system
•Roorkee College (Thomason College of
Civil Engineering) is established in 1847
3
About you home for the next 4 years
4
About you home for the next 4 years
5
Syllabus
1. Introduction, installation of compiler
2. Basic C++ program
3. Variables and Comments (Keywords)
4. Variable scope (Local and global) and data types (size, range)
5. I/O in C++ (Insertion and extraction operators)
6. Header files (System and User-defined) and operators (arithmetic, comparison,
logical)
7. Referencing (scope resolution operator ::) and typecasting
8. Manipulators (endl, setw() from <iomanip>), operation precedence, constants
9. Control statements (Sequence, selection, loops) (if-else, if-else ladder, switch
case, while, do-while)
6
Course information
Course Objective:
• To introduce fundamentals of a higher-level programming language, and their
application to engineering problems.
CEC-101 Computer Programming
Credits 4
Lectures Mon 5-6 PM
Tues 5-6 PM
Contact hours Thurs 5-6 PM
Practical 2 hours per week
Description Weightage (%)
Classwork sessional (CWS) ??
Practical sessional (PRS) ??
Mid-Term Exams 25 (1.5 hrs)
End-Term Exams 40 (3 hrs)
7
Academic Calendar
8
Academic Calendar
9
Course content
• Object-Oriented Programming concepts
• Keywords, identifiers, variables,
• Control Statements
• Loops (if/else, do/while, for etc.)
• Classes and Objects
• Member Functions and Strings
• Operator Overloading
• Inheritance
• Polymorphism
10
Some ground rules
• Interactive sessions and not sermons !
• No beg-borrow-steal approach for marks/attendance
• Slides will be shared…doesn’t mean no need for notes
• Phones in SILENT mode
11
What do you think of
programming?
Introduction
Computer
An electronic device for storing and processing data,
according to instructions given to it in a program
Programming
The process of writing instructions (a set of commands) to
solve a specific problem
Algorithm
Step-by-step instructions for a problem-solving process
13
Programs
Algorithm Program (in C++)
Input A, B, C #include <iostream>
using namespace std;
Sum = A + B + C
Print Sum int main() {
int A, B, C, Sum;
cin>>A>>B>>C;
Sum = A + B + C;
cout<<Sum;
return 0;
}
14
Programs
Algorithm
• A set of explicit and unambiguous finite steps, which, when carried out for
a given set of initial conditions to produce the desired 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.
Set of instructions Input to the program
15
Basics components
Main memory consists of a long
list of numbered locations called
memory locations (a few
thousands to millions);
Secondary memory (or storage)
The contents of these locations
is the memory that is used for
can change and contain the same
keeping a permanent record of
number of 0s and 1s binary
information after (and before) the
digit or a bit most computers
computer is used. Also referred as
contains 8 bits (i.e., byte) The
auxiliary memory, auxiliary
number that identifies a byte is
storage, external memory, and
called its address.
external storage
16
High and low-level languages
17
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
• Use more complicated instructions • Assembly
than the CPU can follow ADD X Y Z
• Must be translated to zeros and • Machine language
ones for the CPU to execute a – CPU can follow machine language
program – Assembly must be converted to
• C, C++, Java, Visual Basic, etc. machine language
• 0110 1001 1010 1011
18
In this course
• How to write programs?
• Program a set of instructions special
notation or syntax (i.e., programming language)
• C++
• C++ designer: Bjarne Stroustrup (1979)
• Powerful and complex
• We will cover only a part of C++….
19
C++ basics
• Syntax: language specific statements (instructions/
notations/ rules) which are legal
• Comments:
– used to explain the code or increase the readability
– Comments are ignored by compiler
– Single/ multi lined
// this is a comment
/* this is also
a comment */
• Every instruction/ command is terminated by semi-colon
“;”
20
Processing a C++ program
• Editor simply writing the set of instructions as per
the syntax (many text editors support syntax/ color
highlighting)
• Preprocessor these are directives which give
instructions to the compiler to pre-process the
information
– Starts with # (only whitespace is allowed before this)
– these are filenames in the Standard C++ Library.
#include <iostream> this is a C++ standard library
header for input-output stream
21
Processing a C++ program
• Compiler
– checks the program for syntax errors (set of rules which are language
specific)
– Translate into machine language (low-level language); e.g., binary (“Hello
World.”)
01001000 01100101 01101100 01101100 01101111
00100000 01010111 01101111 01110010 01101100
01100100
• Execution
– from top to bottom
– from left to right
• Output
22
Hello World!
#include <iostream>
int main( ) {
// hello world program
std::cout << “Hello world”;
return 0;
23
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.
24
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…
25
Reserved keywords
• int • catch 32 such
keywords
• float • enum
• double • public
All small-case
• char • false
• const • true
• void • break
• return • continue
• switch • namespace
• while • throw
• do • static
• for • private
• this • …
26
Special symbols
• + • ?
• - • ,
• * • <=
• / • !=
• . • ==
• ; • >=
27
C++ basic input, output
• cout sends the output from main memory to standard
devices (e.g., console screen)
• cin takes the input from standard devices (e.g.,
keyboard, mouse) to the main memory
• header (required): #include <iostream>
• For cout, insertion (<<) operators are used, whereas for cin
extraction (>>) operators are used
• Let’s look on the Hello World example
28
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?
Return statement; optional
but some compilers expect it
to be included as the last line
of the main() function
29
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( ) {
// write the comment here…
cout << “Hello world”;
return 0;
Let’s look on another example
30
cout
#include <iostream>
using namespace std;
What will be the output of this?
int main( ) {
cout << “Hel” << “lo” <<“Wo” << “rld”;
return 0;
Let’s look on another example
31
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;
32