0% found this document useful (0 votes)
56 views16 pages

1D, 2D Arrays

The document explains arrays as a fixed-size collection of elements of the same data type, highlighting their advantages and disadvantages. It discusses how arrays can efficiently represent multiple data items and implement other data structures, while also noting the limitations of fixed size and difficulty in insertions and deletions. Examples of one-dimensional and two-dimensional arrays are provided, along with a program demonstrating their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views16 pages

1D, 2D Arrays

The document explains arrays as a fixed-size collection of elements of the same data type, highlighting their advantages and disadvantages. It discusses how arrays can efficiently represent multiple data items and implement other data structures, while also noting the limitations of fixed size and difficulty in insertions and deletions. Examples of one-dimensional and two-dimensional arrays are provided, along with a program demonstrating their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction

Why arrays?

The fundamental data types namely char, int, float are constrained by the fact
that a variable of these types can store only one value at any given time. In order
to handle a large volume of data in terms of reading, processing and printing we
need a derived data type known as array.

Def:

An array is a fixed-size sequenced collection of elements of the same data type.

Examples where the concept of an array can be used:


• List of temperatures recorded every hour in a day, or a month, or a year.
• List of employees in an organization.
Memory Representation of Array
Introduction…..

• List of employees in an organization.

• List of products and their cost sold by a store.

• Test scores of a class of students.

For instance, we can use an array name salary to represent a set of salaries of a
group of employees in an organization. We can refer to the individual salaries by
writing a number called index or subscript in brackets after the array name.

e.g. salary[10]

represents the salary of 10th employee.


Introduction…..

Advantages and Disadvantages of Array

Advantages
• It is used to represent multiple data items of same type by using only single
name.

• It can be used to implement other data structures like linked list, stacks,
queues, trees, graphs, etc.

• 2D arrays are used to represent matrices.

• The elements of arrays are stored in consecutive memory locations so


searching an element is easy.
Introduction…..

Disadvantages
• We must know in advance that how many elements are to be stored in array.

• Array is static structure. It means that array is of fixed size. The memory which
is allocated to array can not be increased or decreased.
• Since array is of fixed size, if we allocate more memory than requirement then
the memory space will be wasted. And if we allocate less memory than
requirement, then it can create problem.

• As the elements are stored in consecutive memory locations so insertions and


deletions are very difficult and time consuming.
Introduction…..

The ability to use a single name to represent a collection of items and to refer to
an item by specifying the item number enables us to develop concise and
efficient programs.

We can use arrays to represent not only simple lists of values but also tables of
data in two, three or more dimensions. We have the following types of arrays:
• One-dimensional array

• Two- dimensional array

• Multi- dimensional array


Example…..
// Program to take 5 values from the user and store them
in an array

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Example…..
Bounds Checking

Data entered with a subscript exceeding the array


size will simply be placed in memory outside the
array; probably on top of other data, or on the
program itself.
Out of Bound
MCQ - 1

1. Which of these best describes an array?

A. A data structure that shows a hierarchical behaviour

B. Container of objects of similar types

C. Arrays are immutable once initialized

D. Array is not a data structure

Answer: B.
Two - Dimensional Array

int x[3][3];
2D Array
Two-Dimensional Array
#include<stdio.h>
void main(){
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
}
MCQ - 2

2. What are the advantages of arrays?

A. Objects of mixed data types can be stored

B. Elements in an array cannot be sorted

C. Index of first element of an array is 1

D. Easier to store elements of same data type

Answer: D.

You might also like