Prepared by: Er. Rupesh Shrestha BEL I/I, NPI Rupeshshrestha64@gmai.
com
8. Array and Strings
Types of arrays:
1. One-dimensional Array (1D Array)
20 28 35 44 53 61
Nepal Polytechnic Institute
3.4 3.52 2.0 1.99 4.0 3.0
Declaration of 1D Array
Syntax:
data_type array_name[size];
Eg:
int roll[48];
memory requirements: 4*48 = 192 Bytes
char name[30]; //1*30 =30B
float salary[5]; //4*5= 20B
Initialization of an 1D Array
Compile-time Initialization
Initialize array at declaration.
eg. float salary[5] = {30000.0, 37000.75, 43500.0, 51250.5, 60000};
salary[0] = 30000.0 salary[1] = 37000.75 salary[2] = 43500.0
salary[3] = 51250.5 salary[4] = 60000
int roll[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char country[] = “Nepal”;
char country[] = {‘N’, ‘e’, ‘p’, ‘a’, ‘l’, ‘\0’};
Run-time Initialization
Initialize array at run time from user.
float salary[5];
int i;
…
for(i = 0; i < 5; i++){
scanf(“ %f”, &salary[i]);
}
...
Accessing 1D Array
use array elements to display, sort, add elements….
1
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI Rupeshshrestha64@gmai.com
Examples:
1. Minimum and Maximum among the given elements
2. Write a program to display the third largest number in an array of size N.
3. write a program to input N numbers of integer type in an array and find the sum of square of the
largest number and cube of the smallest number present in the given array.
4. Write a program to read the age of 400 persons and count the number of persons in the age group 70
to 75. Use for and continue statements.
2. Multi-dimensional Array (ND Array)
Declaration of 2D array
syntax:
data_type array_name [row_size][column_size];
examples:
float marks[3][5]; // 3*5 = 15 elements
int matrix[4][3];
char college_name[3][50];
Initialization of 2D Array
I. compile-time
float marks[3][5] = {{50,35,45,49,42}, {49,47,46,48,43}, {49,46,48,47,45}};
marks[0][0] = 50, marks[0][1] = 35, marks[0][2] = 45, … marks[0][4] = 42
marks[1][0] = 49, marks[1][1] = 47, marks[1][2] = 46, … marks[1][4] = 43
marks[2][0] = 49, marks[2][1] = 46, marks[2][2] = 48, … marks[2][4] = 45
ii. Run-time
float marks[3][5];
int i, j;
for (i = 0; i < 3; i++){//row
for(j = 0; j < 5; j++){//column
scanf(“ %f”, &marks[i][j]);
}
}
Accessing of 2D Array
Use nested loop to access elements of 2D array.
Examples:
1. Write a program to multiply given two matrices A and B of order MxN and PxQ.
2. WAP to read a 3x3 matrix. Then multiply each odd number of the matrix by 3 and display the
resultant matrix.
3. Write a program to input p by p matrix and print sum of squares of diagonal elements of matrix.
4. Transpose of matrix
5. Sum of two matrices
2
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI Rupeshshrestha64@gmai.com
6. Sum of all elements
7. Sum of row elements
String Handling
Array of characters
Declaration of 1D string:
syntax:
char string_name[char_size];
Example:
char name[30];
char address[50];
char phone[10];
char college[20];
Declaration of 2D string:
syntax:
char string_name[no_of_string][char_size];
Example:
char name[20][30]; //20 names
char address[100][50]; //100 addresses
String Handling Functions
All string handling functions are defined in string.h header file.
a. strlen()
syntax:
length = strlen(string);
It finds then returns the length of given string (variable/constant). Here length is integer variable.
Example:
char college[] = “Nepal Polytechnic Institute”;
int len1, len2;
len1 = strlen(college); //len1 = 27
len2 = strlen(“Purwanchal University”); //len2 = 21
//printf(“Length = %d”, strlen(college));
b. strlwr()
syntax:
strlwr(string);
It finds the lowercase equivalent of given string.
Example:
char name[] = “Narayan”;
strlwr(name);
The name contains narayan.
c. strupr()
syntax:
3
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI Rupeshshrestha64@gmai.com
strupr(string);
It finds the uppercase equivalent of given string.
Example:
char name[] = “Narayan”;
strupr(name);
The name contains NARAYAN.
d. strrev()
syntax:
strrev(string);
It finds the reverse of the given string.
Example:
char address[] = “Bharatpur”;
strrev(address);
The address contains ruptarahB.
e. strcpy()
syntax:
strcpy(destination string, source string);
It copies content of source string to destination string.
Example:
char name1[30];
char name2[] = “Nepal”;
strcpy(name1, name2);
The name1 and name2 contain Nepal.
f. strcat()
syntax:
strcat(string1, string2);
It concatenates two strings and stores the new string to string1.
Example:
char fname[30] = “Subash ”.
char lname[15] = “Poudel”.
strcat(fname, lname);
The fname stores Subash Poudel.
g. strcmp()
syntax:
strcmp(string1, string2);
It compares two strings character by character and returns
I. 0 if both strings are identical (same).
II. >0 (positive) if string1 is greater than string2.
III. <0 (negative) if string1 is less than string2.
Examples:
strcmp(“Ram”, “Sita”); // returns negative
strcmp(“Sita”, “Ram”); //returns positive
4
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI Rupeshshrestha64@gmai.com
strcmp(“Ram”, “Ram”); //returns 0
strcmp(“Rita”, “Ram”); //returns positive
strcmp(“Ram”, “Ramesh”); //returns negative
strcmp(“Ramesh”, “Ram”); //returns postive
char address[] = “Bharatpur”;
strcmp(“Bhadrapur”, address); //returns negative
Examples:
Write a program to read a string from user and check whether the given string is palindrome or not.
Old Questions (Program Only)
1. Write a program to display the third largest number in an array of size N.
2. Write a program to multiply given two matrices A and B of order MxN and PxQ.
3. Write a program to input N numbers of integer type in an array and find the sum of square of the
largest number and cube of the smallest number present in the given array.
4. Write a program to read a string from user and check whether the given string is palindrome or not,
without using string handling.
5. WAP to read a 3x3 matrix. Then multiply each odd number of the matrix by 3 and display the
resultant matrix.
6. Write a program to read the age of 400 persons and count the number of persons in the age group 70
to 75. Use for and continue statements.
7. Write a program to enter a string and print its reverse without using strrev() function.
8. Write a program to input p by p matrix and print sum of squares of diagonal elements of matrix.