Arrays
Arrays
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.
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:
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];
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.
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
count[2] = 9
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries
printf(“%d”, count[0]);
printf(“%d”, count[3]);
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries
scanf(“%d”, &count[0]);
scanf(“%d”, &count[1]);
scanf(“%d”, &count[i]);
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries
Print
Set: I=0 Positio
n
End
1
SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
UNIVERSITY
Beyond Boundaries
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
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
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
} {
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!!