0% found this document useful (0 votes)
8 views8 pages

An Introduction To Data Structure

Data structure , array list .

Uploaded by

Aala' Mansour
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)
8 views8 pages

An Introduction To Data Structure

Data structure , array list .

Uploaded by

Aala' Mansour
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/ 8

Data Structure

‫هياكل البيانات‬
‫المحاضرة االولى‬ ‫وجدان ياسين‬.‫م‬

An Introduction to Data Structure


Data Structure Definition:
• In computer science, a data structure is a particular way of storing and organizing data in a
computer’s memory (or sometimes on a disk) so that it can be used efficiently.

Classification of Data Structures:


• Data Structure are generally classified into primitive and nonprimitive data structure. The
primitive data type consist of characters that cannot be divided such as (integer, real, character
and Boolean). They are also called simple data type. The figure shows
the classification of data structures.

Non-Primitive Data Structure


Linear Data Structure:

• A data structure is said to be linear if its elements form a sequence, or, in other words, a linear
list. There are two basic ways of representing such linear structures in memory. these linear
structures are called Arrays and Linked List.

The operations that normally performs on any linear structure, whether it be an array or a
linked list, include the following:
1. Traversing: means to visit all the elements of the array in an operation is called traversing.
2. Sorting: Re-arrangement of values in a list in a specific order
3. Searching: The process of finding the location of a particular element in a list is called
searching.
4. Insertion: Adding a new element to the list.

1
5. Deletion: Removing an element from the list.
6. Merging: Combining two lists into a single list

Linear Data Structure:

1- Array:
Array classified in C++ in three category:
1. One – Dimensional Arrays.
2. Two – Dimension Arrays.
3. Multidimensional Arrays.

One – Dimensional Arrays


• A one dimensional array is used when it is necessary to Keep a large number of items in
memory and reference all the items in uniform manner.
• Int N[100];
• That is mean we reserves 100 successive memory locations and each location is large enough
to conation single integer. In C++, the array element indices are 0-99.

• The address of the first location is called the base address of the array and is denoted by base
(BA) and the rest of the array elements come after this address.
• Computer does not need to keep track of the address of every array element, but need to track
only the address of the first element of the array Base Address (BA) and to reach to any array
element and the compiler use the following formula to do so.
Loc ( N [I] ) = BA + ( I ) × Size
• Loc N[I] : The location of the element I, BA: Fixed base address, Size: A fixed constant, is
also known as size of the data type.

Example : Consider an one dimension array (N) with size 10 and the base address equal (3002)
and each element of the array occupy 1 byte. find the address the element number six.
Loc ( N [I] ) = BA + ( I ) × Size
then, Loc ( N [5] ) = 3002 + ( 5 ) × 1
Loc ( N [5] ) =3007.

2
‫الصيغة العامة لإلعالن عن المصفوفة ذات البعد الواحد‪:‬‬

‫; ] ‪data type array name [ index‬‬

‫حيث‪:-‬‬

‫‪ : data type‬نوع بيانات المصفوفة‬

‫‪ :array name‬اسم المصفوفة ويراعى فيه شروط تسمية المتغيرات‪.‬‬

‫‪ :index‬دليل المصفوفة ) عدد عناصرها ( وهي عبارة عن قيمة صحيحة يمكن أن تكون محدده)ثابتة (أو مدخلة من قبل‬
‫المستخدم)متغيرة‪( .‬‬

‫أمثلة‪:‬‬

‫;] ‪1. int x [ 50‬‬ ‫إعالن عن مصفوفة حجمها ‪ 50‬وبياناتها من النوع الصحيح‬

‫;] ‪2. float y [ 20‬‬ ‫إعالن عن مصفوفة حجمها ‪ 20‬وبياناتها من النوع الحقيقي‬

‫; ] ‪3. char name [ 15‬‬ ‫إعالن عن مصفوفة حجمها ‪ 15‬وبياناتها من النوع الحرفي‬

‫; }‪4. int x [ 3 ] = { 5,10,15‬‬ ‫إعالن عن مصفوفة حجمها ‪ 3‬وبياناتها من النوع الصحيح مع إعطائها قيم ابتدائية‬

‫إعالن عن مصفوفة حجمها ‪ 4‬وبياناتها من النوع الحرفي مع إعطائها قيم ابتدائية ; } '‪5. char y [ 4 ] = { 'a','b','c','d‬‬

‫‪3‬‬
Program -1-: Write C++ program to read one-dimensional array consisting of 10
elements, then give the summation for these elements and print it :

#include <iostream>
using namespace std;
int main ()
{
int a[10];
int sum, i;
sum=0;
for(i=0;i<10;i++)
{
cin>>a[i];
sum +=a[i];
}
cout<< "sum=" << sum <<"\n";
return 0;
}
Program-2: Write a program to read one-dimensional array has 5 real values and
order them in ascending form and print them (‫?)ترتيب تصاعدي‬

#include <iostream>
using namespace std;

const int n=5;


int main( )
{
float x[n],temp;
int i,j;
for(i=0;i<n;i++)
{
cout<<"enter element["<<i<<"]\n";
cin>>x[i];
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(x[i]>x[j])
{

4
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
cout<<"elements after sorting\n";
for(i=0;i<n;i++)
cout<<x[i]<<"\n";
return 0;
}

Two – Dimensional Arrays


• 2D array is a data structure type that consists of a set of elements that are all of the same type,
and all elements are distributed on a set of rows and columns that represent the size of the array.
• Int N[3][5]; That is mean we reserves (3*5=15) successive memory locations and each
location is large enough to conation single integer. The number of Rows or Columns is called the
range of the dimension.
• The array N will be represented in the memory by block of (3 × 5) sequential memory location.
Programming language will store array N either :
1. Column by Column: called (Column-Major Order) Ex: Fortran, Matlab.
2. Row by Row: called (Row-Major Order) Ex: C, C++, Java.

Column-Major Order :
• Loc ( N [I][J] ) = BA + ( m × J + I ) × Size
• Where:
• Loc N[I][J] : the location of the element I,J.
• BA: fixed base address.
• m: number of row.
• Size: a fixed constant, is also known as size of the data type

Example : Column-Major Order


Consider a two dimension array (N) with size (m=3 × n= 5) and the base address equal (300) ,
each element of the array occupy 1 byte. find the address to element N[1][2].Suppose the
programming store 2D using Column-Major:

Loc ( N [I][J] ) = BA + ( m × J + I ) × Size


Loc ( N [1][2] ) = 300 + ( 3 × 2 + 1 ) × 1
Loc ( N [I][J] ) = 307

5
Row-Major Order :
• Loc ( N [I][J] ) = BA + ( n × I + J ) × Size
• Where:
• Loc N[I][J] : the location of the element I,J.
• BA: fixed base address.
• n: number of column.
• Size: a fixed constant, is also known as size of the data type

Example : Consider a two dimension array (N) with size (m=3 × n= 5) and the base
address equal (600) and each element of the array occupy 1 byte. find the address to
the element N[2][3].Suppose the programming store 2D using Row-Major.
Loc ( N [I][J] ) = BA + ( n × I + J ) × Size
Loc ( N [2][3] ) = 600 + ( 5 × 2 + 3 ) × 1
Loc ( N [I][J] ) = 613

:‫الصيغة العامة لإلعالن عن المصفوفة ذات البعدين‬


data type array name [row size] [column size];
‫حجم الصفوف اسم المصفوفة نوع المصفوفة‬ ‫حجم األعمدة‬

:‫أمثلة‬
1. int y[4][3] ‫ اعمدة‬3‫ صفوف و‬4 ‫ كمصفوفة ذات بعدين وتتكون من‬y ‫اعالن عن المصفوفة الصحيحة‬
2. y[4][3]=23; y ‫ لعنصر الصف الرابع والعمود الثالث من المصفوفة‬23 ‫إسناد القيمة‬
3. int y[4][3]={{5,0,-4},{-2,3,1},{4,7,6},{9,8,-1}}; ‫ اثناء التصريح‬y ‫إسناد قيم ابتدائية لعناصر المصفوفة‬

Program-3: Write a program to read 2 (two-dimensional) integer array of size


(3x3) ,then print the elements as binary order and sum these arrays elements in
third array and print it?

#include <iostream>
using namespace std;

6
int main( ) {
int a[3][3],b[3][3],c[3][3], i, j;
cout<<"input first array a[3][3]:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\n";
}
cout<<"input second array b[3][3]:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
cout<<"\n";
}
cout<<"print first array a[3][3] :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
cout<<"the second array b[3][3] look like :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
cout<<" sum of A & B arrays in C[3][3] :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
return 0;}
7
H.W:

• H.W.1: Consider Int X[3][4] ,what is the address of the X[2][2] if the base address equal (300)
and each element of the array occupy 1 byte. Suppose the programming store 2D using Column-
Major.
• H.W.2: Consider Int X[5][3] ,what is the address of the X[3][2] if the base address equal (100)
and each element of the array occupy 2 byte. Suppose the programming store 2D using Row-
Major.
• H.W.3: Write C++ program to give the average for array consist of 5 real numbers.
• H.W.4:
:‫ ثم عمل ما يلي‬A(4x3) ‫اكتب برنامجاً لقراءة عناصر مصفوفة‬
.‫طباعة المصفوفة بشكل ثنائيًمرتب‬- a
‫ طباعة األرقام الموجبة و األرقام السالبة و األرقام الصفري ًة في المصفوف ًة‬-b
.‫طباعة عدد األرقام الزوجي ًة والفردية‬- c

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy