You are on page 1of 73

CSEE2123: OOP and Data Structures

Fall 2018

Introduction
Lecture 1
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Course Information

Instructors Mr. Muhammad Tahir

Email mtahir@cust.edu.pk

Classes Two Lectures of 1h:20m duration / Week

Text “Data structures using C++”, by D. S. Malik

“Object-Oriented Programming in C++,” by Robert


Lafore, 4th Edition

Lecture Slides

Softwares Microsoft Visual Studio, CodeBlock

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 2


Course Instructor : Contact Info
• Mr. Muhammad Tahir
• Assistant Professor , EE Department
• Office : A-Blocks, First Floor
• E-mail : mtahir@cust.edu.pk,
• Phone : (051) 111-555-666 – Ext : 127

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 3


Text Books

• “Object-Oriented • “Data structures using


Programming in C++,” by C++”, by D. S. Malik
Robert Lafore,
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 4
Reference Books
Reference “C++ How to Program” , 9th Edition by Paul Deitel ,
Harvey Deitel

“Data structures and algorithm analysis in C++” by


Mark Allen Weiss

Readings Lecture Slides, Class Handouts

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 5


Prereqs
 CSEE1122 : Computer Programming

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 6


Course Group & Folder
• Course Yahoo Group
– https://groups.yahoo.com/neo/groups/cust_csee1133/info

• Course Folder
– \\fs\lectures$\M.Tahir\CSEE2123

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 7


Course Yahoo Group : cust_csee1133
• a

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 8


Computer Programming at a Glance
• Computer programming is about
finding solution of real world
problems using computational
techniques like algorithm
development, program creation,
software debugging and testing

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 9


Programming Guru‟s
• “Everybody in this country should learn
how to program a computer. Because it
teaches you how to think.” Steve Jobs,
• Founder & CEO : Apple Computers

• Bill Gates
• Founder: Microsoft Corporation
• Bill Gates started Microsoft while he was
a University student at Harvard

• Founder & CEO : Facebook


• Mark Zuckerberg started Facebook as a
project while he was a University
Student at Harvard

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 10


What this Course is about ?
• This course is about
– Study of C++ Language and Object oriented
Programming (OOP) Paradigm in C++
– Study of simple and advanced data structures
using C++
– Study of searching and sorting algorithms for
data structures
– Design of solutions to simple and advanced
real life Problems using C++
– Testing and Debugging of software using
software tools like Visual Studio

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 11


Course Learning Outcomes (CLO‟s)
• At the end of this course, the student should be
able to
• CLO1: Describe and identify fundamental
concepts of object-oriented programming, and
basic and advanced data structures (Level: C2)
• CLO2: Explain, interpret, compare and apply
algorithms and principles of object-oriented
programming and advanced data structures to a
particular situation (Level: C3)
• CLO3: Design, implement and debug small-to-
moderate programs to manipulate and manage
data elements while exhibiting the object-
oriented programming skills (Level: C5)
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 12
Course Outlines
• Introduction of the course
• Basic data structures
– Multi-dimensional Arrays
– Structures
– Unions
• Pointers & Typecasting
– Typecasting
– Memory Allocation (Static vs. Dynamic)
– Array of Pointers vs. Pointer of Arrays
– User-defined Data Types

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 13


Course Outlines …
• Object-Oriented Programming in C++
– Classes and Objects
– Pointers, Reference Pointers
– Operator and Function Overloading
– Inheritance
– Function and Class templates
– Method Overriding
– Polymorphism and Virtual Functions
• Advanced Data Structures
– Array Lists, Linked Lists
– Vectors
– Stacks
– Queues
– TreesCSEE2123: OOP and DS
9/18/2018 © M. Tahir Awan, CUST 14
Course Outlines …
• Algorithms
• Recursion
• Searching
» Linear & Binary
» Depth-First
» Breadth-First
• Sorting
» Insertion & Selection sort
» Bubble sort
» Merge sort
» Heap sort
» Radix sort

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 15


Course Outlines …
• Hashing
• Big O Notation
• Trees Traversal

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 16


Course Grading

 Quizzes 20%
 Assignments 15%
 Class Participation 05%
 Midterm Exam 20%
 Final Exam 40%

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 17


Assignments
• Written & Programming Assignments
– Assignments will be done individually
– You are allowed to discuss problems with
each other/ instructor
– Late Submission of Assignments is not
allowed
– Copying/Cheating in any form will be
penalized

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 18


Quizzes

 Quizzes
 Quizzes will be announced
 There will be no make-up quizzes

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 19


Class Participation
 Everybody is encouraged to participate
in class discussion
 Regular attendance in class ensures
maximum participation

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 20


Exams

 Mid-Term
–Tentative Date : Nov 10 – 17, 2018
 Final Exam
–Tentative Date : Jan 15 – 26, 2019
 All the Exams will be close-book /
close-notes

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 21


General Guidelines\Recommendations
• Be in class on time , Be Regular in Class
• Switch-off Mobile Phones during Lecture
• Actively Participate in Class discussions
• Don‟t miss Quizzes / Assignments
• If something doesn't make sense, ask
questions, don‟t wait for others to ask
• For issues related to course, you can
– Discuss with me during/after class
– Meet with me in my Office
– Contact me through E-mail
• Avoid making disturbance during class
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 22
Engineering Courses that use
Computer Programming
• Computer Programming & Data Structures
–C/C++
• ASIC Design & FPGA
–Verilog
• Microprocessors & Computer Architecture
–Assembly Programming
• Signal and Systems
• Digital Signal Processing (DSP)
• Digital Image Processing (DIP)
–Matlab Programming
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 23
Why use C/C++ ?
• C/C++ still has the largest share in all the
programming languages being used
Programming Application %age Share
Language
1 C/C++ OOP, Embedded 33 %
Systems
2 Java Android App 17 %
development, Web
Development
3 Objective C iOS Development 10 %
4 PHP Web Development 6%
5 Visual Basic GUI, Front End 5%
7 Others … …

• Many languages are derived


CSEE2123: OOP and DS
9/18/2018
from C
© M. Tahir Awan, CUST 24
Microprocessor based System
• A Microprocessor-based systems consists of central processing
unit (CPU), memories, I/O Devices and other peripherals.

• CPU takes input from Address Bus


I/O devices such as
keyboards Memory
• Software Programs Control
direct/instruct CPU to Bus
processes data CPU
Data Bus
• CPU sends output to (mP)
I/O devices such as
monitors, speakers
• CPU stores data and I/O
code in Memory Devices

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 25


Computer Programming
• Microprocessors are highly efficient and fast
number crunching machines
• Computer program is sequence of instructions
used to tell a microprocessor , where to get data,
how to process it and where to show results
• A computer program will
– take input from user,
– modify/process it in some form
– Display result as output or store in memory
• Computer programs can be simple or advanced
applications i.e. OS

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 26


Programming Languages
• Computer Programming Languages can be
divided into two main categories
–High Level Programming Languages
» C/C++
» Java
» Visual Basic
–Low Level Programming Languages
» Machine Language
» Assembly Language

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 27


High Level Programming Languages
• High Level programming languages abstract
details of mP hardware such as internal registers,
addressing modes, control instructions from the
programmer e.g. C/C++ , Java, Visual Basic
• Syntax of high level language is close to human
language .e.g. „if-else‟ , „while‟ statements in
C/C++ are in English language
• High level languages are user friendly and can
be easily ported from one platform to other
• Software tools such as compilers are used to
convert programs written in high level language
to processor readable binary code (machine
code)
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 28
Algorithm
• An algorithm is a procedure for solving a
problem in terms of the actions to be executed
and the order in which those actions are to be
executed. An algorithm is merely the sequence
of steps taken to solve a problem
• Algorithms can be represented by
–Flow-Charts
–Pseudo code

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 29


C/C++ Development Cycle
Editor Disk
Preprocessor program
Preprocessor Disk processes the code.
Compiler creates object
Compiler Disk code and stores
it on disk.
Linker Disk Linker links the object
Primary Memory
code with the libraries

Loader
Loader puts program in
memory.
Disk ..
..
..

Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
.. values as the program
9/18/2018 CSEE2123: OOP and DS ©.. M. Tahir Awan, CUST 30
executes.
Software Tools
• Compiler
• Compiler converts the code written in high-level language
into object code.

• Assembler
• Assembler converts the low level assembly language into
machine code of underlying processor.
• Linker
• Linker uses the object files created by the compiler and
predefined library objects to create an executable.
• Interpreter
• Interpreter interprets the user written code line by line,
every time program is executed. Interpreters are time
consuming and not used in real-time applications.

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 31


Review of C Programming Concepts
C Program Components
• Preprocessor Directive
–#include , #define
• Functions
–void main() { … }
• Variables
–int Num1, Num2;
• Comments
// This is my code > Single Line Comment
/* This is my code > Multi-Line Comment
Part of my Class Assignment */

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 33


C Data Types
• Data types in C Language are :
Data Type Size Description, Range
(byte)
char char 1 -128 ~ 127
unsigned char 1 0 ~ 255
int 4 -215 ~ 215-1 or -231 ~ 231-1
unsigned int 4 0 ~ 65535 or 0 ~ 232-1
int short 2 -32768 ~ 32767
unsigned short 2 0 ~ 65535
long 4 -231 ~ 231-1
unsigned long 4 0 ~ 232-1
float 4 Single Precision Fractional No.

double 8 Double Precision Fractional No.

Bool 1 Boolean TRUE (1) or FALSE (0)

9/18/2018
27CSEE2123: 15 =32768, 231 = 2147483648
= 128, 2OOP and DS © M. Tahir Awan, CUST 34
C\C++ Operators
• Arithmetic/Numeric Operators
(+, -, *, /, %)
• Comparison Operators (Relational Operators)
(>, <, <=, >=, ==, !=)
• Boolean Operators (Logical Operators)
(!, &&, ||)
• Shorthand/Compound Assignment Operators
(+=, -=, *=, /=, %=)
• Increment and Decrement Operators(Unary
Operators)
(++, --)
• Bitwise operators
(&, |, ~, >>, <<, <<<, &=, |=, >>=, <<=)
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 35
C\C++ : Operator Precedence
• Precedence of Operators

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 36


C Input & Output
• Output:
• output means to display some data on screen, printer
or in any file. C programming language provides a
set of built-in functions to output required data.
– printf (“Lecture No. %d”, Lecture);
• Input
• input means to feed some data into program. This
can be given in the form of file or from command line.
– scanf(“%d”, &number);
• C programming language provides a set of built-in
functions to read given input and feed it to the
program as per requirement.

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 37


C Program : Area of a Circle
• Take radius of circle as input and computer area
of circle
/* function to compute area of a circle */
#include <stdio.h>

// function main
void main()
{
const float PI = 3.14159;
float radius, area;
printf("Entre Radius of Circle = ");
scanf("%f", &radius);
area = PI * radius * radius;

printf("Area of Circle = %f\n", area);


} /* end function main */

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 38


C : Structured Programming
• All computer programs can be written using these
control structures:
– Sequence
– Decision Structures
» IF no yes

» IF-ELSE
» SWITCH
– Repetition Structures
no
» WHILE
» DO-WHILE yes
Loop
Body
» FOR

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 39


Repetition Control Structures : Loops
( while, do-while, for )

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 40


C Language Family
• C
• Developed in 1972 by Dennis Ritchie at Bell Labs.
• Procedural language.
• C++
• Developed in 1979 by Bjarne Stroustrup at Bell
Labs.
• Object-oriented programming (OOP) language
• C-Language is subset of C++
• C# (outside scope of the course)
• Multi-paradigm language with support for function,
and OO programming and memory management.
• Developed at Microsoft, release circa 2001.
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 41
C++ : Input & Output
• Input (cin : Standard input stream)
• cin and stream extraction operator „>>‟ is used to
get input from the standard input device
(keyboard)
• Stream extraction operator : >>
• int height ;
• cin >> height;
• Waits for user to input value, press „Enter‟ key.
User input is placed in the variable „height‟
• int feet, inches
• cin >> feet >> inches;

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 42


C++ : Input & Output
• Output (cout : standard output stream)
• cout and stream insertion operator „<<„ is used
to display output to the standard output device
(screen)
• float num = 100;
• cout << num ;
• Displays value of variable number i.e 100 on
screen
• float Temp = 98.6;
• cout << “Temperature = “<< temp << endl;
• endl causes cursor to move to beginning of next
line
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 43
C++ : Preprocessor Directive,
Namespace
• Preprocessor directive (begin with #) is used to
include header files at start of program
• cin and cout are declared in the header file
iostream,
• To use cin and cout in a C++ program, use the
following statement :

#include <iostream>
• Tells preprocessor to include the input/output
stream header file <iostream>

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 44


C++ : Namespaces
• A namespace is a declarative region that
provides a scope to the identifiers (functions,
variables, etc) inside it.
• Namespaces are used to organize code into
logical groups and to prevent name collisions
that can occur especially when program includes
multiple libraries
• „using namespace‟ directive :
• „using namespace‟ directive tells the compiler
that the subsequent code is making use of
names in the specified namespace
• #include <iostream>
• using namespace std;
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 45
C++ Program : Printing Variables
#include <iostream>
using namespace std;

void main(){
int num1, num2;

num1 = 4;
cout << "num1 = " << num1 << endl;

num2 = num1 * 5 - 11;


cout << "num2 = " << num2 << “\n”;

float weight = 0.25 * 100;


cout << “weight = " << weight << endl;

char first = 'D';


cout << "char = " << first << endl;
}

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 46


C Program : Area of a Circle
• Take radius of circle as input and compute area
of circle
/* Program to compute area of a circle */
#include <stdio.h>

// function main
void main()
{
const float PI = 3.14159;
float radius, area;
printf("Entre Radius of Circle = ");
scanf("%f", &radius);
area = PI * radius * radius;

printf("Area of Circle = %f\n", area);


} /* end function main */

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 47


C++ Program : Area of a Circle
• Take radius of circle as input and compute area
of circle
/* Program to compute area of a circle */
#include <iostream>
using namespace std;

// function main
void main()
{
const float PI = 3.14159;
float radius;
cout<<"Entre Radius of Circle = ";
cin>>radius;
float area = PI * radius * radius;

cout<<"Area of Circle = " << area<<endl;


} /* end function main */

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 48


Review of Functions, Arrays & Strings
Modularity
• How to solve a big/complex problem?
• Divide complex problem into small tasks and solve
each task.
• Combine these solutions to solve complex problem

Divide and Conquer

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 50


Advantages of using Functions
• Divide and conquer
– Manageable program development
– Modules can be written and tested separately
– Large projects can be developed in parallel
• Software reusability
– Use existing functions as building blocks for
new programs (reusability)
– Abstraction - hide internal details
– We just need to know what a function does. We
don‟t need to know how it does it
• Avoid code repetition
– Reduces length of program, making a program
moreCSEE1133:Data
9/18/2018 readableStructures © M. Tahir Awan, CUST 51
C Function
• Function prototype #include <iostream>
using namespace std;
– Remember semicolon!!
int product(int x, int y);
• Function Definition void main(){
int var1 = 3, var2 = 5;
• Function Call
• Function Return int ans = product(var1, var2);
cout<<"var1 = "<<var1<<endl<<
– optional for functions "var2 = "<<var2<<endl;
of type void cout<<"var1*var2 = “<<ans<<endl;
» or just, return;
}
/* function definition */
int product(int x, int y)
{
int result = x * y;
return result;
}

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 52


Function Definition: Example
• Function to Compute factorial of a number

Function name
Return Type
int factorial(int n)
{ Function
Local Variables int factres = 1; Arguments
while(n>1)
{
factres = factres*n;
n--;
}
Return Value
return factres;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 53
Functions Example
• Write a function to compute average of two
numbers
#include <iostream> /* User defined Function */
using namespace std; float avg (float val1, float
val2)
float avg(float num1, float {
num2); float result;
result = (val1+val2)/2;
void main()
{ return result;
float data1, data2; }
data1 = 5;data2 = 7;
cout<<“Average =”<<
avg(data1, data2)<<endl;

data1 = 15;data2 = 9;
cout<<“Average =”<<
avg(data1, data2)<<endl;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 54
Function Argument/Parameter
Passing
• Call by value
– formal parameter receives the value of the
actual parameter
– function can NOT change the value of the
actual parameter
• Call by reference
– actual parameters are passed by reference/
pointers
– function can change the value of the actual
parameter
– Function can return multiple values when using
call by reference
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 55
Passing Arguments by Reference :
Example
#include <iostream>
• Swap values of using namespace std;
two variables
void swap(int *a, int *b);

void main() {
int q = 3;
int r = 5;
swap(&q, &r);
cout<<"q = “<<q<<endl;
cout<<“r = “<<r<<endl);
}
/* function definition */
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 56


Scope of a Variable
• Local scope
– a local variable is defined within a function or
a block and can be accessed only within the
function or block that defines it
• Global scope
– a global variable is defined outside the main
function and can be accessed by any function
within the program file.

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 57


Scope of a Variable : Example
• Local and global variables
#include <iostream> /* User defined Function */
using namespace std; float avg (float val1, float
val2)
float avg(float num1, float {
num2); float result;
float var1 ; result = (val1+val2)/2;
void main(){
return result;
float data1, data2; }
data1 = 5;data2 = 7;
cout<<“Average =”<<
avg(data1, data2)<<endl;
{
float data3 = 15;data4 = 9;
cout<<“Average =”<<
avg(data3, data4)<<endl;
}
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 58
Static Variables „static‟
• A static variable preserves its value
• Requests that memory for a local variable be
reserved throughout the execution life of the
program.
• The static storage class does not affect the
scope of the variable i.e. variable cannot be
accessed outside of function it is defined in
• Scope of static variable is local

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 59


Reference variable
• C++ introduces a new kind of variable known
as the reference variable.
• A reference variable provides an alias
(alternative name) for a previously defined
variable.

• Syntax:
dataType& reference-name = variable-name

• Example:
float total = 100;
float& sum = total;
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 60
Recursion : Recursive Function
• A function that calls itself is called a recursive
function
• Recursive function only solves simplest case
(base case)
• Recursive function divides problem into two :
– If base case, Solve the problem
– If not base case, simplify the problem and
recursively call the function again
• Recursively calling itself function will ultimately
reach base case and solve it

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 61


Recursive Function : Example
• Using recursive function to compute
factorial
/* find factorial */ /* recursive function */
#include <iostream>
using namespace std; int fact(int k)
{
int fact(int k); if (k <= 1)
return 1;
int main() { else
int n, ans; return k*fact(k-1);
}
cout<<"Enter number =";
cin>>n;

ans = fact(n);

cout<<"Factorial= “<<ans;

return(0);
}

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 62


Math Library :
Hypotenuse of right triangle
• Using „sqrt(), pow() to find hypotenuse
#include <iostream> /* recursive function */
#include <cmath>
using namespace std; float hypotenuse( float base,
float perp)
{
float hypotenuse( float, float);
float hyp;
hyp = sqrt( pow(base,2)+
void main() { pow(perp,2) );
float x, y;
cout<<"Enter base ="; return hyp;
}
cin>>x;

cout<<"Enter perpendicular =";


cin>>y;
float z = hypotenuse(x,y);
cout<<"Hypotenuse = “<<
z<<endl;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 63
One-Dimensional Arrays
• An array is an indexed data structure to describe
several variables having the same data type and
name
int y[100];

y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]

• An element of an array is accessed using the array


name and an index or subscript,
• In C\C++, the subscripts always start with 0
• Name of the array is the address of the first
elementCSEE1133:Data
9/18/2018
and the Structures
index/subscript is the offset
© M. Tahir Awan, CUST 64
Arrays : Example
• Program to find //Count number of students
#include <iostream>
number of students using namespace std;
with F-Grade
void main() {
• Student will have F- int i, count;
Grade if int marks[10];

Marks < 50 , for (i=0; i<10; i++) {


cout<<“Enter Marks = “;
cin>>marks[i];
}
count = 0;
for (i=0; i<10; i++) {
if ( marks[i] < 50 )
count ++;
}
cout<<“Cout of Fail Students =”<<
count<<endl;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 65
Multidimentional Arrays
• Multidimensional arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
– datatype array_name[row_size][column_size];
int a[3][4];
Column 0 Column 1 Column 2 Column 3

Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]

Column subscript
Array name

Row subscript
66
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST
2D Arrays in Memory
• 2D Arrays in C can be used to define matrices
• 2D Arrays are stored row-wise (row-major order)
in computer memory
4
int matrix[3][4];
1
0
Row 0 4 1 0 2 2D array in
memory 2
-1
Row 1 -1 2 4 3 2
4
Row 2 0 -1 3 1 3
0
-1
Column 3 3
Column 0 Column 1 Column 2
1

9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 67


2-D array : Example
• Adding two matrices of size 3x3
//Adding two matrices cout<<“Sum Matrix =”;
#include <iostream>
using namespace std; for (i=0; i<3; i++){
for (j=0;j<3;j++){
void main() {
cout<<sum[i][j]<<endl;
int i,j,k;
}
int sum[3][3];
}
int m1[3][3]= {{3,4,9},
}
{12,34,40}, {11,44,31}};
int m2[3][3]= {{13,14,9},
{19,53,54}, {11,4,23}};

for (i=0; i<3; i++){


for (j=0;j<3;j++){
sum[i][j] = m1[i][j] +
m2[i][j];
}
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 68
Strings
• A string is an array of characters
– char data[10] = “Hello”;
– char data2[] = {„H‟, „e‟, „l‟, „l‟, „o‟, „\0‟}
• Print strings
– cout << data;
• String can be accessed char by char
– data[0] is first character
– Null character '\0' terminates strings
End of String
Symbol „\0‟

H e l l o \0

0 1 2 3 4 5 6 7 8 9

9/18/2018 CSEE1133:Data Structures data


© M. Tahir Awan, CUST 69
Home Assignment
• Find the answer to the following question :
– What is the difference between C and C++
• Revise following topics from Computer
Programming course (CSEE1122)
–C Functions
–Arrays & Strings
–Pointers
–Structures

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 70


C++ for C Programmers :
• Online course on C++
– Video Lectures, Tutorials, Slides

• https://www.coursera.org/learn/c-plus-plus-a
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 71
Hour of Code : Programming Tutorials
• http://hourofcode.com/us

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 72


C/C++ Programming Resources
• Online C/C++ Programming IDE
• http://www.tutorialspoint.com/codingground.htm#co
dingground
• Visual Studio Community 2013 (Free Download)
• http://www.visualstudio.com/en-us/products/visual-
studio-community-vs
• MSDN Library
• https://msdn.microsoft.com/en-us/library/

• http://www.cplusplus.com/doc/tutorial/

9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 73

You might also like