You are on page 1of 4

WORKSHEET – 1 – REVIEW OF C++ - KEY

1a Find the correct identifiers out of the following, which can be used for naming
) Variable, Constants or Functions in a C++ program :
For, while, INT, NeW, delete, 1stName, Add+Subtract, name
Ans: For, INT, NeW, name
b Differentiate between #define and const. Explain #define macro with example.
#define is a preprocessor directive used to define constants and macros within
the program.
Const is a keyword applied to a data type that indicates that the data is read only.
Macros are brief abbreviations for longer constructs
Macro Example
#define sum(a+b) a+b
c Write the related library function name based upon the given information in C++.
(i) Get single character using keyboard. This function is available in stdio.h file.
getchar()
(ii) To check whether given character is alpha numeric character or not. This
function is available in ctype.h file.
isalnum()
d Write the name of the function and the header file required for the following
task.
* to check for upper case character. isupper()
* to find the remainder for float value. fmod()
e Rewrite the following program after removing the syntactical errors (if any).
Underline each correction.
#include[iostream.h]
typedef char Text(80) ;
void main ( )
{ Text T= "Indian";
int Count=strlen(T) ;
cout<<T<<'has'<<Count<< 'characters' <<endl;
}
#include<iostream.h>
#include <string.h>
typedef char Text[80] ;
void main ( )
{ Text T= "Indian";
int Count=strlen(T) ;
cout<<T<<”has”<<Count<< “characters” <<endl;
}
f Jayneel has just started learning C++. He typed the following C++ code and
during the compilation of the code, he got some errors. When Jayneel asked
his teacher, the teacher told him to include necessary header files in the code.
Write the names of those header files, which Jayneel needs to include, for
successful compilation and execution of the following code.
void main()
{ cout <<"We win !"<<endl;
cout <<strlen("The World")<<endl ; }
string.h,iostream.h
g Write the output of the following program:
#include<iostream.h>
#include<string.h>
#include<ctype.h>
void swap(char &c1, char &c2)
{
char temp;
temp = c1;
c1 = c2;
c2 = temp;
}
void update(char str[])
{
int k, j, l1, l2;
l1 = (strlen(str) + 1)/2;
l2 = strlen(str);
for(k = 0, j = l1 – 1; k<j; k++, j- -)
{
if(islower(str[k]))
swap(str[k], str[j]);
}
for(k = l1, j = l2 – 1; k<j; k++, j- -)
{
if(isupper(str[k]))
swap(str[k], str[j]);
}
}
void main( )
{
char data[100] = {“bEsTOfLUck”};
cout<< “Original Data:”<<data<<endl;
update(data);
cout<< “Updated Data:”<<data;
}
Answer:
Original Data: bEsTOfLUck
Updated Data: OEsTbfcULK

h Write the program to find the maximum and minimum element in one
dimensional array
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int A[]={10,13,45,9,8,2,45},i,n,large,small;
n=sizeof(A)/sizeof(int);
cout<<n;
large=small=A[0];
for(i=0;i<n;i++)
if(large<A[i])
large=A[i];
if(small>A[i])
small=A[i];
cout<<"\nLargest "<<large<<" Smallest "<<small<<endl;
}
i Write a C++ function TWINPRIME( ) which takes the array of integers and the
no. of integers as an argument and display the twin prime numbers from the
array.
Twin prime numbers are the two consecutive prime numbers whose difference is
two.
Example. If the array is 3,5,6,8,11,13,14,17
Then the function should display 3,5,11,13
void twinprime(int A[], int n)
{
int i,j,fl1=0,fl2=0;
for(i=0;i<n;i++)
{ for(fl1=0,j=2;j<A[i];j++)
if(A[i]%j==0) break;
if(j==A[i]) fl1=1;
for(fl2=0,j=2;j<A[i+1];j++)
if(A[i+1]%j==0) break;
if(j==A[i+1]) fl2=1;
if(fl1==1&&fl2==1&&abs(A[i]-A[i+1])==2)
{cout<<A[i]<<" "<<A[i+1]<<" ";
i++;
} }}
j Write a function to display all the elements and their sum of the two
dimensional array (i.e. A[6][6]) which are stored at the odd row number and
even column number. 2
For example, if the array is:
int A[6][6] = {5,6,3,2,7,3
1,2,4,9,4,2
2,5,8,1,6,3
9,7,5,8,-2,4
4,5,2,5,1,-3
-3,-6,-5,-2,2,1};
Output:
The numbers are : 1 4 4 9 5 -2 -3 -5 2
The sum is 15
k Write a function which takes a 2-d array as an argument and display all the
numbers from the array whose starting and ending digit is the same.
Example.if the array is
121 11 213
272 44 527
389 22 6236

then the function should display 121,11,272,44,22,6236


void samedigit(int A[][3], int r,int c)
{
int i,j,k,rem,first,last;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{for(int p=-1,k=A[i][j];k>0;k/=10,p++);
first=A[i][j]/pow(10,p);
last=A[i][j]%10;
if(first==last)
cout<<A[i][j]<<" ";
}}}

You might also like