Chapter-5 (12)
Chapter-5 (12)
Chapter No. 5
Arrays and Strings
MCQs ……………………………………………………………. Page 2
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
2
int k[3][5] ={ {3, 10, 12, 27, 12}, {21, 20, 18, 25, 1}, {15, 16, 17, 44, 4} };
What is in k [1][3]; ?
A. 12 B. 18
C. 25 D. 15
vii. Given the following:
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
3
Ans: - An array is a collection of consecutive/contiguous memory locations with same name and data
type. Each memory location in an array is called element of array. Each element in the array is
accessed by its index or subscript. Examples are a[10], b[3][4] etc.
Advantages of array: -
Ans: - The differences between one dimensional and two dimensional arrays are as follows.
Ans: - An array is a collection of consecutive/contiguous memory locations with same name and data
type. The following operations are performed on arrays.
a) Defining an array: - Defining/Declaring of an array means to tell about the data type of array,
name of array and type of array.
float b[10];
char name[30];
b) Initialization an array: - Initialization of array means to give values to array using assignment
operator.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
4
Examples: - int a[3]]4] = {{4, 18, -16, 11} , {-5, 10, -2, 12} , {15, -3, 17, 18}};
char country*10+ = ,‘P’, ‘A’, ‘K’, ‘I’, ‘S’, ‘T’, ‘A’, ‘N’-;
c) Filling of array: - Filling of array means to store values in the array either by using assignment
statement or using other input statements like cin ( ), gets ( ), cin.get ( ).
Examples: -
a[2] = 7 ;
a[3] = 49 ;
cin>>a [i] ;
cin.get(str1, 30);
gets(str);
d) Manipulation of array: - Manipulation of array means to perform different arithmetic, relational
operations etc on values stored in the array.
Examples: -
if (a[i] ==5)
sum = sum + a[i][j];
e) Printing of array: - Printing of array means to print the values stored in the array.
Examples: -
cout<< [i] ;
puts(str);
Ans: - A type of array in which all elements are arranged in the form of a list is known as one-
dimensional array. It is also called linear array. The following segments of codes indicate that how
one dimensional array can be defined and initialized.
char country[10] = ,‘P’, ‘A’, ‘K’, ‘I’, ‘S’, ‘T’, ‘A’, ‘N’-;
Ans: - Two-dimensional array represents a collection of same type of data with a single that is in the
form of table or matrix. The following segments of codes indicate that how two dimensional array
can be defined and initialized.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
5
int a[3]]4] = {{4, 18, -16, 11} , {-5, 10, -2, 12} , {15, -3, 17, 18}};
float b[2][4] = {{12.3 , 15.8, -3.4, 17.2}, {18.6, 10.7, 34.8, 89.2}};
6. Declare an array named x, that has 3 rows and 5 columns and assign it values from 1 to 15 in
the declaration statement.
Ans: -
int x[3]]5] = {{1, 2, 2, 3, 4, 5} , {6, 7, 8, 9, 10} , {11, 12, 13, 14, 15}};
OR
{6, 7, 8, 9, 10} ,
Ans: - The sizeof( ) function is used to find the number of bytes occupied by a variable or a data type
like int, float, double, char etc. The name of variable or data type is given in parenthesis. Its general
syntax is :
Output:-
Sizeof(datatype);
Data Type Bytes
Example: -
-------------- -------
# include <conio.h>
char 1
void main (void)
int 4
{
long 4
clrscr ( );
cout <<” \n Data Type Bytes”<<endl ; float 4
cout <<” -------------- -------”<<endl ; double 8
cout <<”char ” <<sizeof(char)<<endl ;
cout <<”int ” <<sizeof(int) <<endl ;
cout <<”long ” <<sizeof(long) <<endl ;
cout <<”float ” <<sizeof(float) <<endl ;
cout <<”double ” <<sizeof(double) <<endl ;
getch ( );
}
8. Define string and explain how it is stored in computer memory.
Ans: - String is a sequence of characters. It may consist of alphabetic characters, digits and special
characters. A string in C ++ is stored in one-dimensional character array. Each element of array
holds/stores one character. The string ends with a special character known as null character denoted
by \0. The null character is automatically appended at the end of string. For example
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
6
Index 0 1 2 3 4 5 6 7 8 9
country P A K I S T A N \0
9. What is the advantage of using cin.get( ) function over cin statement for reading a string.
Ans: -The advantage of using cin.get( ) function over cin statement is that it can input a string that
may contain blank spaces.
Example: -
Ans: -The differences between strcpy( ) and strcmp( ) functions are as follows.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
7
Ans: -The differences between strlen( ) and strcat( ) functions are as follows.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
8
Long Questions/Answers
or
Examples: -
int arr[5];
int table[4][5];
The above figure is an example of one dimensional array with a name arr and size 5. Each memory
location in an array is called element of array and is represented by an integer called subscript.
One Dimensional array: - A type of array in which all elements are arranged in the form of a list is
known as one-dimensional array. It is also called linear array because all elements are arranged in
one row or a column. Each element in one-dimensional array is accessed/referred with the help of
one index. By default the address of first subscript is zero (0) and the last subscript is one less than
the total number mentioned in the size.
dataType is the type of data which will be stored in the array. It can be int, float or char.
arrayName can be any valid variable name.
arraySize is the total number of element array can store. It must be an unsigned integer
constant.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
9
Example: - The following program will fill numeric array of 5 elements and will print the them in a
column.
# include <iostream.h>
# include <conio.h>
void main (void)
{
Output:-
clrscr ( );
Enter a number = 4
int a[5], i ; // declaration of array
for (i = 1 ; i<= 5 ; i ++) // filling of array Enter a number = 5
{ Enter a number = 3
cout<<”Enter a number = “; Enter a number = 9
cin>>a [i] ;
Enter a number = 10
}
===Array===
cout<<”\n ===Array===“<< endl ;
4
for (i = 1 ; i <= 5 ; i ++) // printing of array
cout<< a [i] << endl; 5
getch ( ); 3
} 9
10
Ans: - Two-dimensional array represents a collection of same type of data with a single that is in
the form of table or matrix. Two-dimensional array has two dimensions i.e. horizontal and vertical.
Each element in two-dimensional array is accessed/referred with the help of two indexes. One index
is used to indicate the row and the second index indicates the column of the array.
Example: - The following program will accept 9 numbers and will print them in 3 rows and 3
columns.
# include <iostream.h>
# include <conio.h>
void main (void)
{
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
10
clrscr ( );
int a[3][3], i, j ; // declaration of array
for (i = 1 ; i <= 3; i ++) // filling of array
Output:-
for (j = 1 ; j <= 3 ; j ++)
{ Enter a number = 4
getch ( ); 4 5 3
} 9 10 50
7 19 60
3. What are strings? How strings are defined in C ++? Give examples.
Ans: - String: - String is a sequence of characters. It may consist of alphabetic characters, digits and
special characters. A string in C ++ is stored in one-dimensional character array. Each element of
array holds/stores one character. The string ends with a special character known as null character
denoted by \0. The null character is automatically appended at the end of string. The strings are
typically used to represent name, address, object, book tile etc. Its general syntax is:
Example- 1: -
char country[10] = ,‘P’, ‘A’, ‘K’, ‘I’, ‘S’, ‘T’, ‘A’, ‘N’-;
Index 0 1 2 3 4 5 6 7 8 9
country P A K I S T A N \0
Example- 2: -
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
11
Index 0 1 2 3 4 5 6 7 8 9
country S U N D A Y \0
Example- 3: -
Index 0 1 2 3 4 5
country M A N G o \0
d) strlen ( ) f) strcmp ( )
Ans: - a) cin.get( ) function: - The cin.get( ) function is used to read a string from the keyboard that
may contain blank spaces. Its general syntax is:
cin.get(strvar, strsize);
# include <iostream.h>
# include <conio.h> Output:-
# include <string.h>
Enter a string : C ++ is a High Level Language.
void main (void)
You typed : C ++ is a High Level Language.
{
clrscr ( );
char str[50];
cout<<“\n Enter a string : “;
cin.get(str, 50);
cout<<“\n You typed : “ <<str;
getch ( );
}
b) strcpy( ) function: - strcpy stands for string copy. The strcpy( ) function is used to copy the
contents of a string variable or string constant to another string variable including the null
character. Its general syntax is:
strcpy(string-1, string-2);
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
12
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( ); Output:-
char str1[10], str2[10] ; String-1 : Science
str1 = “Computer”;
String-2 : Science
str2 = “Science”;
strcpy(str1, str2);
cout<<“\n String-1 : “ << str1;
cout<<“\n String-2 : “ << str2 ;
getch ( );
}
c) strcat( ) function: - strcat stands for string concatenation. The strcat( ) function is used to
append the contents of one string to the end of another string. This function is used for
concatenation or joining of two strings. Its general syntax is:
strcat(string-1, string-2);
string-1 is the string variable with which value of strng-2 will be appended.
String-2 is the string variable/constant whose value is to be appended to string-1.
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( ); Output:-
char str1[10], str2[10] ; String-1 + String-2 : Islamabad
str1 = “Islam”;
str2 = “abad”;
strcat(str1, str2);
cout<<“\n String-1 + String-2 : “ << str1 ;
getch ( );
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
13
d) strlen( ) function: - strlen stands for string length. The strlen( ) function is used to find the
length of a string. The length includes all characters as well as spaces in the string. Its general
syntax is:
strlen(string);
# include <iostream.h>
# include <conio.h>
# include <string.h>
Output:-
void main (void)
{ Total number of characters are : 8
clrscr ( );
char str[10];
str = “Computer”;
cout<<“\n Total number of characters are : “ <<strlen(str) ;
getch ( );
}
e) strcmp( ) function: - strcmp stands for string compare. The strcmp( ) function is used to
compare two strings character by character. The comparison is case sensitive. Its general
syntax is:
strcmp(string-1, string-2);
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( ); Output:-
int z ; String-1 is less than String-2
char str1[10], str2[10] ;
str1 = “Pakistan”;
str2 = “Zindabad”;
z = strcmp(str1, str2);
if (z == 0)
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
14
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
15
Lab Activities
1. Write a program that reads ten numbers in an array and prints them in reverse order.
Ans: - Method-1:
Output:-
# include <iostream.h>
# include <conio.h> =Reverse order=
void main (void) 67
int i, a[10] = {5,3,7,9,12, 6, 56, 32, 49, 67}; 49
cout<<”==Reverse order==“<< endl ; 32
for (i = 9 ; i >= 0 ; i - -)
56
cout<< a [i] << endl ;
6
getch ( );
12
}
9
7
3
5
Method-2:
# include <iostream.h>
Output:-
# include <conio.h>
void main (void) Enter a number = 4
{ Enter a number = 5
clrscr ( ); Enter a number = 3
int i, a[10] ; Enter a number = 9
for (i = 1 ; i <= 10 ; i ++)
Enter a number = 1
{
Enter a number = 6
cout<<”Enter a number = “;
cin>>a [i] ; Enter a number = 8
} Enter a number = 7
cout<<”\n =Reverse order=“<< endl ; Enter a number = 2
for (i = 10 ; i >= 1 ; i - -) Enter a number = 71
cout<< a [i] <<“\t” ;
=Reverse order=
getch ( );
71 2 7 8 6 1 9 3 5 4
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
16
2. Write a program that reads 5 integer values in array a and finds their total and average.
(Page No. 98)
Ans: -
# include <iostream.h>
# include <conio.h>
void main (void)
{
clrscr ( );
Output:-
int i, a[5];
Enter a number = 4
float avg ;
sum = 0 ; Enter a number = 5
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
17
3. Write a program that reads ten integers and prints the biggest.(Page No. 98-99)
Ans:
# include <iostream.h>
# include <conio.h>
void main (void) Output:-
{ Enter a number = 41
clrscr ( ); Enter a number = 52
int i, bg, a[10] ;
Enter a number = 34
for (i = 1 ; i <= 10 ; i ++)
Enter a number = 90
{
Enter a number = 100
cout<<”Enter a number = “;
cin>>a [i] ; Enter a number = 60
} Enter a number = 18
bg = a[1]; Enter a number = 78
for (i = 2 ; i <= 10 ; i ++)
Enter a number = 20
if (a[i] >bg)
Enter a number = 71
bg = a[i];
The Biggest number = 100
cout<< “\n The Biggest number = “<<bg;
getch ( );
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
18
4. Write a program that reads ten numbers and prints the smallest along with its index.
Ans: -
# include <iostream.h>
Output:-
# include <conio.h>
Enter a number = 41
void main (void)
Enter a number = 52
{
clrscr ( ); Enter a number = 34
int i, loc, sm, a[10]; Enter a number = 90
for (i = 1 ; i <= 10 ; i ++) Enter a number = 100
{
Enter a number = 60
cout<<”Enter a number = “;
Enter a number = 18
cin>>a [i] ;
Enter a number = 78
}
sm = a[1]; Enter a number = 20
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
19
5. Write a program that reads marks of n students and prints the number of students passed. The
marks are in the range of 0 to 100 and passing marks are 33. (Page No. 99)
Ans:
# include <iostream.h>
# include <conio.h>
void main (void)
{
clrscr ( );
int n, i, pass, marks[50] ;
pass = 0 ;
cout<<”Enter the number of students (Max 50) = “;
cin>>n;
for (i = 1 ; i <= n ; i ++)
{
cout<<” \n Enter Marks = “;
cin>>marks [i] ;
}
for (i = 1 ; i <= n ; i ++)
if (marks[i] >=33)
pass = pass ++;
cout<< “\n Number of students Passed = “<<pass;
getch ( );
Output:-
}
Enter the number of students (Max 50) = 10
Enter Marks = 60
Enter Marks = 56
Enter Marks = 45
Enter Marks = 23
Enter Marks = 33
Enter Marks = 12
Enter Marks = 54
Enter Marks = 40
Enter Marks = 30
Enter Marks = 48
Number of students Passed = 8
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
20
6. Write a program that declares a two dimensional array of 3 rows and 4 columns, initializes it
with the data 30, 20, 55, 206, 78, 81, 25, 90, 3, 48, 67, 104 and finds the total of all the values.
(Page No. 105)
Ans:
# include <iostream.h>
# include <conio.h>
void main (void)
{
clrscr ( );
int i, j, sum, a[3]]4] = {{30, 20, 55, 206} , {78, 81, 25, 90} , {3, 48, 67, 104}};
sum = 0 ;
for (i =0 ; i <= 2 ; i ++)
{
for (j =0 ; j <= 3 ; j ++)
sum = sum + a[i][j];
}
cout<< “ Sum of all values = “ << sum ;
getch ( );
}
Output:-
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
21
7. For the given array: int arr[15] = {4, 8, 5, 1, 3, 5, 0, 12, 5, 7, 3, 15, 8, 4, 11};
Write a program that prints the number of times 5 appears in the array.
# include <conio.h>
void main (void)
{
clrscr ( );
int i, c, a[15] = {4, 8, 5, 1, 3, 5, 0, 12, 5, 7, 3, 15, 8, 4, 11} ;
c=0;
Output:-
for (i = 1 ; i <= 15 ; i ++)
5 appears 3 times in the array
if (a[i] ==5)
c ++;
cout<<“ 5 appears “ << c << “times in the array” ;
getch ( );
}
8. For the given array: int a[3]]2] = {{6, 3} , {7, 8} , {4, 5}};
Write a program that displays all the elements in the form 6 3
of a matrix as shown and finds its sum. 7 8
# include <conio.h>
void main (void)
{
clrscr ( );
int i, j, sum, a[3][2] = {{6, 3} , {7, 8} , {4, 5}};
sum = 0 ;
for (i =0 ; i <= 2 ; i ++)
{
Output:-
for (j =0 ; j<= 1 ; j ++)
{ 6 3
cout<< a[i][j]<< “\t” ; 7 8
sum = sum + a[i][j];
4 5
}
Sum = 33
cout<<endl ;
}
cout<<“ Sum = “ <<sum ;
getch ( );
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
22
Ans: -
# include <iostream.h>
# include <conio.h>
void main (void)
{
clrscr ( );
int i, j, sum, a[3]]4] = {{4, 18, -16, 11} , {-5, 10, -2, 12} , {15, -3, 17, 18}};
sum = 0 ;
for (i =0 ; i <= 2 ; i ++)
for (j =0 ; j <= 3 ; j ++)
{
if (a[i][j]>= 0)
sum = sum + a[i][j];
}
cout<<“ Sum of positive numbers = “ << sum ;
getch ( );
}
Output:-
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
23
10. Write a program that adds the corresponding elements of two 2-dimensional arrays with 2
rows and 4 columns. It displays the result in the form of matrix.
OR
For the given array:
int a[2]]4] = {{14, 8, 11, 10} , {15, 12, 20, 3}} ,
b[2]]4] = {{2, 3, 4, 7} , {6, 7, 8, 9}}
Write a program that adds the two arrays and produce the following output.
Sum of two arrays is:
16 11 15 17
21 19 28 12
Ans: -
# include <iostream.h>
# include <conio.h>
void main (void)
{
clrscr ( );
int i, j, c[2][4] ;
getch ( );
}
Output:-
16 11 15 17
21 19 28 12
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
24
11. Write a program that inputs data from keyboard in a two-dimensional array x, that has r rows
and c columns and print the sum of each row. (Page No. 115)
Ans: -
# include <iostream.h>
# include <conio.h>
void main (void)
{ Output:-
{ Sum of row 2 = 16
for (j =0 ; j <= 2 ; j ++) Sum of row 3 = 22
sum = sum + a[i][j] ;
cout<< “\n Sum of row “<< i <<” = “<< sum<<endl ;
}
getch ( );
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
25
12. Write a program that reads a string, copies it into another string and then prints both strings.
Ans: - Method-1:
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( ); Output:-
char str1[10], str2[10] ; String-1 : Science
str1 = “Computer”;
String-2 : Science
str2 = “Science”;
strcpy(str1, str2);
cout<<“\n String-1 : “ << str1;
cout<<“\n String-2 : “ << str2 ;
getch ( );
}
Method-2:
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
Output:-
clrscr ( );
int i ; Enter a string : Information Technology
char str1[10], str2[10] ; String-1 : Information Technology
cout<< “\Enter a string : “; String-2 : Information Technology
cin.get (str1, 10) ;
while (str1[i] != „\0‟)
{
str2[i] = str1[i] ;
i ++ ;
}
str2[i] = „\0‟ ;
cout<<“\n String-1 : “ << str1;
cout<<“\n String-2 : “ << str2 ;
getch ( );
}
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
26
13. Write a program that reads two strings of size 20 and perform the following operations.
a) Print both strings with their length.
b) Concatenate the second string onto the end of first string and print it.
Ans: -
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( );
char str1[20], str2[20];
str1 = “ISLAM”;
str2 = “ABAD”;
cout<<“\n String-1 = “ <<str1;
cout<<“\n String-2 = “ <<str2;
cout<<“\n Number of characters in “ <<str1<< “ are “ <<strlen(str1);
cout<<“\n Number of characters in “ <<str2<< “ are “ <<strlen(str2);
strcat(str1, str2);
cout<<“\n Concatenation of both strings : “ << str1;
getch ( );
}
Output:-
String-1 = ISLAM
String-2 = ABAD
Number of characters in ISLAM are 5
Number of characters in ABAD are 4
Concatenation of both strings : ISLAMABAD
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
27
14. Write a program that reads three strings and prints the smallest.
Ans: -
# include <iostream.h>
# include <conio.h>
# include <string.h>
void main (void)
{
clrscr ( );
int a, b, c ;
char str1[30], str2[30],str3[30];
cout<< “\n Enter string-1 : “;
cin.get(str1, 25) ;
cout<< “\n Enter string-2 : “;
cin.get(str2, 25) ;
cout<< “\n Enter string-3 : “;
cin.get(str3, 25) ;
a = strlen(str1) ;
b = strlen(str2) ;
c = strlen(str3) ;
if ((a<b) && (a<c))
cout<<”\n Smallest string is : “ <<str1;
else if ((b<a) && (b<c))
cout<<”\n Smallest string is : “ <<str2;
else
cout<<”\n Smallest string is : “ <<str3;
getch ( );
}
Output:-
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
28
15. Write a program that sorts a numeric list of 10 numbers in ascending order.
Ans: -
{ Enter a number = 60
cout<<”Enter a number = “; Enter a number = 18
cin>>a [i] ; Enter a number = 78
}
Enter a number = 20
for (i = 1 ; i <= 9 ; i ++)
Enter a number = 71
{
for (j = i + 1 ; j <= 10 ; j ++) ==Ascending Order==
if (a[j] <a[i]) 18
{ 20
sm = a[j]; 34
a[j] = a[i];
41
a[i] = sm;
52
}
} 60
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
29
16. Write a program that sorts a numeric list of 10 numbers in descending order.
Ans: -
# include <iostream.h>
Output:-
# include <conio.h>
Enter a number = 41
void main (void)
Enter a number = 52
{
clrscr ( ); Enter a number = 34
int i, j, sm, a[10] ; Enter a number = 90
for (i = 1 ; i <= 10 ; i ++) Enter a number = 100
{
Enter a number = 60
cout<<”Enter a number = “;
Enter a number = 18
cin>>a [i] ;
Enter a number = 78
}
for (i = 1 ; i <= 9 ; i ++) Enter a number = 20
{ Enter a number = 71
for ( j = i + 1 ; j <= 10 ; j ++) ==Descending Order==
if (a[j] > a[i])
100
{
90
sm = a[j];
78
a[j] = a[i];
a[i] = sm; 71
} 60
} 52
cout<< “\n ==Descending Order==“<< endl ; 41
for (i = 1 ; i <= 10 ; i ++)
34
cout<<a [i] <<endl ;
20
getch ( );
} 18
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar