ARRAYS IN C LANGUAGE
Course Title: Programming language 1
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 08 Week No: 9 Semester: Spring 23-24
Lecturer: Md. Faruk Abdullah Al Sohan; faruk.sohan@aiub.edu
Lecture Outline
List of topics,
The concept of array
Defining arrays
Types
The concept of ARRAY
C Array is a collection of variables belongings to the same data
type. You can store group of data of same data type in an
array.
Array might be belonging to any of the data types, e.g.,
char, int, float..
Array size must be a constant value.
Always, adjacent memory locations are used to store array
elements in memory.
It is a best practice to initialize an array to zero or null
while declaring.
Declaring an array
Declaring an array :
Declaring the type of elements that will be contained in the array—such as int, float,
char, etc.
Declaring the maximum number of elements that will be stored inside the array.
The compiler needs this information to determine how much memory space to reserve for
the array.)
This must be a constant integer value
The range for valid index values:
First element is at index 0
Last element is at index [size-1]
It is the task of the programmer to make sure that array elements are referred by
indexes that are in the valid range ! The compiler cannot verify this, and it comes to
severe runtime errors !
ARRAY Example
int values[10];
Declares an array of 10 elements of type int
int values[10];
Valid indexes:
values[0]=5;
values[9]=7;
Invalid indexes:
values[10]=3;
values[-1]=6;
In memory: elements of an array are stored
at consecutive locations
ARRAY Types
Types of array
There are 2 types of arrays. They are
• One dimensional array
• Multi-dimensional array
Two-dimensional array
Three-dimensional array etc….
ONE DIMENTIONAL ARRAY
Array declaration, initialization and accessing Example
Integer array example:
Array declaration syntax: int age [5];
data_type arr_name [arr_size]; int year[5]={2020, 2021, 2022, 2023};
Array initialization syntax: year[0]; /*2020 is accessed*/
data_type arr_name [arr_size]=(value1, value2, year[1]; /*2021 is
value3,….); accessed*/ year[2]; /*2022
is accessed*/
Array accessing syntax: Character array example:
arr_name[index];
char str[10];
str[10]={‘H’,‘a’,‘i’};
(or)
str[0] = ‘H’;
str[1] = ‘a’;
str[2] = ‘i;
str[0]; /*H is accessed*/
str[1]; /*a is accessed*/
str[2]; /*i is accessed*/
ONE DIMENTIONAL ARRAY
#include<stdio.h>
int main()
{ Memory location
int i; 0 1 2 3
int arr[5] = {10,20,30,40,50}; ar 4
r 10 20 30 40 50
/*To initialize all array elements to 0, use
int arr[5]={0};
Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40; arr[4] = 50; */ Output:
value of arr[0] is 10
for (i=0;i<5;i++) value of arr[1] is 20
{ value of arr[2] is 30
// Accessing each variable value of arr[3] is 40
printf("value of arr[%d] is %d \n", i, arr[i]); value of arr[4] is 50
}
}
TWO DIMENTIONAL ARRAY
Array declaration, initialization and accessing Example
Array declaration syntax: data_type Integer array example:
arr_name [num_of_rows][num_of_column];
int arr[2][2];
Array initialization syntax: int arr[2][2] = {1,2, 3, 4};
data_type arr_name[2][2] =
{{0,0},{0,1},{1,0},{1,1}}; arr [0] [0] = 1;
arr [0] [1] = 2;
Array accessing syntax: arr [1][0] = 3;
arr_name[index]; arr [1] [1] = 4;
TWO DIMENTIONAL ARRAY
#include<stdio.h>
int main()
{
int row,col; Memory
// declaring and Initializing array ar location
int arr[3][3] = {10,20,30,50,80,30,20,12,17}; r 0
0 10 20 1 30
/* Above array can be initialized as below also 2
1 50 80 30
arr[0][0] = 10; // Initializing array
arr[0][1] = 20; 2 20 12 17
arr[0][2] = 30;
arr[1][0] = 50;
Output:
arr[1][1] = 80;
arr[1][2] = 30;
arr[0][0] = 10
arr[2][0] = 20; arr[0][1] = 20
arr[2][1] = 12; arr[0][2] = 30
arr[2][2] = 17; */ arr[1][0] = 50
for (row=0;row<3;row++) { arr[1][1] = 80
for (col=0;col<3;col++) { arr[1][2] = 30
printf("arr[%d] [%d] = %d\n",row,col,arr[row][col]); arr[2][0] = 20
}
}
arr[2][1] = 12
} arr[2][2] = 17
Class Work:
1. Develop a program that stores your Name and ID using two
different arrays and displays your information at the end.
2. Develop a program that can store 4 integer numbers by
asking the user for inputs in arrays. The program estimates
the summation, average as well as multiplication of the
stored numbers and prints all the results.
3. Develop a program that takes five student’s CGPAs as inputs
using a single array and finds the lowest CGPA.