Sei sulla pagina 1di 8

PANDIT DEENDAYAL PETROLEUM UNIVERSITY

INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR


B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____

DATE: 19-09-2008 TIME: 9.30 A.M. TO 11.30 A.M. TOTAL MARKS: 50

Instructions:
1. Do Not Repeat Questions.
2. Answer New Question on New Page.
3. Q.1 to Q.8 are compulsory.
4. Q.9 is bonus question of 5 marks.

Q.1. State whether the following statements are true or false. [5]
A) Array occupies contiguous memory locations. [T]
B) In c/c++, all arrays have 0 as the index of their first element. [T]
C) Keywords can be used as variable names. [F]
D) ASCII-7 uses leftmost 7 bits of a byte. [F] RIGHT-MOST 7 BITS
E) BCD is unable to store 26 lower case alphabets. [T]
F) You can’t add an integer number to a pointer. [F]
G) char name[] = “PDPU-IPTG” occupies 9 bytes in the memory. [F] 10 BYTES
H) strlen() returns length of a string. [T]
I) strcmp() returns 0 if both strings are equal. [T]
J) while(1); is an infinite loop statement. [T]

Q.2. Fill in the Gaps. [5]


A) INDEX / Pointer is used to access a specific element in the array.
B) The format string for unsigned int is _%u / cout << (unsigned int) …__.
C) _\r__ escape sequence character puts the cursor at the 1st position in the line.
D) do…while is an exit-controlled loop structure.
E) ( ) is used to override precedence rules.

Q.3. Do the following: (show working also.) [5]


A) (1110111)2 + (1010101)2 = ( ? )10
1110111
+1010101
11001100

= (1 * 27) + (1 * 26 ) + ( 0 * 25 ) + ( 0 * 24) + (1 * 23) + ( 1 * 22 ) + ( 0 * 21 ) + ( 0 * 20)


= 128 + 64 + 0 + 0 + 8 + 4 + 0 + 0
= (204)10

B) (AB.CD)16 = ( ?) 8
Step 1 : Conversion from Hexa Decimal to Binary
Grouping into 4 Bits & comparing with 8421, we get
A = 1010 B = 1011 C = 1100 D = 1101
( 10101011.11001101)2
Step 2 : Conversion from Binary to Octal
Grouping into 3 bits & comparing with 421, we get
( 010 101 011.110 011 010 )2
( 2 5 3 . 6 3 2 )8

Page 1 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____

C) (705.25)8 = ( ? ) 2
= 7 0 5 . 2 5
Comparing with 421, we get
= (111 000 101 . 010 101) 2

D) Convert (-105)10 into 2's complement using 9 bits.

-28 27 26 25 24 23 22 21 20
-256 128 64 32 16 8 4 2 1
1 1 0 0 1 0 1 1 1

E) Convert (-20.0625)10 into IEEE 754 standard (32 bits)

The sign is negative. Therefore the sign bit will be 1.

Converting 20.0625 into binary, we get 10100.0001

Normalizing the number, we get 1.01000001 * 24

The first binary digit is dropped. Fraction part on the right is filled with 0s on right-hand side
upto 23 bits, we get: 01000001000000000000000

The exponent is 4. Adding 127 bias in it, we get 131. Converting 131 in binary,
we get: 10000011

Sign Bit Exponent Mantissa


31 30 23 22 0
1 10000011 01000001000000000000000

Q. 4. Write the output for the following segment of the program. [5]
a) int i = 4 , j = 5; b) int a = 10, * b = &a, **c = &b;
if (++i > j - -) **c = *b + 10; c++;
cout << i << “ “ << j ; cout << *c;
cout << i << “ “ << j ; (if addresses of a ,b and c are 65524,
65522 & 65520 respectively.)
(Assume 16-bit compiler)

5 4 20
Explanation: Explanation:
In if condition, ++i will make value of i to 5. Here, b & c are integer pointers.
This value will be checked with value of b is pointing to a, while c contains
j, which is 5. The condition result is false. address of b i.e. 65522. When we
Thereafter, value of j is reduced by 1. execute c++, the value of c gets
Hence, the above output. changed to 65524. This is the address
of a. So *c will give value 20 as output.
Remember, **c and *b are pointing to
Variable a only. So value of a gets
changed to 20 by statement **c=*b+10;

Page 2 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____

c) int p = 30 , c = 80, m = 90, a;


a = ( p + c + m ) / 3;
cout << (p < 35 || c < 35 || m < 35)?”Fail”:(a>=70)?”Distinction”: (a>=60)?”First Class”:
(a>=50)?”Second Class”:”Third Class”;

Fail
Explanation: As p contains 30, 1st block of conditions becomes true.

d) int i, j, k; e) int a[5], i;


for(i = 1 ; i <= 5 ; i++) a[0] = 1;
{ for(i=1;i<=3;i++)
k = 0; {
for(j=1; j <= i; j++) a[i] = a[i-1] * 10;
{ cout << a[i] << endl;
cout << (( k = = 0)?1:0); }
k++;
}
cout << endl;
}

1 10
10 100
100 1000
1000
10000

Q.5. Find out 10 errors in the following segment of the program & rewrite it. [5]
main( ) Dbl(int x, int y);
{ {
int a = 10; b = 20; int x;
dbl(&a, &b); x = x + x;
cout >> a >> “ “ >> b; y = y + y;
} }

Answer:
void main( ) void dbl(int * x, int * y) remove ;
{ {
int a = 10, b = 20; int x;
void dbl(int *, int *); *x = *x + *x;
dbl(&a, &b); *y = *y + *y;
cout << a << “ “ << b; }
}

Page 3 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____

Q.6. Write algorithm for the following. [10]


Find out the integer number whose factorial is given to us. e.g. if user enters 120  the
output should be 5. Give appropriate message, if you are unable to find the exact value.

INPUT N
T=N
y
IF N = 1  PRINT 0, “!”®
n|
V
I=0
© I = I + 1
N=N/I
y
IF N > 1  ©
n|
V y
IF N = 1  PRINT I, “ IS FACTORIAL OF “ , T ®
n|
V
PRINT “FACTORIAL OF “ , T , “ NOT FOUND “
® STOP

Or
Q.6. Draw flowchart to calculate the value of a raised to b. (ab) [10]
Where b should be a positive integer only. Reenter the value of b, if it is not
positive.

Page 4 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____

START

INPUT A

INPUT B

y
IF
B <= 0

ANS = 1

I=0

I=I+1

ANS = ANS * A

y
IF
I<B

n
PRINT ANS

STOP

Page 5 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____
Q.7. Write C/C++ program for the following. [10]
A) Summation of digits of a given no. e.g. 351 = 3+5+1 = 9

#include <iostream.h>
#include <conio.h>
void main()
{
int n;
int sumofdigit(int); // function prototype
cout << “Enter a Number” << endl;
cin >> n;
cout << sumofdigit(n);
getch();
}

int sumofdigit(int n)
{
int q,r,s = 0;
do
{
q = n / 10;
r = n % 10;
s += r;
n = q;
} while ( n );
return s;
}

B) Find out largest and smallest out of 100 different nos.

#include <iostream.h>
#include <conio.h>
void main()
{
int i,v,h,l;
cout << “Enter a Number “ << endl;
cin >> v;
// assuming the 1st no. entered is highest as well as lowest.
h = v;
l = v;
for(i=2;i<=100;i++)
{
cout << “Enter a Number “ << endl;
cin >> v;
if (v>h)

Page 6 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____
v = h;
else
if ( v < l )
l = v;
}
cout << “Highest Value = “ << h << endl;
cout << “Lowest Value = “ << l << endl;
getch();
}

Q.8. Write short note on the following. [5]


A) printf ( ) B) Header Files

Please go through the presentation “c++ basic”

Q.9. Bonus Question: [5]


Write a complete C/C++ program to remove a particular number from the array having 10
elements.
e.g. array contains { 1 , 3, 5, 3, 2, 4 ,6, 3, 2, 7 }. After removing 3 from the array, the array
should look like {1, 5, 2, 4, 6, 2, 7, 0, 0, 0}.
Ask the contents of the array and the number to be removed from the user.

#include <iostream.h>
#include <conio.h>

void main()
{
int a[10],i,j,n;
clrscr();
// Accept 10 values & no. to be deleted from the user.
cout << "Enter 10 values" << endl;
for(i=0;i<10;i++)
{
cin >> a[i];
}
cout << "Enter the Number to be removed from the array " ;
cin >> n;

// Remove the number.


for(i=0;i<10;i++)
{
if ( n == a[i])
{
for(j = i; j< 9; j++)
{
a[j] = a[j+1];

Page 7 of 8
PANDIT DEENDAYAL PETROLEUM UNIVERSITY
INSTITUTE OF PETROLEUM TECHNOLOGY, GANDHINAGAR
B.TECH 2008-MID-SEMESTER EXAM COMPUTER PROGRAMMING (ESC101)
Roll No. _____ Batch No. ____
}
a[9] = 0; // at the end of the list, put zero.
}
}

// No. is removed, so now print it on the monitor.


cout << "After Removing No. " << n << " From the Array : " <<
endl;
for(i=0;i<10;i++)
{
cout << a[i]<< " ";
}
getch();
}

Page 8 of 8

Potrebbero piacerti anche