0% found this document useful (0 votes)
21 views57 pages

Lecture 1

The document provides information about an introductory algorithms and computing course, including the instructor's contact details, course objectives which are to introduce programming languages and formulate algorithms, prerequisites which are none, and the course outline covering various programming topics to be taught through lectures and labs.

Uploaded by

Abdullah Tanweer
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)
21 views57 pages

Lecture 1

The document provides information about an introductory algorithms and computing course, including the instructor's contact details, course objectives which are to introduce programming languages and formulate algorithms, prerequisites which are none, and the course outline covering various programming topics to be taught through lectures and labs.

Uploaded by

Abdullah Tanweer
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/ 57

EC 100 - ALGORITHM AND

COMPUTING
Personal Information
• Nida Aziz
• BE Mechatronics (DE 25)
• MS Intelligent Systems and Robotics, University
of Essex, UK

• Email: [email protected]
[email protected]
Course Objectives
• This is a basic course in Mechatronics which is offered to all
students in the college.
• The course aims to
– introduce the students to programing languages,
– To formulate algorithms and
– proficiently program in C++.
• All basic topics including but not limited to input/output
statements, selection statements, repetition structures,
indirect addressing methods, arrays, hardware, etc are
covered in this course and are practiced in the labs.
• It will give you enough knowledge to proficiently develop
software in C++.
Prerequisites
• None
Course Outline
• Language syntax • Arrays & Strings
• Algorithm formulation • Pointers
• Operators • Structures
• Selection • File handling
• Repetition • Classes & Objects
• Functions
Resources
• C++ How to program (5th Ed): Deitel and Deitel
• Object oriented programming in C++ (4th Ed):
Robert LaFore
Course Details
• Credit Hours: 3-1
• Modes of Teaching: Lectures, Labs and Demos
• Grading (100% of 75%)
– Finals 40%
– Mid-Terms 25%
– Projects 2 x 10%
– Quizzes 15%
Please Note:
• All correspondence will be done via your
official yahoo group DE 34 MTS. Please join it
and check it regularly!
• Plagiarism is strictly not allowed and will be
dealt with seriously!
What constitutes PLAGIARISM??
• Plagiarism is when you
– Copy code (from a fellow or the internet)
– Share code (working together)
– Borrow code (even just for an idea)
– Take or give help in coding (from your fellows, the
internet or anyone else)
• Consequences:
– Your assignment will be cancelled. Both parties will
get ZERO marks.
– Further disciplinary action might be taken if need be.
What does not constitute
PLAGIARISM??
• Discussions
– You may discuss the strategies of solving a
problem without sharing any piece of code.
EC-100 - Algorithm and
Computing
Lecture 1
Introduction to Computers
• A computer is a device capable of
– performing computations and
– making logical decisions
– at speeds millions (even billions) of times faster than
human beings can (Deitel and Deitel).
• Many of today's personal computers have
frequencies in GHz range.
• What is the speed of the fastest computer in the
world??
• What is the speed of your computer??
Introduction to Computers
• A computer is a device capable of
– performing computations and
– making logical decisions
– at speeds millions (even billions) of times faster than
Trivia:
human beingscan
A supercomputer cando[Deitel and
in a day Deitel].
what a dual-core personal
• computer
Many would
of today's personal
take 160 years computers
to complete have
[techradar.com]
frequencies in GHz range.
• What is the speed of the fastest computer in he
world??
• What is the speed of your computer??
Introduction to Computers
• Computers process data under the control of
sets of instructions called computer programs.
• A computer consists of various devices
referred to as hardware (e.g., the keyboard,
screen, mouse, hard disk, memory, DVDs and
processing units).
• The programs that run on a computer are
referred to as software.
Computer Organization
1. Input unit
• It obtains data and computer programs from input devices for
example, keyboards, mouse, camera, etc.
2. Output unit.
• It takes processed information and places it on various output
devices to make the information available for use outside the
computer. For example, monitors, screen, printer or can be used to
control other devices.
3. Memory unit.
• It stores computer programs while they are being executed.
However, the memory unit only stores information temporarily. RAM
is a part of the memory unit.
Computer Organization
4. Arithmetic and logic unit (ALU).
• It is responsible for performing calculations, such as addition, subtraction,
multiplication and division and for checking whether two numbers are
equal.
5. Central processing unit (CPU).
• It coordinates and supervises the operation of the other sections. The CPU
tells the input unit when information should be read into the memory unit,
tells the ALU when information from the memory unit should be used in
calculations and tells the output unit when to send information from the
memory unit to certain output devices.
6. Secondary storage unit.
• Programs or data not actively being used by the other units normally are
placed on secondary storage devices, such as CDs or DVDs until they are
again needed.
History of C/C++
BC Languages such as FORTRAN, COBOL, ALGOL,
Early
PL/I and others.An official C++ standard is
adopted.
1970 Brian Kernigham and Dennis Ritchie invent C.
The language they used for inspiration was
called “ B”
1980 Bjarne Stroustrup created “C with Classes.”

1995 The ANSI Committee releases their draft of the


C++ Standard.

1998 An official C++ standard is adopted.


Applications
• C++ is a very widely used programming language.
A few applications of c++ are
– Google search engine
– Microsoft Windows
– Adobe Systems (including Acrobat, PhotoShop and
ImageReady)
– Amazon.com
– Ericsson
– Nokia
– Seimens, etc
Other Programming Languages
• Pascal
• Fortran
• Cobol
• Ada
• Perl
• Basic
• Java
• Prolog, etc
Programming language rules
• Rules of Syntax which specify how valid
instructions are written in the language.
– They deal with the structure of an instruction

• Rules of Semantics which determine the


meaning of the instructions (what the
computer will do in response to the given
instructions).
– They deal with the content of an instruction
Bugs and Debugging
• Programming errors are called bugs
• The process of tracking bugs and correcting them is
called debugging.
• Three kinds of errors can occur in a program:
– syntax errors
– semantic errors
– runtime errors

• It is useful to distinguish between them in order to


track them down more quickly
Syntax errors
• Most languages including c++ can only
execute a program if the program is
syntactically correct; otherwise, the process
fails and returns an error message.
– Syntax refers to the structure of a program and
the rules about that structure.
– For example, in English, a sentence must begin
with a capital letter and end with a period.
this sentence contains a syntax error.
So does this one
Semantic Errors
• Semantic error is an error in the content of a
code.
• If there is a semantic error in your program, it
will run successfully, i.e. the computer will not
generate any error messages, but it will not do
the right thing.
Semantic Errors
• The problem is that the program you wrote is
not the program you wanted to write. The
meaning of the program (its semantics) is
wrong.
• Identifying semantic errors can be tricky
because it requires you to work backward by
looking at the output of the program and
trying to figure out what it is doing.
Runtime Errors
• The third type of error is a runtime error, so
called because the error does not appear until
you run the program.

• These errors are also called exceptions because


they usually indicate that something exceptional
(and bad) has happened.

• Runtime errors are rare in the simple programs so


it might be a while before you encounter one.
Types of Program
• Source Program
– A program written in a human readable version,
which you will write.
• Object Program
– The machine language version of a source
program in 0s and 1s.
• EXE Program
– It is an executable program
Programming Languages
• Programming Language
– A set of rules, symbols, and special words used to
construct a computer program.
• Machine Language
– The binary representation of the instructions that a
computer's hardware can perform.
• Assembly Language
– A low-level programming language in which a mnemonic is
used to represent each of the machine language
instructions for a specific computer.
• High-Level Language
– A computer language that is more understandable and
closer to standard notations than assembly language. It is
more close to plain English. C/C++ are high-level
languages.
Compiler
• A program written in a high
level language (such as C++)
must be translated into
machine code before the
computer can run it (i.e.
execute the instructions),
because a computer can
only understand machine
code.
• C++ programs are written
by humans and translated
into machine language by
the C++ compiler.
EC-100 - Algorithm and Computing

Lecture 2
A basic program
// ------------------------------------------------------------
// hello.cpp is a demonstration program

#include <iostream>

int main ( )
{
std::cout << "Hello C++ World"<<std::endl;
return 0;
}
A basic program
// ------------------------------------------------------------
// hello.cpp is a demonstration program

#include <iostream>
using namespace std;

int main ( )
{
cout << "Hello C++ World" << endl;
return 0;
}
A basic program
• Programs typically contain the following
elements:
– Descriptive comments (double forward slash // or /* … */)
– Include files
– Declarations: function prototypes and global variables
– Functions including exactly one main function

• The main() function


– Should have it’s type and arguments (if any) declared
– Should return a value of the correct type
– Controls the execution of the program, even if it is not first in the
file.
Input / Output Statements
cout << “Hello C++ World”;
• cout is an object in c++, predefined to display the
standard output stream
• << insertion operator
• The output of this program
– Hello C++ World

cin >> var;


• cin is an object in C++, predefined to wait for user
input. This input is stored in the variable ‘var’
• >> extraction operator
A basic program
// ---------------------------------------------------- Descriptive
comments
// hello.cpp is a demonstration program

#include <iostream> Include files

int main ( ) main() function


insertion
End line

{ operator

std::cout << “Hello C++ World” <<std::endl;


return 0; Standard
} output
stream
A basic program
// ---------------------------------------------------- Descriptive
comments
// hello.cpp is a demonstration program

#include <iostream> Include files

int main ( ) main() function


insertion
End line

{ operator

std::cout << “Hello C++ World” <<std::endl;


return 0; Standard
} output
stream
Variable Declaration
• A variable declaration
– Defines the name of the variable
– Defines the type of the variable (int, float, etc.)
– Gives the programmer a description of the variable

• Declaration format:
– type name; //comment

– Examples:
– int sum; //stores ‘sum’ in int type variable
– float answer; //stores ‘answer’ in float type variable
Variable Declaration
• Once the variable has been declared, it can be
used in assignment statements
• Sum = 2457 + 5363;
• Answer = 2.456+3.2;

– Note: Variables are case sensitive


Variable Data Types
• Variable types: (32-bit)
char 1 byte -128 to 127
short 2 bytes -32768 to 32767
int 4 bytes -2147483648 to
2147483647
long int 4 bytes -2147483648 to
2147483647
float 4 bytes -1.0x1038 to 1.0x1038
double 8 bytes -1.0x10308 to
1.0x10308
Variable Data Types
– signed (default)
– unsigned
• A variable declared as signed will have its sign
(positive or negative) for its MSB (most
significant bit)
– Will this effect its range?
Variable Data Types
– signed (default)
– unsigned
• A variable declared as signed will have its sign
(positive or negative) for its MSB (most
significant bit)
– Its range will be reduced by half!
Variable Data Types
– signed (default)
– unsigned
• A variable declared as signed will have its sign
(positive or negative) for its MSB (most
significant bit)
– Its range will be reduced by half!
• Similarly a variable declared as unsigned will
not have a sign (it will always be positive)
– Its range will be twice as much of signed!
// -------------------------------------------------
-----------
// variables.cpp is a program to demonstrate
declaraion and usage of basic datatypes

#include <iostream> Descriptive


Include files
using namespace std; comments

int main ( ) main() function


{
int num1 = 1;
int num2 = 2; Variable
int sum; declaration
sum = num1 + num2;
cout << "Sum of " << num1 << " and " << num2
<< " is " << sum << endl;
return 0;
}
// getvalue.cpp is a program to demonstrate the usage of cin object

#include <iostream>

int main ( )
{
int num1 ;
int num2 ;
int sum; Standard input stream

cout << "Enter value of num1 ";


cin >> num1;
Extraction operator
cout << "Enter value of b ";
cin >> num2;

sum = num1 + num2;


cout << "Sum of " << num1 << " and " << num2 << " is " <<
sum << endl;
return 0;
}
Number Systems
• Decimal (Base 10)
• Binary (Base 2)
• Octal (Base 8)
• Hexadecimal (Base 16)
Memory
• Variable names such as a and sum actually correspond
to locations in the computer's memory.
• When a variable is declared, the compiler allocates a
memory location to that variable. Any further
operation on that variable takes place in that memory
location.
• Every variable has a name, a type, a size and a value.
For example
int num1;
cin >> num1; // read first integer from user into num1
the characters typed by the user are converted to an integer that will be
placed into a memory location to which the variable number1 has been
assigned by the C++ compiler.
Memory
• Destructive Process
– Whenever a value is placed in a memory location
it overwrites the previous value in that location
placing a new value into that memory location.

• Nondestructive Process
– Whenever a value is read from a memory location,
the value of that variable in that memory location
remains unchanged.
Memory
• For example
sum = num1 + num2;
// add the nums and store result in sum
– The values of ‘num1’ and ‘num2’ will be read out
and used in the calculation of sum. So it will be a
nondestructive process.
– However, the result of the addition process will be
stored in the variable ‘sum’. So it will be a
destructive process.
• Thus, it is always best to declare a variable as null or
zero.
Operators
• “Operators are words or symbols that cause a program to do something to variables.”

OPERATOR TYPES:
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b
Arithmetic ‘+=’ ‘-=’ ‘*=’ ‘/=’ ‘%=’ a+=b is the same as a=a+b
assignment a-=b a*=b a/=b a%=b
Increment and ‘++’ ‘- -’ a++ is the same as a=a+1
decrement a-- is the same as a=a-1
Relational ‘<’ ‘>’ ‘<=’ ‘>=’ ‘==’ ‘!=’
Logical ‘&&’ ‘||’ ‘!’
Bitwise ‘&’ ‘|’ ‘^’ ‘~’ ‘<<’ ‘>>’
Operator Precedence
Polynomials in C++
• In algebra
𝑎+𝑏+𝑐+𝑑+𝑒
𝑚=
5
• In c++
𝑚 = (𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒)/5

• What happens if I don’t put the parenthesis ()?


The Modulus Operator
• % is known as the Modulus Operator or the
Remainder Operator.
• It calculates the remainder of two variables
• It can only be used with two ints.. WHY?
Evaluate
In what order will the expression be evaluated?
Evaluate
In what order will the expression be evaluated?
2nd Degree Polynomial

𝑦= 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
2nd Degree Polynomial

𝑦= 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
Summary
• By the end of this lecture, you should know
– how to write a basic program, declare variable and
input/output statements.
– the different number systems and their inter-
conversions.
– The basic concept of what memory is and how it is
used.
– Arithmetic Operators and their precedence

You might also like