KIMATHI UNIVERSITY COLLEGE OF TECHNOLOGY
YEAR TWO SEMESTER I EXAMINATION FOR THE DEGREE OF BACHELOR OF
SCIENCE MECHANICAL ENGINEERING / MECHATRONIC ENGINEERING
SMA 2175/ ICS 2175 COMPUTER PROGRAMMING I
DATE: TIME: 2 HRS
Instructions: Answer Question 1 and Any Other Two.
QUESTION ONE: COMPULSORY (30 MARKS)
a) Explain the two main preprocessor directives used in C language. Give examples of how
each is used. (6 Marks)
b) Using a diagram briefly highlight key features of the C Compilation model.(4 Marks)
c) Explain the following``character escapes''. (6 Marks)
i. \n
ii. \b
iii. \r
iv. \'
v. \"
vi. \\
d) What is the difference between ‘while’ and ‘do-while’ statement. (4 Marks)
e) Declare x to be an array of 1000 integers. (2 Marks)
f) The roots are given by the quadratic formula
Write a program that accepts input and produces the result of ‘x’. x>0 being real roots;
x=0 being repeated roots and x<0 being imaginary roots. ( 8 Marks)
QUESTION TWO [20 MARKS]
a) Giving examples, define the following terms as used in programming. (6 Marks)
i. Variable
ii. Data type
iii. Constant
b) Write a program to find the largest of three numbers. (6 Marks)
c) Write a Program to Find Value of sin(x) using Expansion Series Given Below:
sin(x) = x - x3/3! + x5/5! - x7/7!........ (8 Marks)
QUESTION THREE [20 MARKS]
a) Write a program to print Fibonacci series using recursion. (13 Marks)
b) Write a Program to Print the Numbers, Which are Divisible by 3 and 5 from
First 100 Natural Numbers (7 Marks)
QUESTION FOUR [20 MARKS]
a) What will be printed by the following program? (6 Marks)
void f(int *x)
{
*x = *x+1;
}
void main(void)
{
int x = 5;
f(&x);
printf("\%d",x);
}
b) Write a program that uses a function to get the factorial N (N!).
Where N! = 1 ´ 2 ´ 3 ´ …… N . (8 Marks)
c) Draw a flowchart for computing factorial N (N!).
Where N! = 1 ´ 2 ´ 3 ´ …… N . (6 Marks)
QUESTION FIVE [20 MARKS]
a) Explain the following functions in the string.h library: (4 Marks)
i. char* strcpy(char* dest, const char* src);
ii. char* strncpy(char* dest, char* src, int n);
b) Write a Program that Reads a text letter-by-letter, store it into an array and then print it
letter-by-letter. (8 Marks)
c) What is a pointer? (2 Marks)
d) What will be printed by the following program? (6 Marks)
#include <stdio.h>
#include <stdlib.h>
void main()
int x, *p;
p = &x; /* initialise pointer */
*p = 0; /* set x to zero */
printf("x is %d\n", x);
printf("*p is %d\n", *p);
*p += 1; /* increment what p points to */
printf("x is %d\n", x);
(*p)++; /* increment what p points to */
printf("x is %d\n", x);
exit(EXIT_SUCCESS);