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

Chapter-5 (12)

The document contains educational materials on arrays and strings, including multiple choice questions, short questions with answers, and long questions. It covers definitions, advantages, and examples of one-dimensional and two-dimensional arrays, as well as string manipulation functions in C++. The notes are intended for second-year computer students at APS & C (Boys) Peshawar, authored by Riaz Khattak.

Uploaded by

arslan2000888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter-5 (12)

The document contains educational materials on arrays and strings, including multiple choice questions, short questions with answers, and long questions. It covers definitions, advantages, and examples of one-dimensional and two-dimensional arrays, as well as string manipulation functions in C++. The notes are intended for second-year computer students at APS & C (Boys) Peshawar, authored by Riaz Khattak.

Uploaded by

arslan2000888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1

Chapter No. 5
Arrays and Strings
MCQs ……………………………………………………………. Page 2

Short Questions/Answer ………………………………. Pages 3 - 7

Long Questions …………………………………………….. Pages 8–14

Lab Activities…..…………………………………………….. Pages 15–29

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
2

Multiple Choice Questions (MCQs)


Note: - Select the best answer for the following MCQs.

i. Which of the following is correct declaration of array?


A. int arr; B. int arr {10};
C. 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. 4
C. 0 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 non-contiguous 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 an array names 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 [9][10]
C. a [11][11] 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. 18
C. 25 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; B. arr [1][2] = 50;


C. arr [2][1] = 50; D. arr [19] = 50;
viii. Which of the following function appends a string onto the end of another string?
A. strcpy ( ) B. strcat ( )
C. strlen ( ) D. strcmp ( )
ix. What will be produced by the statement cout <<strlen (“COMPUTER”) ;?
A. COMPUTER B. computer
C. 7 D. 8
x. The number of rows declared by the statement int array [5]6]: are :
A. 4 B. 5
C. 6 D. 30
Answers
i. C ii. B iii. C iv. A v. D
vi. C vii. B viii. B ix. 8 x. B

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
3

Short Questions/Answers (Q/A)


1. Define array and give its advantages in programming.

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: -

 Array reduces program size.


 It provides an easy way of handling list of numbers or string.
 It makes programming task simple and easy.
 Array elements can be sorted easily.

2. Differentiate between one dimensional and two dimensional arrays.

Ans: - The differences between one dimensional and two dimensional arrays are as follows.

One dimensional array Two dimensional array


 One dimensional array stores  Two dimensional array stores
elements in one row or a column. elements in rows and columns.
 It is also called list or linear array.  It is also called table or matrix.
 Elements in one dimensional array  Elements of two dimensional array are
are accessed by a single accessed by using two
index/subscript. indexes/subscripts.
 Examples:  Examples:
int a[10]; int a[4][5];
float b[10]; float b[4][5];
etc etc

3. Define the following terms.


a) Defining an array b) Initialization of array c) Filling of array
d) Manipulation of array e) Printing of array

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.

Examples: - int a[4][5];

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}};

float b[5] = {12.3 , 15.8, -3.4, 17.2, 18.6};

char weekday*10+ = “SUNDAY”;

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);

4. Briefly describe how one dimensional array is defined and initialized.

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.

int a[5] = {4, 18, -16, 11,-5};

float b[5] = {12.3 , 15.8, -3.4, 17.2, 18.6};

char weekday*10+ = “SUNDAY”;

char country[10] = ,‘P’, ‘A’, ‘K’, ‘I’, ‘S’, ‘T’, ‘A’, ‘N’-;

char fruit*+ = “APPLE”;

5. Briefly describe how two dimensional array is defined and initialized.

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

int x[3]]5] = {{1, 2, 2, 3, 4, 5} ,

{6, 7, 8, 9, 10} ,

{11, 12, 13, 14, 15}};

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

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

char country[]= “PAKISTAN”;

will be represented in computer memory as:

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: -

cin.get( ) function cin statement


char str[50]; char str[50];
cout << “Enter a string : “ ; cout << “Enter a string : “ ;
cin.get(str, 50); cin>>str ;
cout << “\n You typed : “ <<str ; cout << “\n You typed : “ <<str ;
output: output:
Enter a string : Time Table Enter a string : Time Table
You typed : Time Table You typed : Time
10. Differentiate between strcpy( ) and strcmp( ) functions.

Ans: -The differences between strcpy( ) and strcmp( ) functions are as follows.

strcpy( ) function strcmp( ) function


 strcpy( ) is used to copy the contents  strcmp( ) is used to compare two
of a string constant to another string strings character by character
variable.
 It returns a string constant as a  It can return an integer value 1, 0 or -
result. 1
 Its general syntax is:  Its general syntax is:
strcpy(str1, str2); strcmp(str1, str2);
 Example:  Example:
cout <<strcpy(str, “Abad”); cout <<strcmp(“Islam” , “Abad”);

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
7

11. Differentiate between strlen( ) and strcat( ) functions.

Ans: -The differences between strlen( ) and strcat( ) functions are as follows.

strlen( ) function strcat( ) function


 strlen( ) is used to find the length  strcmp( ) is used to join two strings
of a string. and append the contents of second
string to the end of first string.
 It returns an integer number as a  It returns string constant.
result.
 Its general syntax is:  Its general syntax is:
strlen(string); strcat(str1, str2);
 Example:  Example:
cout <<strlen(“Islam”); cout <<strcat(“Islam” , “abad”);
will produce will produce
5 Islamabad

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
8

Long Questions/Answers

1. What is an array? Explain one dimensional array with one example.

Ans: - Array: - 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. Array reduces program size, provides an easy way of
handling list of numbers or string and makes programming task simple and easy. Array has the
following general syntax.

dataType arrayName [arraySize] ;

or

dataType arrayName [rows][columns];

Examples: -

int arr[5];

int table[4][5];

arr[0] arr[1] arr[2] arr[3] arr[4]

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.

In C ++, array has two types.

 One Dimensional array


 Two Dimensional array

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.

The general syntax of one-dimensional array is:

dataType arrayName [arraySize] ;

 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

2. Explain two dimensional array in detail with one example.

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.

The general syntax of two-dimensional array is:

dataType arrayName [rows][columns] ;

 dataType is the type of data which will be stored in the array.


 arrayName can be any valid variable name.
 rows indicate the number of rows in array. It must be an unsigned integer constant.
 columns indicate the number of columns in array. It must be an unsigned integer constant.

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

cout<<”Enter a number = “; Enter a number = 5


cin>>a [i][j] ; Enter a number = 3
} Enter a number = 9
cout<<”\n ===Array===“<< endl ; Enter a number = 10
for (i = 1 ; i <= 3 ; i ++) // printing of array
Enter a number = 50
{
Enter a number = 7
for (j = 1 ; j <= 3 ; j ++)
Enter a number = 19
cout<<a [i][j] << ” \t “;
Enter a number = 60
endl ;
} ===Array===

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:

char stringName [stringSize];

 char is a keyword used to indicate the type of array used as string.


 stringName can be any valid variable name used as string.
 stringSize specifies the number of characters that can be stored in the string.

In C ++, strings can be defined in different ways.

Example- 1: -

char country[10] = ,‘P’, ‘A’, ‘K’, ‘I’, ‘S’, ‘T’, ‘A’, ‘N’-;

In computer memory the string variable country is represented as:

Index 0 1 2 3 4 5 6 7 8 9
country P A K I S T A N \0
Example- 2: -

char weekday[10] = “SUNDAY”;

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
11

In computer memory the string variable country is represented as:

Index 0 1 2 3 4 5 6 7 8 9
country S U N D A Y \0
Example- 3: -

char fruit[] = “Mango”;

In computer memory the string variable country is represented as:

Index 0 1 2 3 4 5
country M A N G o \0

4. Explain the purpose of the following string functions.


a) cin.get ( ) b) strcpy ( ) c) strcat ( )

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);

 strvar is the name of string variable.


 strsize is the maximum size of the string or character array.

Example: - The following program indicates the use of cin.get function.

# 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);

 string-1 is the string variable in which value of another string variable/constant is to be


copied.
 String-2 is the string variable/constant whose value is to be copied.

Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
12

Example: - The following program indicates the use of strcpy( ) function.

# 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.

Example: - The following program indicates the use of strcat( ) function.

# 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);

 string is a string variable/constant.

Example: - The following program indicates the use of strlen( ) function.

# 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);

strcmp( ) function returns an integer value based on the following criteria.


 It returns 0 if both strings are equal.
 It returns 1 if string-1 is greater than sting-2.
 It returns -1 if string-1 is less than string-2.
Example: - The following program indicates the use of strcmp( ) function.

# 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

cout<<“\n String-1 and String-2 are equal “;


else if (z == 0)
cout<<“\n String-1 is greater than String-2 “;
else
cout<<“\n String-1 is less than String-2 “;
getch ( );
}

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

for (i = 1 ; i <= 5 ; i ++) Enter a number = 3


{ Enter a number = 9
cout<<”Enter a number = “; Enter a number = 1
cin>>a [i] ;
Sum = 22
}
Average = 4.4
for (i = 1 ; i <= 5 ; i ++)
sum = sum + a[i];
avg = sum/5.0 ;
cout<<“\n Sum = “ <<sum<<endl ;
cout<< “Average = “ <<avg;
getch ( );
}

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

loc = 1; Enter a number = 71


for (i = 2 ; i <= 10 ; i ++) The smallest number = 18
if (a[i] <sm)
Index is = 7
{
sm = a[i];
loc = i;
}
cout<<“\n The smallest number = “<<sm<<endl;
cout<< “Index is = “<<loc;
getch ( );
}

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:-

Sum of all values = 807

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.

Ans: - # include <iostream.h>

# 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

Ans: - # include <iostream.h> 4 5

# 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

9. For the given array:


int arr[3]]4] = {{4, 18, -16, 11} ,

{-5, 10, -2, 12} ,

{15, -3, 17, 18}};

Write a program to find the sum of positive numbers.

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:-

Sum of positive numbers = 105

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] ;

int a[2]]4] = {{14, 8, 11, 10} , {15, 12, 20, 3}} ;

int b[2]]4] = {{2, 3, 4, 7} , {6, 7, 8, 9}} ;

for (i =0 ; i <= 1 ; i ++)


for (j =0 ; j <= 3 ; j ++)
c[i][j] = a[i][j] + b[i][j] ;
cout<<“Sum of two arrays is : “;
for (i =0 ; i <= 1 ; i ++)
{
for (j =0 ; j <= 3 ; j ++)
cout<< c[i][j] << “\t” ;
cout<<endl ;
}

getch ( );
}
Output:-

Sum of two arrays is :

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:-

clrscr ( ); Enter a number = 5


int i, j, sum, a[3]]3] ;
Enter a number = 8
sum = 0 ;
Enter a number = 2
for (i =0 ; i <= 2 ; i ++)
Enter a number = 9
for (j =0 ; j <= 2 ; j ++)
Enter a number = 6
{
cout<< “Enter a number = “ ; Enter a number = 1
cin>> a[i][j] ; Enter a number = 10
}
Enter a number = 4
cout<<“\n ===Array===” ;
Enter a number = 8
for (i =0 ; i <= 2 ; i ++)
{ ===Array===
for (j =0 ; j <= 2 ; j ++) 5 8 2
cout<<a[i][j] << “\t” ; 9 6 1
cout<<endl ;
10 4 8
}
for (i =0 ; i <= 2 ; i ++) Sum of row 1 = 15

{ 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:-

Enter string-1 : Tree


Enter string-2 : Mountain
Enter string-3 : Earth
Smallest string is : Tree

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: -

# include <iostream.h> Output:-


# include <conio.h> Enter a number = 41
void main (void)
Enter a number = 52
{
Enter a number = 34
clrscr ( );
Enter a number = 90
int i, j, sm, a[10];
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
}
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

cout<< “\n ==Ascending Order==“<<endl ; 71


for (i = 1 ; i <= 10 ; i ++) 78
cout<<a [i] <<endl ; 90
getch ( );
100
}

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

You might also like