XII-Ch.5
XII-Ch.5
Chapter # 5
Arrays and Strings
Exercise .
Q1. Select the best answer for the following MCQs.
i. Which of the following is correct declaration of array?
A. Int arr; C. int arr{10}
B. int arr[10] D. int arr(10)
ii. What is the index number of the last element of an array with 5 elements?
A. 5 B. 0 C. 4 D.6
iii. What is an array?
A. An array is a series of elements.
B. An array is a series of elements of the same type placed in noncontiguous
memory locations.
C. An array is a series of elements of the same type placed in contiguous
memory locations.
D. none of the above.
iv. Which of the following identifies the first element in array named temp?
A. temp[0] B. temp(1) C. temp[1] D. temp(0)
v. Which of the following identifies the last element of array declared as int a[10][10];?
A. a[10][10] B. a[11][11] C. a[9][10] D. a[9][9]
vi. Given the following
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. 25 C. 18 D. 15
vii. Given the following:
int arr[3][4]=
{{ 12,0,5,10},
{7,8,19,30}
{33,1,2,22}};
Which of the following statements will replace 19 with 50?
A. arr[2][3]=50; C. arr[1][2]=50;
B. arr[2][1]=50; D. arr[19]=50;
viii. Which of the following functions is used to append a string onto the end of another
string?
A. strcpy() C. strcat
B. strlen D. strcmp
Answer:
i. B ii. C iii. C iv. A
v. D vi. B vii. C viii. C
Short Questions
iv. Declare an array named x, that has 3 rows and 5 columns and assign it values from 1 to
15 in the declaration statement.
Answer:
#include <iostream.h>
#include <conio.h>
void main()
{
int i,j,k[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
for(i=0; i<3; i++)
{
for(j=0; j<5; j++)
{
cout<<k[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
v. Define string and explain how it is stored in computer memory.
Answer:
String:
String is a sequence of characters written in double quotes. It may consist of alphabetic character,
digits and special symbols. A string in C++ is stored in one dimensional array of characters. Each
location of array stores one character. The string ends with a special symbol known as null character
denoted by \0.
vi. What is the advantage of using cin.get() function over cin statement for reading a string.
Answer:
The advantage of using cin.get function over cin statement is that cin.get can input a string that may
contain blank spaces.
By Using cin:
#include <iostream.h>
#include <conio.h>
void main()
{
char str[50];
cout<< "Enter a string";
cin>>str;
cout<< "You typed:" <<str<<endl;
getch();
}
Output:
Enter a string: Information Technology
You typed Information
By Using cin.get:
#include <iostream.h>
#include <conio.h>
void main()
{
char str[50];
cout<< "Enter a string";
Computer Science HSSC – II P a g e | 47
Hamza Army Public School & College, Girls Wing Chapter No.5
cin.get(str,50);
cout<< "You typed:" <<str<<endl;
getch();
}
Output:
Enter a string: Information Technology
You typed Information Technology
vii. Differentiate between strcpy() and strcmp() functions.
Answer:
strcpy() Function
The strcpy() functions is used to copy contents of a string variable or string constant to another string
variable.
Syntax:
strcpy(string2, string1);
Example:
#include <iostream.h>
#include <conio.h>
void main()
{
char string1[10]= "ISLAMABAD", string2[10], string3[10];
strcpy(string2,string1 );
cout<<"string2="<<string2<<endl;
strcpy(string3, "PAKISTAN");
cout<<"string3="<<string3<<endl;
getch();
}
Output:
string2=ISLAMABAD
string3=PAKISTAN
Strcmp() Function:
The strcmp() function compares two strings and returns an integer value based on the result of
comparison. This comparison is based on ASCII codes of characters.
Syntax:
strcmp(string1, string2);
The function will return the following integer values based on the result of comparison.
It will return 0 if string1 and string2 are the same.
It will return 1 if string1 is greater than string2.
It will return -1 if string1 is less than string2.
Example:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[15]="MANGO", string2[15]= "MANGO",
string3[15]= "POTATO", string4[15]="ORANGE";
int x, y, z;
x=strcmp(string1 ,string2);
cout<<"stringl and string2 are equal, x="<<x<<endl;
y=strcmp(string3, string1);
Computer Science HSSC – II P a g e | 48
Hamza Army Public School & College, Girls Wing Chapter No.5
Extensive Questions
Q3. Write long answers of the following questions.
i. What is an array? Explain one dimensional array in detail with one example.
Answer:
Array:
An array is a collection of consecutive memory locations with same name and type.
Example:
int a[3][5];
float height[8][10];
Initialization of Two Dimensional Array:
int a[3][4]= {{45, 66, 39, 72}, {87, 50, 56, 63}, {44, 23, 58, 88}};
Accessing and Filling a Two dimensional array:
Data can be written in any element of a two dimensional array as if it was a normal variable by
specifying the index of row and column. Two dimensional arrays are generally expressed row by
row using nested loop. The row index is used as outer loop variable and column index as inner
loop variable. It may also be accessed column by column.
Example:
Program that declares a two dimensional integer 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.
#include <iostream.h>
#include <conio.h>
void main()
{
int i, j, total,k[3][4]={{30, 20, 55, 206},{78, 81, 25, 94},{3, 48, 67, 104}};
total=0;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
total=total+k[i][j];
cout<< "Total="<<total<<endl;
getch();
}
iii. What are strings? How strings are defined in C++? Give examples.
Answer:
String is a sequence of characters written in double quotes. It may consist of alphabetic characters,
digits and special symbols. In C++ a string is stored in a one dimensional array of char data type. Each
element of array stores one character. The string may end with a special symbol known as null
character denoted by \0. The null character is automatically appended at the end of string. String is
most commonly used item is computer programming to represent name, address, object, book title,
etc.
STRING DECLARATION:
C++ stores a string as an array of characters. The size of the string should be long enough to store
the null character. For example, if the string value has 15 characters then the size of string array
must be at least 16.
Syntax:
The following is the general form of declaration of string.
char stringname[stringsize];
• The char keyword lets the compiler know that a variable of type character is declared.
• The stringname is the name of the string variable. it follows the same name rules of other
type of variables.
• The stringsize enclosed within square brackets specifies the number of characters that can
be stored in the string.
• Since the null character is appended at the end of string, if a string has n characters then
the stringsize should be at least n+1.
INITIALIZING STRINGS
Just like arrays of integer and floating-point numbers, strings arrays can also be initialized in the
declaration statement.
Computer Science HSSC – II P a g e | 51
Hamza Army Public School & College, Girls Wing Chapter No.5
Example:
The following statement declares and initializes the string variable weekday to Sunday.
char weekday[10]={‘S’,’u’,'n', 'd', ‘a', 'y'};
The next statement provides another easy way for the same declaration and initialization.
char weekday[10]= "Sunday";
It allows to declare and initialize the string variable by including the weekday within double quotes. When a string
variable is initialized in this way as a whole, the curly brackets are not required.
The following diagram shows how the string variable weekday is represented in computer memory.
Index:
0 1 2 3 4 5 6 7 8 9
Variable :
S u n d a y \0
The index starts from zero. The compiler automatically places the null character (/0) after the last character. The
remaining three characters are not defined.
Example:
Another way used to initialize a string variable is to type the contents within the curly brackets but without
mentioning its size by leaving the square brackets empty. This is shown in the following example.
char city[ ]= "Karachi";
iv. Explain the purpose of the following string functions.
Answer:
strcpy() Function
The strcpy() functions is used to copy contents of a string variable or string constant to another string
variable.
Syntax: strcpy(string2, string1);
Example:
#include <iostream.h>
#include <conio.h>
void main()
{
char string1[10]= "ISLAMABAD", string2[10], string3[10];
strcpy(string2,string1 );
cout<<"string2="<<string2<<endl;
strcpy(string3, "PAKISTAN");
cout<<"string3="<<string3<<endl;
getch();
}
strcat() Function
The strcat() function is used for concatenation or joining of two strings.
Syntax: strcat(stringl, string2);
When this function is executed, it will append (concatenate) string2 onto the end of string1.
Example:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[10], string2[10];
strcpy(string1 ,"HOME");
Computer Science HSSC – II P a g e | 52
Hamza Army Public School & College, Girls Wing Chapter No.5
strcpy(string2,"WORK");
strcat(string1 ,string2);
cout<< "string1 ="<<string1 <<endl;
getch();
}
strlen() Function
The strlen() function is used to return the length (the number of characters) of a string.
Syntax: strlen(string);
Example:
char string[10]="COMPUTER";
cout<<"The number of characters in the string are "<<strlen(string)<<endl;
The output will be:
The number of characters in the string are 8
Strcmp() Function:
The strcmp() function compares two strings and returns an integer value based on the result
of comparison. This comparison is based on ASCII codes of characters.
Syntax: strcmp(string1, string2);
The function will return the following integer values based on the result of comparison.
i. It will return 0 if string1 and string2 are the same.
ii. It will return 1 if string1 is greater than string2.
iii. It will return -1 if string1 is less than string2.
Example:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[15]="MANGO", string2[15]= "MANGO",
string3[15]= "POTATO", string4[15]="ORANGE";
int x, y, z;
x=strcmp(string1 ,string2);
cout<<"stringl and string2 are equal, x="<<x<<endl;
y=strcmp(string3, string1);
cout<<"string3 is greater than string 1, y="<<y<<endl;
z=strcmp(string1,string4);
cout<<"string1 is less than string4, z="<<z<<endl;
getch();
}
The output of the program will be:
stringl and string2 are equal, x= 0
string3 is greater than string 1, y= 1
string1 is less than string4, z= -1
Lab Activities .
Q1. Write a program that reads ten numbers and prints them in reverse order.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int arr[10], i;
Computer Science HSSC – II P a g e | 53
Hamza Army Public School & College, Girls Wing Chapter No.5
if(arr[i] ==5)
count = count+1;
}
for (i=0; i<count; i++)
cout<<"5"<<endl;
getch();
}
Q4. 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 of a matrix as shown below and
find its sum.
6 3
7 8
4 5
#include <iostream.h>
#include <conio.h>
void main()
{
int i,j,a[3][2] = {{6,3},{7,8},{4,5}};
int sum = 0;
for(i=0; i<3; i++)
{
for(j=0; j<2; j++)
{
cout<<a[i][j]<<"\t";
sum = sum+a[i][j];
}
cout<<"\n";
}
cout<<"Sum of elements = "<<sum;
getch();
}
Q5. For the given array:
int arr[3][4] = {{4, 18,-16,11}, {-5, 10, -2, 12}, {15, -3, 15,18}};
Write a program to find the sum of positive numbers.
#include <iostream.h>
#include <conio.h>
void main()
{
int arr[3][4] = {{4, 18,-16,11}, {-5, 10, -2, 12}, {15, -3, 15,18}};
int i, j, sum = 0;
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
cout<<arr[i][j]<<"\t";
if(arr[i][j]>0)
sum = sum+arr[i][j];
}
cout<<"\n";
Computer Science HSSC – II P a g e | 55
Hamza Army Public School & College, Girls Wing Chapter No.5
}
cout<<"Sum of elements = "<<sum;
getch();
}
Q7. Input data from keyboard in a two dimensional array x, that has r rows and c columns
and print sum of each row in the following format.
Sum of row1=
Sum of row2=
.
.
.
Sum of row r=
#include <iostream.h>
#include <conio.h>
void main()
{
int r, c, i,j, sum;
int a[10][10];
cout<<"Enter number of rows";
cin>>r;
cout<<"Enter number of columns";
cin>>c;
for(i=0; i<r; i++)
{
Computer Science HSSC – II P a g e | 56
Hamza Army Public School & College, Girls Wing Chapter No.5
char str1[20],str2[20];
cout<<"Enter a string:\n";
cin.getline(str1,80);
cout<<"Enter Second string:\n";
cin.getline(str2,80);
cout<<"String1 = " <<str1 << " \n The length of string1 is" << strlen(str1)<<endl;
cout<<"String2 = " <<str2 << " \n The length of string2 is" << strlen(str2)<<endl;
getch();
}
Q10. Write a program that reads 3 strings and prints the smallest.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[80],str2[80],str3[80];
cout<<"Enter first string:\n";
cin.getline(str1,80);
cout<<"Enter second string:\n";
cin.getline(str2, 80);
cout<<"Enter third string:\n";
cin.getline(str3,80);
if(strlen(str1)<strlen(str2)&&strlen(str1)<strlen(str3))
cout<<"The smallest String is string1"<<str1;
else if(strlen(str2)<strlen(str3)&&strlen(str2)<strlen(str1))
cout<<"The smallest String is string2"<<str2;
else if(strlen(str3)<strlen(str2)&&strlen(str3)<strlen(str1))
cout<<"The smallest String is string3"<<str3;
getch();
}