Sunteți pe pagina 1din 39

+

MTS3023
Data Structure
Arrays, Pointers and Struct
+
ARRAYS
+
What is an array?

 An array in a variable with multiple values

 It allows you to Stack the same variable with different values.

 They allow you to use a single variable and attach different


values to it.
int list ;
list = 10 ;
-------------------------------
int list[4];
list[0] = 7;
list[1] = 8; Can you see
list[2] = 6; the difference
list[3] = 1; ?
+
Initializing and assigning values of an
array

Option 1: Option 3:
int myarray[ 5 ]; int myarray[5];
for ( int i = 0; i < 5; i++ ) myarray[0] = 5;
{ myarray[1] = 8;
myarray[ i ] = 0; myarray[2] = 2;
} myarray[3] = 1;
myarray[4] = 9;
Option 2
int myarray[ 5 ] = { 2, 7, 4, 8, 5 };

It’s up to you to choose which one.


+
Printing out each of the values in the
array – use for loop

int main()
{
int i;
int list[ 10 ] = { 2, 7, 4, 8, 5, 4, 9, 7, 6, 3 }; 2
for ( i = 0; i < 5; i++ ) 7
{ 4
8
cout<<list[ i ]<<endl; 5
4
} 9
7
return 0; 6
} 3
+
2 Dimensional arrays

 Example of the table


 3 rows and 4 columns

 How to declare 2D arrays ?

#include <iostream>
using namespace std;
5 4 5 5
int main() 3 7 4 7
{ 1 6 8 4
int table[3][4];
return 0;
}
+
How to assign values?

#include <iostream>
using namespace std;

int main()
{
int table[3][4] = {{5,4,5,5},{3,7,4,7},{1,6,8,4}};
return 0;
}
5 4 5 5
3 7 4 7
1 6 8 4
+
How to access or display the values in
the 2D arrays ?

 Use nested loop (outer loop and inner loop)

 Outer loop – for rows

 Inner loop – for columns


for (i= 0 ; i < 3 ; i++) //outer loop
{
for (j = 0; j < 4 ; j++) //inner loop
{
cout << table[i][j]; 5 4 5 5
}
} 3 7 4 7
1 6 8 4
+
Wait…

 We can also use a nested loop to enter values in


the 2 D arrays

int table[3][4];
for (i= 0 ; i < 3 ; i++) //outer loop
{
for (j = 0; j < 4 ; j++) //inner loop
{
cin>>table[i][j];
}
}
+
POINTERS
+
Introduction

 A pointer is the memory address of a variable

 Pointers tell where to find a variable


 Pointers "point" to a variable by telling where the
variable is located

 How to declare a pointer ?


 int *p ;
(a pointer, p points to the address where the int type value is stored)

 How to assign a value?


 p = &v ;

(a pointer, p points to the address of v. What is the value of v ?)


+
Let’s refresh…
value

Memory management system: name


address
 When we declare a variable, normally 3
spaces are allocated:
Example:
1) The address of that variable – int v;
automatically given/generated by the
computer value
name v
2) The name of the variable - depends
on the given name by us address F001111E

3) The value of the variable – depends v = 10;


on the value that we want to give.
MUST BE THE SAME TYPE WITH OUR
DECLARATION. value 10
name v
address F001111E
+
How about a pointer ?
int *p ; value
name p
address F005555E

value 10 cout<< v ;
Meanwhile….
int v; name v
v= 10 address F001111E

value F001111E
p = &v; name p
address F005555E

cout<< p ;
cout<<*p ;
+
Let’s check…
#include<iostream.h>
using namespace std;
int main()
10
{
int v; v
int *p; F005555E
v = 10;
cout<<v<<endl;
cout<<&v<<endl;
p = &v;
cout<<p<<endl; What is the output ?
cout<<*p<<endl;
*p = 8;
cout<<v<<endl;
cout<<p<<endl;
cout<<*p<<endl;
return 0;

}
+
+
Remember:

 Declaring a pointer:
 Eg:
int *p ;
double *p ;
float *p ;
We can also assign a value like this:

 Assigning a value: int *p ;


 Eg: p = new int;
*p = 45;
p = &v ;
+
+
Let’s check again
 #include<iostream.h>
using namespace std;
int main() p1
{ F005555E
int *p1, *p2;
p1 = new int;
p2 = p1;
*p1 = 42;
cout<<*p1<<" "<<*p2<<endl;
*p2 = 53; p2
cout<<*p1<<" "<<*p2<<endl; F007777E
p2 = new int;
*p2 = 45;
cout<<p1<<*p1<<p2<<*p2<<endl;
*p1 = *p2;
cout<<p1<<*p1<<p2<<*p2<<endl;
p1 = p2;
cout<<p1<<*p1<<p2<<*p2<<endl;
return 0;
}
+
Memory Management

 Static Memory Allocation


 Memory is allocated at compilation time

 Dynamic Memory
 Memory is allocated at running time
+
Static vs. Dynamic Objects

 Static object  Dynamic object

(variables as declared in function calls)  Memory is acquired by


 Memory is acquired program with an allocation
automatically request
 new operation

 Memory is returned  Dynamic objects can exist


automatically when object beyond the function in which
goes out of scope they were allocated

 Object memory is returned by


a deallocation request
 delete operation
Memory Allocation

new
delete
{ int* ptr;
int a[200]; ptr = new int[200];
… …
} delete [] ptr;
+ Object (variable) creation: New

Syntax

ptr = new SomeType;

where ptr is a pointer of type SomeType

Example:
int* p = new int;

Uninitialized int variable

p
+Object (variable) destruction: Delete

Syntax
delete p;
storage pointed to by p is returned to free store and p is now
undefined
Example
int* p = new int;
*p = 10;
delete p;

p 10
+ Example

Dynamic Memory Allocation


 Request for “unnamed” memory from the Operating System

int *p, n=10;


p = new int; new

p
p = new int[100];

new

p
new
p = new int[n]; p
+
Memory Allocation Example
#include <iostream>
using namespace std;
void main()
{
int n;
cout << “How many students? “;
cin >> n;
int *grades = new int[n];
for(int i=0; i < n; i++){
int mark;
cout << “Input Grade for Student” << (i+1) << “ ? :”;
cin >> mark;
grades[i] = mark;
}
...
printMean( grades, n ); // call a function with dynamic
array
...
}
+
Freeing (or deleting) Memory
+ A Simple Dynamic List Example
#include <iostream>
using namespace std;

int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
+
Dangling Pointer Problem

int *A = new int[5];


for(int i=0; i<5; i++)
A[i] = i;
int *B = A;

A
0 1 2 3 4
B
delete [] A;
B[0] = 1; // illegal!

Locations do not belong to program

A —

?
B
+
Memory Leak Problem
int *A = new int [5];
for(int i=0; i<5; i++)
A[i] = i;

A 0 1 22 3 4

A = new int [5];


These locations cannot be
accessed by program

A 0 1 2 3 4

— — — — —
+
Dynamic 2D Array
table 32 18 12 24
table[0]
table[1] 13 11 16 12 42 19 14
 A dynamic array is an table[2]
array of pointers to 22
save space when not table[3]
all rows of the array table[4] 13 13 14
are full. table[5]
 int **table;
11 18
table = new int*[6];

table[0] = new int[4];
table[1] = new int[7];
table[2] = new int[1];
table[3] = new int[3];
table[4] = new int[2];
table[5] = NULL;
+
Memory Deallocation

 Memory leak is a serious bug!

 Each row must be deleted individually

 Be careful to delete each row before deleting the table


pointer.
 for(int i=0; i<6; i++)
delete [ ] table[i];
delete [ ] table;
+
STRUCT
+
Data types

 Simple data types


 Eg:
 integer
 Float
 double
 char

 Have more than one data types = struct


 Eg: The name of the data type:
struct Student Student
{
This struct combines 3
char id[4];
char name[20];
simple data types (char [ ],
int age;
char [ ] and int = 3
}; members)
+
Let’s explore the student struct
struct Student
{
char id[4]; What is the struct’s name ?
char name[20]; How many elements/members
int age; inside Student data type ?
}; How to declare objects of the
Student?

Student student1 ;
Student student2 ;
Let’s compare:

int age ;
Student student1 ;
+
Let’s learn more - struct

 Memory management
age
 Simple data type F005555E
int age ;

 Struct data type


struct Student
{
char id[4];
char name[20]; id name age
int age;
}; student1
Student student1 ; F008888E
+
Assigning values

 If simple data type: 19


int age ;
age = 19 ;
age
F005555E
 Struct object?
struct Student
{
char id[4];
char name[20];
int age; D2010102 Ida 19
}; 22
Student student1 ;
id name age
strcpy(student1.id, “D201010222”);
strcpy(student1.name,“Ida”); student1
student1.age = 19;
F008888E
+
Example of a program

#include<iostream.h>
using namespace std;

struct Student {
char id[14];
char name[20];
int age;
};

int main()
{
int age;
Student student1;
age = 19;
strcpy(student1.id,"D201010100");
strcpy(student1.name,"ida");
student1.age = 19;
cout<<age<<endl;
cout<<student1.id<<student1.name<<student1.age<<endl;
return 0;
}
+
Simple Exercise

 Create an array that consists of 5 names and print out the


names.
 Example of the output:
1. Raymond Chan
2. Linda Safari
3. Tuty Lina Alwi
4. Mohd Nizam Sofian
5. Haslinda Binti Harun
+
Great tutorial

 http://www.youtube.com/watch?v=XEzM77ICfBU

 http://www.youtube.com/watch?v=RwDttcHRYHA&feature=related

 http://www.youtube.com/watch?v=0k23xe3c4es&feature=mfu_in_ord
er&list=UL

S-ar putea să vă placă și