0% found this document useful (0 votes)
35 views62 pages

6 Array

Uploaded by

muhdfaridmfd0
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)
35 views62 pages

6 Array

Uploaded by

muhdfaridmfd0
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/ 62

Chapter 6:

Arrays
Chapter contains

• Single dimensional array [ ]


• Multiple dimensional array [ ][ ], [ ][ ][ ]
• Pointer
WHAT IS ARRAY?

• Symbol of array : [ ]
• Array enable a variable to store more than 1 data,e.g. x[8]
• Data can be stored as integer, character, float
• Scenario: normally we stored value as variable a,b,c. How
about if we have 1000 variables ? You can do it by a[1000]
• Array- A set of data with similar properties and which are
stored in consecutive memory location under common
variable name
Type of array

• Array in integer: int a[10] array ‘a’ with capacity of 10,


it able to store 10 sets of integer value.
• Array in float: float sum[5]  array ‘sum’ with capacity of
5, it able to store 5 sets of float value.
• Array in char: char no[4]  array ‘no’ with capacity of 4, it
able to store 4 set of alphabet.
Example to define array variables:
• Integer type: int x[10]
• Float type: float value[50]
• Character type: char matric[20]

x[10]

Name given to the The variable able


variable type to store 10 sets of
data in integer
Function of array: to store huge
data sets in a systematic way
HOW TO DECLARE ARRAY??
HOW TO DECLARE ARRAY?
Integer and character array
One dimensional array

• int x[10]
• Array is declared as ordinary variable, but size of the array
must be specified within the square bracket
Exercise
By using array function, develop a program to
store 5 different value in X[5] array, next calculate
the average of total obtained values.
Exercise

Write a program to store 5 digits below and perform


different arithmetic operation on these digits using single
array function:
4 5 11 12 9
First operation: 4/5
Second operation: 4+11/12
Third operation: 9%11
• #include<stdio.h>

• int main()
• {
• int mark[5] = {4,5,11,12,9};
• printf("first operation: %d \n",mark[0]/mark[1]);
• printf("second operation: %d\n",mark[0]+mark[2]/mark[3]);
• printf("third operation: %d \n",mark[4]%mark[2]);

• return 0;

• }
TRY THIS?
DEVELOP A PROGRAM TO SHOW STUDENT’S
TEST MARK WHEN THEIR MATRIC NO IS TYPED
USING ARRAY FUCNTION. THE STUDENT AND
THEIR MARKS ARE AS BELOW:

Array MATRIC NO TEST MARK


[0] 111 10
[1] 222 20
[2] 333 30
[3] 444 40
[4] 555 50
Subscript [i][j]

• Array also known as subscripted variable


• General form
• Name[i], number [i][j]
• i and j is subscript
• Following rules should be followed while using subscript
• A subscript may be a valid integer
• The value of subscript is zero or positive
• A subscript must not be subscripted variable
• If a variable is used to represent an array it should not be used as ordinary
variable
Retrieving stored data

int number[5]={1,2,3,4,5};
for (int i= 0; i< 5; i=i+1)
{ printf(“ the number %d is %d”, i, number[i]);}
Exercise

• Write a c program to store 1 into X[0],2 into X[1], 3 into


X[2], 4 into X[3] using subscript i in ‘for’ loop.
Example of one dimensional array with subscript
Run time initialization

• Array can be initialize during execution of the program in


2 different ways
• By using assignment statement
• By using input statement
Exercise

• Write a program to find y=∑𝑛𝑛𝑖𝑖=1 5𝑖𝑖2 using single array. n is


defined by user
Answer
Exercise

Using single dimensional array, solve ∑𝑛𝑛𝑖𝑖=0 𝑥𝑥𝑥𝑥 2 , x and n are


both user insert value.
Answer
Multidimensional array

• In multidimensional array the number of subscript is more than


1
• int a[i][j][k]
• The declaration is similar to one dimensional array.
• int x[3][2]
• x[0][0] x[0][1]
• x[1][0] x[1][1]
• x[2][0] x[2][1]
• The size of this array is 6
Exercise
Using 2-Dimensional array [3][3], display the
values of 1 until 9 when running the program.
*use ‘printf’ ONLY
Exercise: design a complete code
to show all character in 2-
dimensional array

1 2 3
• 𝑢𝑢 = 4 5 6
7 8 9
Applying SUBSCRIPT to retrieve/show all
data in multi dimensional array [i][j]:

• X[2][2]= {1,2,3,4}, i , j;
for(i=0;i<2;i++) apply ‘i’ int X[i][ ]
{for(j=0;j<2;j++)  apply ‘j’ into X[ ][j]
{X[i][j]=X[i][j];
printf(“X[%d][%d] = %d”, i,j,X[i][j]);}}
APPLYING SUBSCRIPT INTO MULTIDIMENSIONAL
ARRAY [i][j]:
Exercise
Using ‘for’ looping , write a program to create
[3][3] array, next shows all value in the created
array using subscript [i][j].
• #include<stdio.h>
• #include<conio.h>
• int main()
• { int x[2][2]={1,2,3,4}, i, j;
• printf("enter your x point: ");
• scanf("%d", &i);
• printf("enter your y point: ");
• scanf("%d", &j);
• printf("the coordinate x[]y[] is: %d", x[i][j]);
• return 0;
• }
Exercise

Given a multi-array x[2][2] without any preset


value. Next, develop a program store value
5,10,15,20 into the array index using for loop and
subscript x[i][j].
Transpose matrix

• The transpose of a matrix is a new matrix whose rows are


the columns of the original.
C code for transpose matrix

#include<stdio.h>
int main()
{
int x[3][3]={1,2,3,4,5,6,7,8,9}, XT[3][3];
int i,j,k;
for(i=0; i<3; ++i)
{for(j=2;j>-1;--j)
XT[i][j]=x[j][i]; }
Exercise: Design a 2-D char array
with preset character as below

𝑎𝑎 𝑏𝑏 𝑐𝑐
• u= 𝑑𝑑 𝑒𝑒 𝑓𝑓
𝑔𝑔 ℎ 𝑖𝑖
Example:
President of United State
Pointer

 When declaring a variable, the compiler sets aside


memory storage with a unique address to store that
variable.
 The compiler associates that address with the variable’s
name.
 When the program uses the variable name, it
automatically accesses a proper memory location.
 No need to concern which address.
 But we can manipulate the memory address by using
pointers.
pointer* vs normal variable

• Define: int *sum vs int sum


• Address:
• pointer: 1 fixed address + 1 dynamic address
• Normal variable: 1 fixed address
To call address:
Pointer : %p / %x
Normal variable: %p / %x
• A pointer is a variable that stores the address of
another variable. Unlike other variables that hold
values of a certain type, pointer holds the address
of a variable.

int *num int num

Store extra Dynamic add


address
Pointer outputs
Define & call pointer
Define pointer (no value): int *p, *sum;

To call variable address (&): %p + “Address of”(&)


e.g. printf(“%p”, &x)
To call pointer address (fixed add) : e.g. printf(“%p”, &p)
To call pointer dynamic add: printf(“%p”, &*p)

To store value into pointer from other variable:


int a=100, *b;
b = &a;
printf(“%d”, *b);
Difference address between %p and
%x
• "x" and "X" serve to output a hexadecimal number. "x"
stands for lower case letters (abcdef) while "X" for capital
letters (ABCDEF).
• "p" serves to output a pointer. It may differ depending upon
the compiler and platform.

Example:
Printf(“the address of ABC is: %p”, &abc);
Printf(“the address of ABC is: %x”, &abc);
Printf(“the address of ABC is: %X”, &abc);
Example- pointer
Example- pointer
Type of pointer (int, char, float)
Arithmetic on pointer variable

• Arithmetic operation such as +,-,++ and – can be performed on


pointer variable under some restriction
• Depends on size of data type
• Character – 1byte
• int- 4 bytes
• float- 4 bytes
• double- 8 bytes
• if float *a and a++ the size increase by 4
Pointer arithmetic (++, -- , + -)
Decrement pointer
Pointer and One dimensional Array

• Address of the first element of array is address of entire


array
• a[ ]  a is an integer of array, then the address of first
element either &a[0] or by simply a.
• If Pa is pointer define by int *Pa and array a defined by int
a[ ];
• E.g. a[3] = {1,2,3}, Pa=&a[2]; or a[2] = Pa=3
Array of pointer[ ] – store array
data in pointer
use an array of pointers to
character to store a list of strings
What is the output?
Pointer to pointer (to store pointer
dynamic address)

• A pointer to a pointer is a form of multiple indirection, or a


chain of pointers. (to store point value in other pointer)

• declares a pointer to a pointer of type int :


Example: int *num, **num1, ***num2, ****num3;
Example of pointer to pointer:
Thank you.

You might also like