Fundamentals of Computing and Programming MS 2023-24

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Fundamentals of Computing and Programming

Mid Term Examination. Maximum score 40, Total marks 43. Time Limit 2 hrs.
September 21, 2023

1. Answer each of these briefly as instructed: [4x2=8]

(a) What does the following print:


printf("%d %c", ’e’ - ’a’, ’a’+ 4);
// prints an integer and a character
Explain your answer in one sentence.
(b) Draw a picture and show using arrow for pointer the relationship between the
array a[ ] and the variables p and q after the last statement:
int a[10]={0,1,2,3,4};
int *p, *q; // p and q are integer pointers
p = &a[3];
q = p-3;
(c) What does the following code print:
char name[20]="praggnananda";
printf("%s",name+6);
(d) What does the following code print:
char name[20]="praggnananda";
scanf("%s",name+6); // assume the input was "chess"
printf("%s",name);

2. Downify
The code below converts given upper case character value to lower case and returns
the converted lower case character: [5]

char downify(char a){


if ( __C__ ) { // check: if a is an upper case character
// then compute the lower case character
__S__ // and return it
}
return a ; // otherwise just return the given character
}

3. Array Read/Print
Write a main() function to do the following: [5]
-Define an array named a of 10 integers.

1
-Read five integers into the array.
-After all integers are read, then print all the read integers from the array.
4. Array Sum
Write a function int sum(int a[ ], int n); [5]
the first parameter a[ ] is an array, the second parameters says how many integers
are in the array. The function simply finds the sum of all the n elements of a[ ] and
returns that value.
5. Flying birds
Write a function called fly() . It has one parameter named s which is a string. [5]
- It returns 0 or 1 or -1 as defined below:
- It returns 1 if the string is ”sparrow” or ”mynah”
- It returns 0 if the string is ”penguin”.
- It returns -1 if it is none of the above
Note: You can use the following C standard library function to compare two strings:
int strcmp(char s1[], char s2[]);
It compares the strings in the two arrays s1 and s2 and returns 0 only if they are
identical, for example strcmp(a,"hello") will return 0 if the string in a is equal to
”hello”. strcmp(a,"bye"); will return a non-zero value if a has he string ”hello”.
6. Checking digits: [5]
Write a function kap(). It takes one integer parameter n. If n has in its units place:
4, 9 or 5 then it prints ”It is a kap” else it prints ”It is not a kap”.
Constraint: You must use a switch-case and NOT an if-else.
7. Searching for TC numbers [2+8=10]
A TC number is one which can be written as the sum of two-cubes in at least two
different ways (with positive integers). For example 152 = 33 +53 is not a TC number
because there is no other way to write it as a sum of two cubes. On the other hand,
1729 = 123 + 13 and 1729 = 103 + 93 , so 1729 is a TC number. This problem shows
how to find if a number is a TC number.
(a) Write a function with prototype:
int checkprod(int a, int b, int t);
It tests if a3 + b3 equals t. If yes, it returns 1, if not it returns 0. Remember that
C does not have an exponentiation operator, just repeated multiplies is the way you
can do it.
(b) Write another function with prototype:
int checktc(int n);
it checks if n is a TC number and returns 1 or 0 to say it is or it is not a TC number.
To do hat it tests every possible (i, j) integer pairs where i ≤ j. Here is how it works:
- Maintain a variable count, see below how it is used.
- Loop over each value of i from 1 to n − 1
- For a given i loop over each value of j from i to n − 1
- Call checkprod() with arguments i, j and n to check if i3 + j 3 equals n
- If it is then increment count;
- After checking every such i and j pair and coming out of the two loops, see if count
is greater than 1, if it is, then the function returns 1 else it returns 0.

You might also like