Professional Documents
Culture Documents
Lec1 1
Lec1 1
waitheart_iba@yahoo.com
Assessment
Mid exam 25
Final lab exam 15
Home works 20
waitheart_iba@yahoo.com
Introduction
Throughout this course examples will be given in
C++. Most of the time its non-object-oriented
features will be used and only in the few problems
that will have to be implemented as classes, its
object-oriented features will be used.
• The language is only being used as a means of
expressing the algorithms. It should be realized that
implementation could be in any modern high level
language as they all tend to have equivalent
features.
waitheart_iba@yahoo.com
Introduction
waitheart_iba@yahoo.com
Introduction
waitheart_iba@yahoo.com
C-DATA TYPES AND DATA STRUCTURE
CLASSIFICATION
waitheart_iba@yahoo.com
Data structures
waitheart_iba@yahoo.com
ARRAYS
A one dimensional array Allows a set of data of a specified type to
be held in a structure where the individual elements can be
accessed by referral to a common variable name and their element
number.
e.g.
int month[12]; is a declarative specifying an array
that can hold 12 integers.
In C++ the element numbers are from 0 - 11.
waitheart_iba@yahoo.com
ARRAYS
Most compilers will initialize the elements to zero automatically (but
always play on the safe side and do it yourself if necessary!).
sum = 0;
for(j = 0; j < 12; j++)
sum = sum + month[j];
waitheart_iba@yahoo.com
ARRAYS
• A double dimension array can be declared as :
int sales[5][12]
The above declaration defines an array with 5 rows and 12
columns.
Input could be:
for(i = 0; i < 5; i++)
{ cout<<"Input the monthly sales for salesperson
"<<i+1<<endl;
for(j = 0; j < 12; j++)
{ cout<<"Month "<<j+1;
cin>>sales[i][j];
}
}
waitheart_iba@yahoo.com
ARRAYS
• Adding all the columns could be:
• int sumcol[12];
for(j=0;j<12;j++)
{
sumcol[j] = 0;
for (i=0;i<5;i++)
sumcol[j] = sumcol[j] + sales[i][j];
}
• Note that this routine puts the column sums into the array sumcol.
waitheart_iba@yahoo.com
ARRAYS
• Searching for a target:
waitheart_iba@yahoo.com
ARRAYS
• Shifting elements of an array to the right and putting the last element
to the first position:
• #include<iostream.h>
• void main()
• {
• int num[5],i,val;
• for (i=0;i<5;i++) cin>>num[i];
• val=num[4];
• for (i=3;i>=0;i--)
• num[i+1] = num[i];
• num[0] =val;
• for (i=0;i<5;i++) cout<<num[i]<<endl;
• } waitheart_iba@yahoo.com