0% found this document useful (0 votes)
6 views

Arrays

Uploaded by

Cind Rella
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)
6 views

Arrays

Uploaded by

Cind Rella
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/ 26

SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


UNIVERSITY
Beyond Boundaries

Arrays

Presented By:

Amit
1
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Example
• Suppose we want to arrange percentage marks
obtained by 100 students.
– Construct 100 variables to store percentage marks
obtained by 100 different students
– Construct one variable (called array) capable of
storing all hundred values.

int Marks={98, 78, 89,……..}

2
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Arrays - Introduction
 Array is a list of finite number n of homogeneous data
elements such that:

• The elements of the array are referenced respectively by


an index set of n consecutive numbers.

• The element of the array are stored respectively in


successive memory location.

• The number n of elements is called the length or size.


SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Arrays: basic idea


• An array is a collection of similar data elements
• The first element of array is numbered as 0, so the
last element is 1 less than the size of the array
• An array is also known as subscripted variable
• Before using an array, its type and dimension
must be declared
• Elements of a array are always stored in
contiguous memory locations.
4
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Graphical Representation
• Position is called a index or superscript. Base index = 0.
• Let DATA be a 6-element array of integers such that
DATA[0]=247, DATA[1]=56, DATA[2]=492,
DATA

0 247
DATA[3]=135, DATA[4]=87, DATA[5]=156
1 56

INDEX 2 492

3 135

4 87

5 156
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Graphical Representation
• Position is called a index or superscript. Base index = 0.
• Let DATA be a 6-element array of integers such that
DATA[0]=247, DATA[1]=56, DATA[2]=492,
DATA

1 247
DATA[3]=135, DATA[4]=87, DATA[5]=156
2 56

Position 3 492

4 135

5 87

6 156
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Graphical Representation
• Position is called a index or superscript. Base index = 0.
• Let DATA be a 6-element array of integers such that
DATA[0]=247, DATA[1]=56, DATA[2]=492,
DATA

0 247
DATA[3]=135, DATA[4]=87, DATA[5]=156
1 56

VALUE 2 492

3 135

4 87

5 156
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Declaration of Arrays
1. Like any other variables, arrays must declared and created before they
can be used. Creation of arrays involve two steps:
1. Declare the array
2. Put values into the array (i.e., Memory location)

2. Declaration of Arrays:

Type arrayname[size];

3. Examples:
float marks[30];
char name[25];
int students[7];

Note: we have to specify the size of arrays in the declaration.


SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Initialization of Arrays
1. Once arrays are created, they need to be initialised with some values
before access their content. A general form of initialisation is:
Arrayname [index/subscript] = value;
2. Example:
students[0] = 50;
students
students[1] = 40;
0 50
3. Array index starts with 0 and ends with n-
1
4. 1
Trying to access an array beyond its 2
boundaries will generate an error message. 3
students[7] = 100; 4
5
6
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Initialization of Arrays
1. Once arrays are created, they need to be initialised with some values
before access their content. A general form of initialisation is:
Arrayname [index/subscript] = value;
2. Example:
students[0] = 50;
students
students[1] = 40;
0 50
3. Array index starts with 0 and ends with n-1
1 40
4. Trying to access an array beyond its
2
boundaries will generate an unexpected result.
3
students[7] = 100;
4
5
6
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Explicit initialization
1. Arrays can also be initialised like standard variables at the time of their
declaration.

Type arrayname[] = {list of values};

2. Example: students
0 55
int students[] = {55, 69, 70, 30, 80, 90, 45};
1 69
3. Creates and initializes the array of integers of length 7. 2 70
4. In this case it is not necessary to mention the dimension. 3 30
4 80
5 90
6 45
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Accessing Array Elements


1. Access individual elements of an array using:

1. the name (handle) of the array

2. a number (index or subscript) that tells which of the element of the


array. It may be any constant or variable.

2. What value is stored in count[2]?

count[2] = 9
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Processing Array Elements using loop


1. Access individual elements of an array:

printf(“%d”, count[0]);

printf(“%d”, count[1]); for( i=0; i<=3; i++)

printf(“%d”, count[2]); printf(“%d”, count[i]);

printf(“%d”, count[3]);
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Entering Data into an Array


1. Enter individual elements of an array:

Printf(“Enter the elements of array”);

scanf(“%d”, &count[0]);

scanf(“%d”, &count[1]);

scanf(“%d”, &count[2]); count


0
scanf(“%d”, &count[3]);
1
2

Printf(“Enter the elements of array”); 3

for( i=0; i<=3; i++)

scanf(“%d”, &count[i]);
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Entering Data into an Array


printf(“Enter marks 1\n”);
scanf(“%d”, &count[0]);
printf(“Enter marks 2\n”);
scanf(“%d”, &count[1]);
printf(“Enter marks 3\n”);
scanf(“%d”, &count[2]);
......
......
printf(“Enter marks 10\
n”);
scanf(“%d”, &count[9]);
for( i=0; i<10; i++)
{
printf(“enter Marks %d\n”, (i+1));
scanf(“%d”, &count[i]);
}
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Array elements in memory:

• Consider the following declaration:


int aa[12];
• Elements of array are always stored in
contiguous memory locations
11 19 22 42 61 44 32 19 14 62 45 92
10246 10248 10252 10242 10256 10258
10242 10244 10250 10254 10260 10262
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Array Operation Example


Write a program to find average marks obtained by a
class of 30 students in a test
void main()
{
int avg, sum=0;
int i;
int marks[30]; //array declaration
for(i=0;i<=29;i++)
{
printf(“\nEnter marks:”);
scanf(“%d”,&marks[i]); //storing data in array
}
for(i=0;i<=29;i++)
sum=sum+marks[i]; //read data from array
avg=sum/30;
printf(“\n Average marks=%d”, avg);
}
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Array Operation Example


Write a program to search an element into array and if
the element is present then print position of element.
1
Start
YES

Array (A) of IF : IF:


I = I+1
defined size A[I] No I<=N-1
N == K
No
YES
Element to “Not
Set:
search (K) Position = K+1 Present”

Print
Set: I=0 Positio
n
End
1
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Two Dimensional Arrays


• Two dimensional arrays allows us to store data that are
recorded in table.
int student[4][4];
4 arrays each having 4 elements
First index: specifies array (row)
Second Index: specifies element in that array (column)
0 1 2 3
0 10 20 85 45

1 56 58 25 58

2 45 85 65 78

3 65 14 28 56
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Two Dimensional Arrays


• Requires two indices
int student[4][4];
• What is the value of student[2][3]?
Student[2][3] = 78

student
0 1 2 3
0 10 20 85 45

1 56 58 25 58

2 45 85 65 78

3 65 14 28 56
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Declaration and Initialization of 2D Array


 Declaring and initializing two-dimensional are
arrays
accomplished by extending the processes used for one-
dimensional arrays:
• Declaration:
int myArray [rows][columns];
• Initialisation:
- Single Value; It is necessary to mention the
second dimension, whereas
myArray[0][0] = 10;
first dimention is optional
- Multiple values:
int table[2][3] = {{10, 15, 30}, {14, 30, 33}};
OR
int table[2][3] = {10, 15, 30, 14, 30, 33};
OR
int table[][3] = {10, 15, 30, 14, 30, 33};
What about this?
int table[][] = {10, 15, 30, 14, 30, 33};
NO
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Entering Data into 2-D Array


1. Enter individual elements of an array:
Printf(“Enter the elements of array”);
scanf(“%d”, &count[0][0]);
scanf(“%d”, &count[0][1]);
scanf(“%d”, &count[1][0]); count
scanf(“%d”, &count[1][1]);
0 1
scanf(“%d”, &count[2][0]);
0
scanf(“%d”, &count[2][1]);
1
2
printf(“Enter the elements of array”);
for( i=0; i<=2; i++)
for(j=0; j<=1; j++)
scanf(“%d”, &count[i][j]);
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Processing Data from 2-D Array


printf(“Enter the elements of array”);
for( i=0; i<=2; i++)
for(j=0; j<=1; j++)
count[i][j] = (i+j);

for( i=0; i<=2; i++)


{
for(j=0; j<=1; j++)
{
printf(“%d\t”, count[i][j]);
}
printf(“\n”);
}
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries

Problem-1
• Write a program to add the elements of two
matrices and save the result in third matrix.
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
D E PAR TME N T OF C O M P U T ER SC I E N C E A N D E N G I N E E R I NG
UNIVERSITY
Beyond Boundaries

void main() for(i=0;i<=2;i++)


{ {
int i, j, a[3][3], b[3][3], c[3][3]; for(j=0;j<=2;j++)
printf(“\nenter the elements of {
first matrix row wise:”); c[i][j]=a[i][j]+b[i]
for(i=0;i<=2;i++) [j];
{ }
for(j=0;j<=2;j++) }
{ printf(“\nThe resultant matrix
scanf(“%d”,&a[i][j]); is:\n”);
} for(i=0;i<=2;i++)

} {
printf(“\nEnter the elements of II for(j=0;j<=2;j++)
matrix:”); {
for(i=0;i<=2;i++) printf(“\t%d”,c[i][j]);
{ }
for(j=0;j<=2;j++) printf(“\n”);
{ }
scanf(“%d”,&b[i][j]); }
}
}
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
D E PAR TME N T OF C O MPU T ER SC I E N CE AND E N G I N E E RI N G
UNIVERSITY
Beyond Boundaries

Thank You!!

You might also like