Dfc20303 - Chapter 4 (Array, Pointer Structure) (1)
Dfc20303 - Chapter 4 (Array, Pointer Structure) (1)
PROGRAMMING
FUNDAMENTALS
CHAPTER 4
ARRAY, POINTER AND STRUCTURE
01 05
COURSE LEARNING DESCRIBE THE USE OF
OUTCOME STRUCTURES
CONTENT
ARRAYS
04
IDENTIFY THE RELATIONSHIP
BETWEEN POINTER AND ARRAY
COURSE LEARNING
OUTCOMES
CLO 1:
Apply programming elements and articulate how they are
used to achieve a working program. (C3, PLO2)
CLO 2:
Demonstrate appropriate algorithm based on well-defined
problem using C++ programming language. (P3, PLO3)
02
EXPLAIN THE USE OF
ARRAYS
LEARNING OUTCOMES
4.1 Explain the use of arrays
Define an array.
Describe the components of an array :
a. index
b. element
c. size
Explain types of array :
a. One dimensional
b. Two dimensional
Declare one dimensional
Initialize one dimensional array
Illustrate the one dimensional array.
Access individual element of one dimensional array
DEFINE AN ARRAY
DEFINITION
An array is a collection of elements of the same type placed in contiguous memory locations that can
be individually referenced by using an index to a unique identifier.
Most applications require the processing of multiple data items that have common characteristics.
Each array element (individual data item) is referred to by specifying the array name followed by one or
more subscripts / index (position of elements).
CHARACTERISTIC OF ARRAYS
An array holds elements that have the same data type
Two-dimensional array elements are stored row by row in subsequent memory locations.
Array name
index/
subscripts
(Start with 0
until n-1)
Element
ONE DIMENSIONAL ARRAY
One-dimensional array is an array with one subscript [].
EXAMPLE
char title [7];
float price[100];
int number[5];
SYNTAX
DataType arrayName [size];
ONE DIMENSIONAL ARRAY
Example of array with default value of zero.
Example:
int Sem[] = {2, 5, 6, 4, 7, 3};
The following statement access the value in the third
element of Sem:
Reminder
int a = 100;
int *p;
p = &a;
REASON WHY USE POINTER
To call a function by reference so that the data passed to the function can be changed inside the
function.
To create a dynamic data structure which can grow larger or smaller as necessary.
POINTER DECLARATION
To declare a pointer, asterisk symbol " * " needs to be add and located after variable type and before a
pointer name.
int *ptr;
data_type *pointer_name; double* ptr;
float * ptr;
POINTER DECLARATION
Consider the following declaring variables of Pointer:
*
It returns the contents of the memory
location pointed to. The indirection
&
It returns the address of the memory where
a variable is located. We called it Address
operator is also called dereferencing operator.
operator.
int *x;
int c=100; int *x;
int p; int c=100;
x=&c; x=&c;
p=*x;
POINTER OPERATOR
* INDIRECTION/DEREFERENCING OPERATOR
We can access to the value stored in the variable pointed to by using the dereferencing operator
(*).
int a = 100;
int *p = &a;
cout << a << endl;
cout << &a << endl;
cout << p << " " << *p << endl; Result is:
cout << &p << endl; 100
**A pointer is also a variable, so it has its own memory address 1024
1024 100
1032
POINTER OPERATOR
& ADDRESS OPERATOR
We can access the address of value stored in the variable pointed to by using the address
operator (&).
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl; Result is:
cout << "The address of b is: " << &b << endl; The address of a is: 1020
The address of b is: 1024
ASSIGN AN ADDRESS OF VARIABLE TO POINTER
To get the
address of a Since pointers only hold
variable, we can int nvalue = 5; addresses, when we
assign a value to a pointer,
use the address
int *ptr = &nvalue; the value has to be an
of operator (&):
address
ACCESSING THE VALUE AT THE MEMORY ADDRESS
HELD BY A POINTER
int nValue = 5;
int *pnPtr = &nValue; //assign address of nValue to pnPtr
Process accessing a variable's value
by a pointer is called indirection, since
the value of variable is accessed cout<<*pnPtr<<endl; //print the value of pnPtr pointed to
indirectly. cout<<pnPtr<<endl; //print the address that pnPtr is
holding
REFERENCE VARIABLES
A reference is an additional name to an existing memory location
REFERENCE VARIABLES
A reference variable serves as an alternative name for an object.
int m = 10;
int &j = m; // j is a reference variable
j = 18;
int main()
{
int a; int b;
cout << "Enter value of A: ";
cin >> a;
cout << "Enter value of B: ";
cin >> b;
int *ptrA=&a;
int *ptrB=&b;
cout << "Value of ptrA is " << *ptrA << " sored in address "<< ptrA<<"\n";
cout << "Value of ptrB is " << *ptrB <<" sored in address "<< ptrB<<"\n";
return 0;
}
MEMORY ALLOCATION
int *bobby;
bobby = new int [5];
Reminder
p = &balance[0]; *(p + 3)
// also assigns address of array The preceding notation is referred to as
balance to p pointer/offset notation. The parentheses are
necessary, because the precedence of * is higher
than the precedence of +.
EXPECTED OUTPUT
ARRAY VS POINTER: EXERCISE 2
Complete the program code below, run and show the output:
#include <iostream>
using namespace std;
int main()
{
int Number[] = { 5,10,15,20,25 };
int *pNumbers = Number;
//a. Write a command to display the 4th element of array Number
//d. write a command to display the last element in array Number using pointer
return 0;
}
05
DESCRIBE THE USE OF
STRUCTURES
LEARNING OUTCOMES
4.4 Demonstrate the use of structures
Describe structures
Identify the difference between structure and arrays.
Define and declare a structure
Assign values to a structure variable.
Access member variables of a structure.
Apply the coding standards and best practices for structure.
Illustrate structures in memory.
Solve a given problem by writing program
Access member variables of a structure using pointer.
INTRODUCTION TO STRUCTURE
Structure is a collection of related data items, possibly of different types.
A structure type in C++ is called struct.
Structure can be referred to using the same name.
The preceding structure definition does not reserve any space in memory but the definition
creates a new data types that is used to declare variables.
Data structure
A data structure is a group of data elements grouped together under one name. These data
elements, known as members, can have different types and different lengths.
INTRODUCTION TO STRUCTURE
Structures hold data that belong together.
Examples:
EXAMPLE
struct structure_name
struct addressBook
{
{
member_type1 member_name1;
string name;
member_type2 member_name2; string address;
member_type3 member_name3; string phone;
…….. };
};
STRUCTURE DECLARATION
Let us assume that we want to create an account. We would need the following information:
STRUCTURE DECLARATION
struct structure_name
{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
……..
}object name/structure variables;
DECLARE STRUCTURE VARIABLE
Data structures variable are declared in C++ using the following syntax:
DECLARE STRUCTURE VARIABLE
Declare a structure named Student that has ID of 10 characters, name of 40 characters, and marks
of integer type. Then, declare an array called Students (object) with the size 25 for the structure.
ASSIGN VALUES TO A STRUCTURE VARIABLES
Given the facts about a computer, write the structure.
Manufacturer: HP
Memory: 512
Price: 2199.90
ASSIGN VALUES TO A STRUCTURE VARIABLES
Given the information structure for workers as follows:
struct infopekerja
{ Write programming statement to assign the following workers
information into the pekerja[9] variable:
string nama;
float gaji; Nama: Ali
} pekerja [10];
Gaji: 650.00
ANSWER!
ACCESS VALUES IN A STRUCTURE VARIABLE
Members of a structure are accessed using the member access operators :–
the dot operator ( . ) and the arrow operator ( -> )
Nesting one structure within another saves time when the program needs similar
structures.
NESTED STRUCTURE
output
EXAMPLE PROGRAM USING STRUCTURE
output
EXERCISE
By using struct, write a program which will accept name, ID number and test marks
from Test 1- Test 5. Your program should calculate and display the total and
average of marks for the students.
THANK YOU