BCSC 1102 INTRODUCTION to PROGRAMMING - Department of Computing and Mathematics
BCSC 1102 INTRODUCTION to PROGRAMMING - Department of Computing and Mathematics
INSTRUCTIONS:
Answer question ONE (compulsory) and any other TWO questions
c) Fix the errors in order to run the program and display the value of m. (Indicate the line
number where the bug and and propose a solution)
1. include stdio
2. int main(void);
3. (
4. int= m
5. b = 10
6. m = 2b+100
7. print(%f\n", M);
8. Return0;
9. ) [6 Marks]
1
d) What is the output of the following program?
1. #include <stdio.h>
2. int main(void)
3. {
4. int a = 4, b = 3, c = 5;
5. printf("%d\n",(a < b) == !(c > b));
6. return 0;
7. } [2 marks]
e) Write a program that reads an integer and displays a message to indicate whether it is
even or odd. [2 marks]
g) Write a program that reads a student’s grade and displays its corresponding
description, as follows:
18–20 Excellent
16–18 Very Good
13–16 Good
10–13 Dangerous Zone
0–10 Need Help [8 Marks]
[10marks]
b. Define the structure type city with members: city name, country name, and
population. Write a program that uses this type to read the data of 80 cities and store
them in an array. Then, the program should read the name of a country and a number
and display the cities of that country whose population is greater than the input
number [10 Marks]
2
QUESTION THREE (20 MARKS)
a. What is the output of the following program?
1. #include <stdio.h>
2. int main(void)
3. {
4. unsigned int i = 10;
5. if((i >> 4) != 0)
6. printf("One\n");
7. else
8. printf("Two\n");
9. printf("%d\n", i);
10. i = 1;
11. if((i << 3) == 8)
12. printf("One\n");
13. else
14. printf("Two\n");
15. printf("%d\n", i);
16. return 0;
17. } [5 marks]
b. Write a function that takes as parameters two pointers to floats and swaps the values
they point to. (5 Marks)
c. Suppose you wish to give a loan to a person who is either over 21 years or has a bank
balance of Kes 90,000. The loan amount is half the bank balance. Write a program to
implement this. Do not use the IF or SWITCH STATEMENTS. (10 Marks)
3
f. Write a program that will give the following display. Use a nested for loop.
1x0=0
1x1=1
1x2=2
2x0=0
2x1=2
2x2=3
3x0=0
3x1=3
3x2=6 [10 marks]
h. Write a program that reads the name of a file and displays its second line. [15 marks]