Faculty of Engineering and Technology
Parul Institute of Technology
Department: Computer Science & Engineering
Course: B.Tech – CSE
Subject: Computational Thinking for Structured Design-1
Subject Code: 303105104
UNIT 5
PART 1
Arrays
Outline
• Introduction
• Types of Array
• Initialization of Array
• Array of Pointers
• Array of Functions
Introduction:
• An array is a collection of items of same data type stored at
contiguous memory locations.
• Arrays work on an index system starting from 0 to (n-1), where n is
the size of the array.
Why Array is important?
• Let's suppose a class consists of ten students, and the class has to
publish their results. If you had declared all ten variables individually,
it would be challenging to manipulate and maintain the data.
• If more students were to join, it would become more difficult to
declare all the variables and keep track of it. To overcome this
problem, arrays came into the picture.
Types of Array:
• There are majorly three types of arrays:
1. One-dimensional array (1D array)
2. Two-dimensional (2D array)
3. Three-dimensional array (3D array)
One-dimensional Array:
• Syntax for declaration of one dimensional array:
data_type array_name[array_size];
data_type: a type of data of each array block.
array_name: the name of the array using which we can refer to it.
array_size: the number of blocks of memory array going to have.
int num[5];
Two-dimensional Array:
• Syntax for declaration of two dimensional array:
data_type array_name [size_of_first_dimension] [size_of_second_dimension];
data_type: is a type of data of each array block.
array_name: is the name of the array using which we can refer to it.
sizeof_dimension: is the number of blocks of memory array going to have in the
corresponding dimension.
int num[2][2];
Three-dimensional Array:
• Syntax for declaration of Three dimensional array:
data_type array_name [size_of_first_dimension] [size_of_second_dimension] [size_of_third_dimension];
data_type: is a type of data of each array block.
array_name: is the name of the array using which we can refer to it.
sizeof_dimension: is the number of blocks of memory array going to have in the
corresponding dimension.
int arr[2][row][col];
Initialization of Array:
• Valid Statements:
int mark[5] = {19, 10, 8, 17, 9};
int mark[ ] = {19, 10, 8, 17, 9};
Program : 1 : To print Array in C
#include <stdio.h>
int main() :OUTPUT:
{ a[0] is 1
a[1] is 2
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; a[2] is 3
int i; a[3] is 4
a[4] is 5
for(i = 0; i < 10; i++) a[5] is 6
{ a[6] is 7
a[7] is 8
printf(“a[%d] is %d\n", i , a[i]); a[8] is 9
} a[9] is 0
return 0;
}
Program : 2 : To print Array in C
#include <stdio.h>
int main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8};
int i;
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 0 0
Program : 3 : To print Array in C
#include <stdio.h>
int main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int i;
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
Array of Pointers:
• Syntax:
pointer_type *array_name [array_size];
#include <stdio.h>
int main()
{
int v1 = 10, v2 = 20, v3 = 30; Output:
int* p_a[3] = { &v1, &v2, &v3 }; Value of v1: 10 Address: 0x7fff1ac82484
for (int i = 0; i < 3; i++) Value of v2: 20 Address: 0x7fff1ac82488
{ Value of v3: 30 Address: 0x7fff1ac8248c
printf("Value of v%d: %d\tAddress: %p\n", i + 1,*p_a[i], p_a[i]);
}
return 0;
}
Array of Functions:
UNIT 5
PART 2
String
Outline
• Reading and printing a string
• String handling functions
Reading and printing a string:
int main() Output:
{ Enter String: abc
char s1[50]; You have entered: abc
printf(“Enter String:”);
scanf(“%s”,&s1);
printf(“You have entered:%s”,s);
return 0;
}
String handling functions:
String Handling
Functions
strlen strcmp strcpy strcat