[go: up one dir, main page]

0% found this document useful (0 votes)
9 views4 pages

CT1 Sol

The document outlines the structure and content of Class Test 1 for CS10003 Programming and Data Structures for Spring 2024-2025, scheduled for January 30, 2025. It includes programming tasks that require students to fill in blanks in C code to solve specific problems related to powers of two, line equations, sums of squares, and digit counting. Each task is designed to evaluate students' understanding of programming concepts and their ability to apply mathematical functions in C.

Uploaded by

jaasritha0210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

CT1 Sol

The document outlines the structure and content of Class Test 1 for CS10003 Programming and Data Structures for Spring 2024-2025, scheduled for January 30, 2025. It includes programming tasks that require students to fill in blanks in C code to solve specific problems related to powers of two, line equations, sums of squares, and digit counting. Each task is designed to evaluate students' understanding of programming concepts and their ability to apply mathematical functions in C.

Uploaded by

jaasritha0210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS10003 Programming and Data Structures

Spring 2024 – 2025


Class Test 1
6:45pm – 7:45pm January 30, 2025 Maximum Marks: 40
_______________________________________________________________________________________

[Write your answers in the question paper itself. Answer all questions. All programs must be written in C.
Fill in the blanks in the following codes to make them work as described. Do not use extra variables.
Not all blanks carry equal marks. Evaluation will depend on overall performance.]

1. The following program reads a positive integer n from the user, and computes the smallest power of
two, that is greater than or equal to n, that is, it computes p = 2e such that p / 2 < n ⩽ p. The program
should use math library functions to do this. The program must not contain any loop. Instead it
should first compute e as log2 n truncated to an integer. It should then compute p = 2e. If p ⩾ n, then
we are done. Otherwise, the above value of p is updated to the correct value. Fill in the blanks below
so that the program behaves as mentioned above.
[10]

#include <stdio.h>

#include <math.h>

int main ( )
{
int n, e, p;

printf("Enter a positive integer: "); scanf("%d", &n);

/* First compute e */

e = log(n) / log(2) [or log10(n) / log10(2)] ;

/* Then compute 2-to-the-power-e as p */

p = pow(2,e) ;

/* Adjust p if it is smaller than n */

if ( p < n ) p = p * 2 ;

printf("Smallest power of 2 greater than or equal to %d is %d\n", n, p);


return 0;
}

Space for Rough Work

Page 1 of 4
2. The following program is meant for computing the equation of the straight line passing through two
points P1 = (x1, y1) and P2 = (x2, y2). The equation of the line is to be specified in the form y = mx + c.
Two special cases need to be handled. First, if P1 and P2 are the same point, then this problem cannot
be solved. Second, a vertical line cannot be written in the above form, and is instead printed in the
form x = c. Assume that the coordinates x1, y1, x2, y2 are integers. However, m and c computed as
above need not be integers. We use the floating-point data type double to store these values. Fill in
the blanks below so that the program works as intended.
[10]

#include <stdio.h>

int main ( )
{
int x1, y1, x2, y2;
double m, c;

printf("First point: "); scanf(" %d%d ", &x1, &y1);

printf("Second point: "); scanf(" %d%d ", &x2, &y2);

if ( x1 == x2 ) {

if ( y1 == y2 ) printf("The two points are the same\n");

else {

c = x1 ;

printf("Equation of the line is x = %lf \n", c);


}

} else {

m = (double)(y2 – y1) / (double)(x2 – x1) ;

c = y1 – m * x1 [or y2 – m * x2] ;

printf("Equation of the line is y = %lf x + %lf \n", m, c);


}
return 0;
}

Space for Rough Work

Page 2 of 4
3. The following program reads a positive integer n from the user, and attempts to express n as the sum
of two squares, that is, as n = a2 + b2, where a and b are positive integers with a ⩽ b. This inequality
implies that a is no larger than the truncated integer value of √ n / 2. Call this maximum value of a as
amax. The program carries out a search for a in the sequence 1, 2, 3, ⋯, amax. For each a, an integer b
is computed as the truncated value of √ n – a2 . If these integers a and b satisfy n = a2 + b2, then the
search loop is broken. If none of the values of a in the above range can produce a suitable b, then
also the loop is broken. At the end, the program prints its decision (failure or the values a and b). Fill
in the blanks below to make the program work as explained above. Write only the main() function.
Use math library functions whenever needed.
[10]

int main ( )
{
int n, a, b, amax;

printf("Enter a positive integer: "); scanf("%d", &n);

amax = sqrt(n / 2) ;

/* for loop on a in the range mentioned above */

for ( a = 1 ; a <= amax ; ++a ) {

b = sqrt(n – a * a) ;

/* Break if a desired b is found */

if ( n == a * a + b * b ) break;
}

if ( a > amax )
printf("%d cannot be written as the sum of two squares\n", n);
else
printf("%d = %d^2 + %d^2\n", n, a, b);

return 0;
}

Space for rough work

Page 3 of 4
4. The following program reads a positive integer n from the user. It then computes and prints the
number of times each digit (decimal) appears in n. For example, if n = 250168005, then the digit 0
appears three times, the digits 1, 2, 6, and 8 once each, and the digit 5 twice. The digits 3, 4, 7, and 9
do not appear in this n. Notice that leading 0 digits should not be counted. For example, if the user
enters 00250168005 as n, the digit 0 will still be reported to have appeared three times in n. You do
not have to do anything special to ensure this, because scanf will ignore the leading zero digits while
storing n. Fill in the blanks in the code below to perform the task mentioned above. Write only the
main() function. Do not use math library functions.
[10]
int main ()
{
int num, n;
int digit, count;

printf("Enter a positive integer: "); scanf("%d", &num);

/* Loop over digit in the range 0 ... 9 */

for ( digit = 0; digit <= 9; digit++ ) {

n = num ; /* Store a copy of num */

count = 0 ; /* Initialize count */

/* Loop so long as all the digits of n are not considered */

while ( n > 0 ) {

if ( n % 10 == digit ) ++count;

n = n / 10 ; /* Update n for the next iteration */

}
printf("Digit %d occurred %d times\n", digit, count);
}

return 0;
}

Space for rough work

Page 4 of 4

You might also like