0% found this document useful (0 votes)
5 views32 pages

Arrays

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

Arrays

Course Title: Programming Fundamentals


1
Course Code: CSE 1113
Course Instructor: Adiba Ibnat Hossain
2 Array
An Array is a fixed-sized sequenced collection of elements of the

same data type.

Arrays are used to store multiple values in a single variable, instead

of declaring separate variables for each value.

The elements share a common name.


3
Properties of Array

Size of an Array is fixed.

Elements of same data type can be stored in an array.

Data resides in contiguous memory locations.


4 Types of Arrays in C

One Dimensional Array


datatype arrayname [arraysize];
int data [5];

Multidimensional Array
For example: two dimensional array
datatype arrayname [row][col];
int data [2][2];
5 Declaring an One Dimensional Array
number[0] = 20

number[1] = 14
number[5] 20 14 32 88 45
number[2] = 32

number[3] = 88

number[4] = 45

To access an array element, refer to its index number.

In the example, data[0] is the first element.

In this example, data[4] is the last element.


6 Declaring an One Dimensional Array(Contd.)
Note:
Arrays have 0 as the first index, not 1. In the example, data[0] is the
first element.

If the size of an array is n, to access the last element, the n-1 index is
used. In this example, data[4]
7 Initializing One Dimensional Arrays

datatype arrayname [arraysize] = {list of values};


integer array of 5 elements
int data [5] = { 1, 2, 3, 4, 5 };
float array of 4 elements
float array [4] = { 3.5, 5.5, 6.5, 7.5 };
character array of 7 elements
char name [7] = { ‘s’, ‘t’, ‘u’, ‘d’, ‘e’, ‘n’, ‘t’ };
8 Store and Print Values from an Array

Write a program to store and print values from an array.


Output
9 Store and Print Values from an Array (Contd.)
10
Compute the sum of the given numbers

You are given 10 integers such as, 10, 12, 8, 34, 3, 7, 22, 17, 23, 44.
Compute the sum of the given numbers using appropriate data
structure and write the program.
Output
11 Compute the sum of the given numbers (Contd.)
12 Finding Max-Min Values in an Array
Write a C program to print the maximum and minimum values in an
array.
Output
13 Finding Max-Min Values in an Array
14 Search Element in an Array
Write a C program to search element in an array.
Output
15 Search Element in an Array
16 Search Element in an Array
17 Multidimensional Array
A multi-dimensional array can be termed as an array of arrays that
stores homogeneous data in tabular form.
General form of declaring N-dimensional arrays is:
data_type array_name[size1][size2]....[sizeN];
Example: Two dimensional array: int two_d[10][20];
Three dimensional array: int three_d[10][20][30];
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000
elements.
18 Multidimensional Array (Contd.)

A two – dimensional array ‘x’ with 3 rows and 3 columns is


shown below:
19

Memory Layout of 2D array


20 Initializing Two – Dimensional Arrays:
• First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

• Second Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

• Third Method:

int x[3][4];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
cin >> x[i][j];
}
}
21 Storing elements in a matrix and printing it.
Write a program to store elements in a matrix and print it.
Output:
Storing elements in a matrix and printing it.
22 (Contd.)
23 Pattern Printing Problem

Write a C program to print the following pattern in C.


24 Pattern Printing Problem(Contd.)
Source code and sample output
25 Pattern Printing Task

Write a C program to print the following pattern in C.

(a) (b) (c) (d)


26 C program to check whether two matrices
are equal or not
Example
Input
Input elements of matrix1:
123
456
789

Input elements of matrix2:


123
456
789
Output
Both matrices are equal
C program to check whether two matrices
27
are equal or not (Contd.)
28
29 C program to check whether two matrices
are equal or not (Contd.)
30 Three-dimensional array
Write a C program to take a 3D matrix as input and show the matrix in the output.
31 Three-dimensional array (Contd.)
32

You might also like