0% found this document useful (0 votes)
9 views

XII-Ch.5

The document covers Chapter 5 on Arrays and Strings from Hamza Army Public School & College, including multiple-choice questions, short questions, and extensive questions related to arrays and strings in programming. It explains the concepts of one-dimensional and two-dimensional arrays, their declarations, initializations, and operations, as well as string definitions and functions in C++. The document provides examples and code snippets to illustrate the usage of arrays and strings.

Uploaded by

hassnagul50984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

XII-Ch.5

The document covers Chapter 5 on Arrays and Strings from Hamza Army Public School & College, including multiple-choice questions, short questions, and extensive questions related to arrays and strings in programming. It explains the concepts of one-dimensional and two-dimensional arrays, their declarations, initializations, and operations, as well as string definitions and functions in C++. The document provides examples and code snippets to illustrate the usage of arrays and strings.

Uploaded by

hassnagul50984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Hamza Army Public School & College, Girls Wing Chapter No.

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

Computer Science HSSC – II P a g e | 45


Hamza Army Public School & College, Girls Wing Chapter No.5

Short Questions

Q2. Write short answers of the following questions.


i. Define array and give its advantages in programming.
Answer:
Array:
An array is a collection of consecutive memory locations with same name and type.
i. Arrays can store a large number of values with single name.
ii. They are used to process a list of numbers and strings easily and quickly.
iii. The values stored in an array can be sorted and searched easily.
iv. The use of arrays reduces program size.

ii. Differentiate between one dimensional and two dimensional arrays.


Answer:
One Dimensional Array Two Dimensional Array
i. It consists of only single row or single column. i. It consists of multiple rows and columns.

ii. Syntax: ii.Syntax:


Data type Array_name[size]; Data type Array_name[rows][col];
iii. Each element of this array can be accessed iii. Each element of this array can be
using one index value. accessed using two index value.

iii. What is purpose of sizeof()function? Give one example.


Answer:
The sizeof() function is used to find the number of bytes occupied by a variable or data type. It is
used to determine the amount of storage reserved for int, float, double, char etc. data types.
Example:
#include <iostream.h>
#include <conio.h>
void main()
{
cout<< "/nData Type Bytes"
<< "\n------------------ -------"
<< "\n int "<<sizeof(int)
<< "\n float "<<sizeof(float)
<< "\n double "<<sizeof(double)
<< "\n char "<<sizeof(char)
<<endl;
getch();
}
The output of the program:
Data Type Byte
int 4
float 4
double 8
char 1

Computer Science HSSC – II P a g e | 46


Hamza Army Public School & College, Girls Wing Chapter No.5

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

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
viii. Differentiate between strlen() ant strcat() functions.
Answer:
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");
strcpy(string2,"WORK");
strcat(string1 ,string2);
cout<< "string1 ="<<string1 <<endl;
getch();
}
The output will be:
string1 = HOMEWORK
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

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.

Computer Science HSSC – II P a g e | 49


Hamza Army Public School & College, Girls Wing Chapter No.5

One Dimensional Array:


Declaration of Array:
i. The data type of the array elements.
ii. The name of array.
iii. Array size: The number of elements it is required to store.
Syntax: datatype array_name[arraysize];
Example:
int a[10], b[15];
float weight[8];
Initialization of Array:
An array can be initialized in declaration statement. The following statement declares the marks array as integer
of size 6 and assigns marks to each element of the array.
int marks[6]={45, 67, 50, 79, 58, 36};
Using Arrays in Programs
The following program reads 5 integer values in array a and finds their total and average.
#include <iostream.h>
#include <conio.h>
void main()
{
int a[5], k, total;
float avg;
for(k=0; k<5; k++)
{
cout<< "Enter a number:";
cin>> a[k];
}
total=0;
for(k=0; k<5; k++)
total=total+a[k];
avg=total/5.0;
cout<< "\nTotal="<<total<<endl;
cout<< "Average="<<avg<<endl;
getch();
}
ii. Explain two dimensional array in detail with one example.
Answer:
Two Dimensional Array:
A two dimensional array uses a single variable name to represent a collection of same type
of data that is in the form of a table or matrix. It has two dimensions i.e. vertical and
horizontal dimensions. Vertical dimension represents rows and horizontal dimension
represents columns. Two dimensional array provides an easy way of handling data that is
stored in the form of a table.
Declaration of Two dimensional Array:
• The data type of the array elements.
• The name of the array
• row_size: The number of elements it is required to store in rows.
• column_size: The number of elements it is required to store in columns.
Syntax: datatype arrayname[rowsize][columnsize];

Computer Science HSSC – II P a g e | 50


Hamza Army Public School & College, Girls Wing Chapter No.5

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

cout<<"Enter array elements : ";


for(i=0; i<10; i++)
{
cin>>arr[i];
}
cout<<"Now the Reverse of the Array is : \n";
for(i=9; i>=1; i--)
{
cout<<arr[i]<<endl;
}
getch();
}
Q2. Write a program that reads ten numbers and print the smallest along with its index.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int arr[10], i,j, min, location;
cout<<"Enter array elements : ";
for(i=0; i<10; i++)
{
cin>>arr[i];
}
min=arr[0];
for(j=1; j<=10; j++)
{
for(i=1; i<=10; i++)
{
if(arr[i] < min)
{
min = arr[i];
location = i;
}
}
}
cout<<"Smallest element = " << min <<"is at location" <<location;
getch();
}
Q3. 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 the number 5 appears in the array.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i, count=0;
int arr[15]={4,8,5,1,3,5,0,12,5,7,3,15,8,4,11};
for(i=0; i<15; i++)
{
Computer Science HSSC – II P a g e | 54
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();
}

Q6. For the given array:


int a[2][4] = {{14,8,11,10},{15,12,20,3}};
int b[2][4] = {{2, 3, 4, 7},{6, 7, 8, 9}};
Write a program that adds the two arrays and produces the following output.
Sum of two arrays is:
16 11 15 17
21 19 28 12
#include <iostream.h>
#include <conio.h>
void main()
{
int a[2][4] = {{14,8,11,10},{15,12,20,3}};
int b[2][4] = {{2, 3, 4, 7},{6, 7, 8, 9}};
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<4; j++)
{
cout<<(a[i][j]+b[i][j])<<"\t";
}
cout<<"\n";
}
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

for(j=0; j<c; j++)


{
cout<<"Enter number";
cin>>a[i][j];
}
}
cout<<"Two dimensional array is"<<endl;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
for(i=0; i<r; i++)
{
sum = 0;
for(j=0; j<c; j++)
{
sum = sum+a[i][j];
}
cout<<"Sum of row" <<(i+1) <<"=" <<sum<<endl;
}
getch();
}
Q8. Write a program that reads a string, copy it into another string and then print both
strings.
#include <iostream.h>
#include <conio.h>
#include <string.h>
int main()
{
char str1[80],str2[80];
cout<<"Enter a string:\n";
cin.get(str1,80);
strcpy(str2, str1);
cout<<"String1 = " <<str1 <<"\n String2 = " << str2;
getch();
}
Q9. Write a program that reads 2 strings of size 20 and perform the following
operations.
a. Print both strings with their length.
b. Concatenate the second string into the end of first string and print
it.
#include <iostream.h>
#include <conio.h>
#include <string.h>
int main()
{
Computer Science HSSC – II P a g e | 57
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();
}

Computer Science HSSC – II P a g e | 58

You might also like