Ch#5 Array & StringsComputer.s (HSSC-II)
Ch#5 Array & StringsComputer.s (HSSC-II)
Answer Keys:
MCQ.s
Prepared By:
Mr. Usman (SS)
Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699
Ch # 5
Array & Strings
Short Questions & Answers
Exercise
4. 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:
# include <iostream>
using namespace std;
int main( )
{
int x[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
cout<< “Values are:” << endl;
for( int i=0; i<3; i++)
{
for( int j=0; j<5; j++)
{
cout<< “x*“ <<i<<”+*“<<j<<”+” ;
cout<< x[i][j]<<endl;
}
}
Index 0 1 2 3 4 5 6 7 8 9
Variable
S u n d a y \0
The index start from zero. The compiler automatically places the null character (\0) after the
last character. The remaining three characters are not defined.
6. What is the advantages of using cin.get( ) function over cin statement for reading a
string?
Ans:
The advantage of cin.get( ) function over cin statement for reading a string is
that cin.get( ) function reads a whole line then consumes the end-of-line character so
that repeating this will read the next line. Whereas, cin statement consider a space as
terminating character. The cin statement reads strings that consist of a single word.
Anything typed after a space is ignored.
Strcpy( ) Strcmp( )
The strcpy( ) functions is used to copy The strcmp( ) function compares two
contents of a string variable or string strings and returns an integer value based
constant to another string variable. on the result of comparison. This
comparison is based on ASCII codes of
characters.
End Exercise
Prepared By: Mr. Usman (SS) Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699
Additional Short Questions & Answers
1. Why array used in program?
Ans: Array allows programmer to use a single variable name to represent a collection of same
type of data. This reduces the program size, provides an easy way of handling list of numbers or
strinns and makes computer-programming task simple and easy way.
2. Define array.
Ans: An array is a collection of same type of elements stored in contiguous memory locations.
Syntax:
datatype arrayname[rowsize][columnsize];
12. What do you know about null character? OR Briefly describe null character.
Ans: All the strings end with a special character, known as null character and it is represented by
'\0'. The null character is automatically appended at the end of string.
R A M \0
29. Write a program in C++, which accepts the user's first and last name and print them in reverse
order with a space between them.
Ans: Program:
# include<iostream>
# include<string>
using namespace std;
int main( )
{
char fname[30], lname[30];
cout<<" Input first name: " ;
cin>> fname;
cout<<" Input last name: " ;
cin>> lname;
cout<< " Name in reverse order is: " << lname << " " << fname<< endl;
cout<< endl;
retutn 0;
}
# include<iostream>
using namespace std;
int main( )
{
string str = " Tech study" ;
// You can also use str.length( )
cout<< "" String length = " << str.size( );
return 0 ;
}
# include<iostream>
# include<string.h>
using namespace std;
int main( )
{
char str1[1000], str2[1000];
cout<< " Enter the first string" << endl;
cin>> str1;
cout<< " Enter the second string" << endl;
cin>> str2;
if (strcmp(str1, str2) == 0)
else
Prepared By:
Mr. Usman (SS)
Army College Mailsi Campus
Cell # 0301-2156545 , 0349-3036699