CH 7 Array
CH 7 Array
Sujan Karki
Email: [email protected]
Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
Bachelor in Computer Engineering – Purwanchal Campus,IOE
UNIT 6
ARRAY
2
Array
⚫ Array is the collection of similar data types that can be
represent by a single variable name.
⚫ The individual data items in an array are called
elements.
⚫ The element of array share same variable name but
each element has a different index of number known as
subscript.
⚫ Example: int marks[5] = {68, 85, 95, 63, 81};
3
Types of array
⚫ One dimensional array
1. Declaration of one dimensional array
Syntax:
data_type array_name [size];
Here,
data_type is the data type of array. It may be int,
float, char etc.
array_name is the name of array used to store data.
The size of array specifies the number of elements that
can be stored in the array. The size of the array should
be always positive integer.
e.g. int age[10];
4
float salary[20];
…
Initialization of one dimensional array
Syntax:
data_type array_name[size]={value1,value2,....valueN};
Here,
data_type is the data type of array. It may be int, float,
char etc.
array_name is the name of array used to store data.
value1, value2,.......valueN are the constant values which are
assigned to the array elements one after another.
e.g. int marks[5]={55,59,78,87,65};
float salary[ ]={55.22, 66.32, 96.21};
int c[10]= {45, 44,98,89,90}
5
…
Accessing of one dimensional array elements
The subscript or index which is the integer in brackets
followed by the array name.
Example.
int marks[5]={55,59,78,87,65};
The values of the array elements after this initialization
are:
marks[0]=55,
marks[1]=59,
marks[2]=78,
marks[3]=87,
marks[4]=65
6
⚫ Example: WAP to read 10 integers form keyboard and store
in an array and display them.
#include<stdio.h>
int main() {
int arr[10],i;
for(i=0;i<10;i++)
{
printf("Enter elements of array\n");
scanf("%d", &arr[i]);
}
printf("Displaying array elements\n");
for(i=0;i<10;i++)
{
printf("%d\n", arr[i]);
}return 0;
7 }
Types of array
⚫ Multidimensional array
1. Declaration of multi dimensional array
Syntax:
data_type array_name [firstsubscript][secondsubscript];
8
…
Initialization of multi dimensional array:
int a[2][3]={3,5,8,9,4,6};
Which means,
a[0][0]=3, a[0][1]=5, a[0][2]=8, a[1][0]=9, a[1][1]=4,
a[1][2]=6.
int marks[][3]={3,5,8,9,4,6};
9
…
Initialization of multi dimensional array:
int marks[][3]={3,5,8,9,4};
marks[0][0]=3, marks[0][1]=5, marks[0][2]=8,
marks[1][0]=9, marks[1][1]=4, marks[1][2]=0.
1
0
⚫ Example: Write a program to read any 2×2 matrix and
display it in appropriate format..
#include<stdio.h>
int main() {
int a[2][2], i, j;
for( i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter elements of matrix\n");
scanf("%d", &a[i][j]);
}
}
11
for( i=0;i<2;i++)
Passing arrays to function
It is possible to pass the value of an array element and
even an entire array as an argument to a function.
To pass an entire array to a function , the array name
must appear itself without brackets or subscripts as an
actual arguments in a function.
#include<stdio.h>
void func(int arr[]);
int main()
{
int arr[10], i;
printf("Enter 10 array elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
func(arr);
return 0;
}
13
Strings
String is an array of characters or alphabets.
Some important facts about string:
(i) A finite set of zero or more characters is called – String.
(ii) Length: The number of characters including space is
called size or length.
Example:" Hello Word!" – Its length is 11 including blank
spaces.
(iii) A string constant is denoted by a characters included in
double quotes.
(iv) The string array contains NULL character („\0‟) to
insure own existence to compiler.
E.g. “BIKAS”
“Microsoft Products”
14 " 521457 "
Initializing Strings
A string is initialized as:
char name[]={‘R’ , ‘A’ , ‘M’ , ‘\0’ };
OR
char name[]=“RAM”;
Array of strings:
char string_name [size1] [size2];
15
String Library Functions: strlen ( )
This function returns the length of the string i.e. the number
of characters in the string excluding the terminating
null character.
#include<stdio.h>
Syntax:
#include<string.h> Integer_variable= strlen(string);
void main( ) {
char str[20]; int length;
printf(“Enter the string:\n”);
scanf(“%s”, str);
length = strlen(str);
printf(“Length of the string is : %d\n”, length);
}
Output:
Enter the string : programming
1
6 Length of the string is : 11
String Library Functions: strcmp( )
This function is used for comparison of two strings. If the
two strings match, strcmp( ) returns a value 0,
otherwise it returns a non-zero value. This function
compares the string character by character. The
comparison stops when either the end of string is reached or
the corresponding characters in the two
strings are not same. The non zero value returned on
mismatch is the difference of the ASCII values of the
non matching characters of the two strings-
strcmp (s1, s2) returns a value-
<0 when s1 < s2
= 0 when s1 == s2
> 0 when s1 > s2
1
7
⚫ Example:
#include<stdio.h>
#include<string.h>
void main( )
{
char str1[10], str2[10];
printf(“Enter the first string:”);
scanf(“%s”, str1);
printf(“Enter the second string:”);
scanf(“%s”, str2);
if((strcmp(str1, str2)) == 0)
printf(“Strings are same\n”);
else
printf(“Strings are not same\n”);
}
18
String Library Functions: strcpy( )
This function is used for copying one string to another
string.
Syntax:
strcpy(srt1, str2) ;
It copies str2 to str1. Here str2 is the source string and str1
is destination string.
If str2 = “programming” then this function copies
“programming” into
str1.
1
9
⚫ Example:
#include<stdio.h>
#include<string.h>
void main( ) {
char str1[10], str2[10];
printf(“Enter the first string:”);
scanf(“%s”, str1);
printf(“Enter the second string:”); scanf(“%s”, str2);
strcpy(str1, str2);
printf(“First string : %s \n
Second string: %s\n”, str1, str2);
strcpy(str1, “Jomsom”)
strcpy(str2, “Kagbeni”)
printf(“First string : %s \n Second string: %s\n”, str1, str2);
}
Output:
20
String Library Functions: strcat( )
This function is used for concatenation of two strings. If first
string is “King” and second string is “size” then after
using this function the first string becomes “Kingsize”.
The null character from the first string is removed, and the
second string is added at the end of first string. The
second string remains unaffected.
Syntax:
strcat(str1, str2);
2
1
⚫ Example:
#include<stdio.h>
#include<string.h>
void main( ) {
char str1[20], str2[20];
printf(“Enter the first string:”);
scanf(“%s”, str1);
printf(“Enter the second string:”);
scanf(“%s”, str2);
strcat(str1, str2);
printf(“First string:%s \t Second string: %s\n”, str1, str2);
strcat(str1, “_one”);
printf(“Now first string is : %s\n”, str1);
}
Output:
22
Enter the first string : data
String Library Functions: strrev( )
This function takes string as an argument and returns the
string in reverse order.
Syntax:
strrev(string);
Example
#include<stdio.h>
#include<string.h>
void main() {
char str[10];
printf("Enter a string\n");
scanf("%s", str);
printf("%s", strrev(str));
}
2
3
String Library Functions: strupr( )
This function converts lowercase string to uppercase string. If
any of the character, already in uppercase then the function
skips that character and converts the remaining character
which is in lowercase.
Example
Syntax:
#include<stdio.h> strupr(string);
#include<string.h>
void main() {
char str[10];
printf("Enter a string\n");
scanf("%s", str);
printf("%s", strupr(str));
}
24
String Library Functions: strlwr( )
This function converts uppercase string to lowercase string. If
any of the character, already in lowercase
then the function skips that character and converts the other
remaining character which is in uppercase.
Example
Syntax:
#include<stdio.h> strlwr(string);
#include<string.h>
void main() {
char str[10];
printf("Enter a string\n");
scanf("%s", str);
printf("%s", strlwr(str));
}
25
. . . to be continued !!!
26