Mid 2011 Solutions
Mid 2011 Solutions
Mid 2011 Solutions
Date: .FN / AN
Time: 2 hrs
Full marks: 60
Instructions:
Write answers in the space provided in the question paper itself.
Do your rough work on the space provided at in the question paper.
Write your roll number at the space provided in every sheet of the question paper.
Name:
Roll No:
Section:
MARKS:
Q1 (15)
Q2 (15)
Q3 (15)
Q4 (15)
Total (60)
Roll No:______________________
1.
[5 3 = 15 marks]
What will be the contents of array x after the following code segment is
executed?
int x[]={1,2,3,4,5,6,7};
int i,j;
i=4; j=3;
x[i] = i++;
x[--j] = --i + 4;
ANS 1(a):
x[] = {1, 2, 8, 4, 4, 6, 7}
21
if e1 then { if e2
then s1
else s2
}
else { if e3
then s3
else s4
}
THERE WAS A TYPO; a then was missing
10 15 20 25 30
5 5 5 5 5
a=5, x=5
rpaaKT
Roll No:______________________
6
4
------------------------------------------------- END OF QUESTION 1 -----------------------------------------------REST OF THIS PAGE MAY BE USED FOR ROUGH WORK
#include <stdio.h>
main()
{
float sum, term, x;
int n;
n = 1;
sum = 0.0;
term = x;
while (fabs(term) > 0.00001)
{
sum = sum + term;
n++;
term = -term * x / n;
}
printf (\nSum of series: %f, sum);
}
Roll No:______________________
ANS 2(b):
#include <stdio.h>
main()
{
int N, i, j;
printf (\nEnter value of N: );
scanf (%d, &N);
for (i=0; i<N; i++)
{
printf (\n);
for (j=0; j<=i; j++)
printf (* );
}
}
16 21
3. Write a C program in the following way. The function int main() reads a
positive integer n, 1 n 1000, and then it reads n positive integers in a onedimensional array of type int. It also prints the input data. It calls the function:
void copf(int data[], int n, int cpf[])
where the input data is passed through the first parameter. The second parameter is
the number of data n.
Values in the array corresponding to the third parameter is computed by the function
copf( ). On execution of this function, cpf[i] will contain the number of
distinct prime factors of data[i], for all i, 0 i < n. As an example, if data[2]
is 140 = 22 5 7, then cpf[2] will contain 3.
Finally main()prints the content of the array which is passed as the third parameter
to the function copf().
The function int pc(int k) returns the count of distinct prime factors of its
argument k. For example, if k is 140, it returns 3. The function copf() calls the
function int pc(int k) with every element of data[i] to get the
corresponding count of distinct prime factors.
a) Write the function int main().
b) Write the function
void copf(int data[], int n, int cpf[]).
c) Write C expressions corresponding to 1, 2, 3, 4, 5 in the function pc()
int pc(int k){
int num, 1 ;
for(2; 3 ; ++num){
if (k%num == 0) {
++ count;
while (4) 5 ;
}
}
return count;
}
[3 5 = 15 marks]
Roll No:______________________
[3 5 = 15 marks]
ANS 3(a):
#include <stdio.h>
#define SIZE 1000
void copf(int [], int, int []);
int pc(int);
int main()
{
int n, data[SIZE], i, cpf[SIZE];
printf("Enter a +ve integer: "); scanf("%d", &n);
printf("Enter %d +ve integers:\n", n);
for(i=0; i<n; ++i) scanf("%d", &data[i]);
printf("Input data:\n");
for(i=0; i<n; ++i) printf("%d ", data[i]);
putchar('\n');
copf(data, n, cpf);
printf("Corresponding prime counts are:\n");
for(i=0; i<n; ++i) printf("%d ", cpf[i]);
putchar('\n');
return 0;
}
ANS 3(b):
ANS 3(c):
1:_____count = 0______________
5:______k /= num_______________
4.
a) A country has coins of denomination 3, 5 and 10 respectively. The following
function returns 1 if it is not possible to pay a value of k using these coins.
Otherwise it returns the minimum number of coins needed to make the
payment.
For example, canchange(7) will return -1. On the other hand,
canchange(14) will return 4 because 14 can be paid as 3+3+3+5 and there
is no other way to pay with fewer coins.
Write C expressions corresponding to 1, 2, 3, 4, 5, 6, 7 in the following
function.
int canchange(int k)
{
int a= -1;
if (k==0) return 0;
if ( 1 ) return 1;
if (k < 3) 2 ;
a = canchange( 3 ); if (a > 0) return 4 ;
a = canchange(k 5); if (a > 0) return 5 ;
a = canchange( 6 ); if (a > 0) return 7 ;
return -1;
}
b) Modify the function of part (a) to write a function to print the change. For
example, if we call the function printchange(14) it should print
3+3+3+5. The function prototype is:
int printchange(int k)
[7 +8 = 15 marks]
Roll No:______________________
ANS 4(a):
3:___k 10 _________________
4:___a+1____________________
5:___a+1____________________
6:___ k 3___________________
7:___ a+1____________________
ANS 4(b):
int printchange(int k)
{
int a= -1;
if (k==0) return 0;
if ((k ==3)||(k == 5)||(k == 10))
{ printf("%d", k); return 1; }
if (k < 3) return -1;
a = printchange(k -10); if (a > 0)
{ printf("+10"); return a+1; }
a = printchange(k - 5); if (a > 0)
{ printf("+5"); return a+1; }
a = printchange(k-3); if (a > 0)
{ printf("+3"); return a+1; }
return -1;
}