MOI UNIVERSITY
OFFICE OF THE CHIEF ACADEMIC OFFICER
UNIVERSITY EXAMINATIONS
2010/2011 ACADEMIC YEAR
FIRST YEAR SECOND SEMESTER EXAMINATIONS
FOR THE DEGREE OF
BACHELOR OF TECHNOLOGY
IN
COMPUTER ENGINEERING
COURSE CODE: COE 162
COURSE TITLE: INTRODUCTION TO COMPUTER PROGRAMMING
DATE: TIME:
INSTRUCTIONS TO CANDIDATES
1. THIS PAPER CONTAINS SEVEN (7) QUESTIONS
2. QUESTION ONE (1) IS COMPULSORY. ATTEMPT ANY OTHER FOUR
QUESTIONS.
THIS PAPER CONSISTS OF (8) PRINTED PAGES
PLEASE TURN OVER
COE 162: Introduction to Programming Semester II Academic year: 2010/11
Question One (Compulsory)
a) Distinguish between the following programming concepts:
i) Objects and Operations
ii) Compilers and Interpreters
iii) Procedural and Non-Procedural languages
[3 marks]
b) How do the following characteristics improve the quality of a program?
i) Modularity
ii) Localization
iii) Delayed decisions
[3 marks]
c) Draw a flowchart to depict the following examination processing scenario:
i) The input consists of students mark in a given course;
ii) Complain if the mark is below 0 or above 100;
iii) Determine the grade from the supplied mark (>=70=A; <40 = C; otherwise B)
iv) Print grade
[4 marks]
c) Explain the output when you execute the following C code.
#include<stdio.h>
void main(){
int a=5;
{
int b=10;
++b;
++a;
{
int a=20;
++a;
a=++b;
}
++a;
++b;
printf("%d %d",a,b);
}
printf(" %d",a);
[3 marks]
d) i) Accounting software has to deal with money in amounts exceeding a trillion dollars
and yet keep the arithmetic accurate to the penny. What kind of number that C
supports would be adequate?
[1 mark]
ii) Write a simple C program that requests three floating point numbers, and prints the
sum and product of their square roots to three decimal places.
[4 marks]
2
COE 162: Introduction to Programming Semester II Academic year: 2010/11
Question Two
a) List four important features of C language.
[2 marks]
b) What is wrong with the variable declaration and assignment in the following programs?
i)
#include<stdio.h>
int main(){
int goto=5;
printf("%d",goto);
return 0;
}
[1 mark]
ii)
#include<stdio.h>
int main(){
int __BIG__ = 32;
int y;
y= __BIG__ && 8;
printf("%d",y);
return 0;
}
[2 marks]
c) By means of a simple illustration, show the difference between local variable and global
variable as used in C programming.
[4 marks]
d)
i) If n is an integer variable, what is the value of the expression n % 8 when n has the
value 24?
[1 mark]
ii) What is the value of the C expression 2 * 6 - 5 + 8 % 5
[1 mark]
iii) What will be printed when the following segment of code is executed as part of a
program?
int i, total ;
for (i = 1 ; i = 15 ;i = i + 1)
{ if ( i % 3 == 0)
{
printf("%d ", i) ;
}
}
printf("\n\n");
[2 marks]
3
COE 162: Introduction to Programming Semester II Academic year: 2010/11
Question Three
a) What is the difference between the expressions x++ and ++x?
[2 marks]
b) Modify the program to achieve the same effect with a conditional expression instead of
if….else statement.
#include <stdio.h>
main()
{
int n;
printf("Enter an integer: ");
scanf("%i", &n);
printf("\nYou entered %i", n);
if (n >= 0)
printf(", which isn't negative.\n");
else
printf(", which is negative.\n");
}
[4 marks]
c) A C program contains the following statements:
#include<stdio.h>
float a,b,c;
Write printf function for each of the following groups of variables or expressions, using
f-type conversion for each floating-point quantity.
i) a, b and c with a minimum field width of 5 characters per quantity, with no more than
three decimal places.
ii) Sqrt (a+b), abs(a-c), with a minimum field width of 8 characters for the first quantity
and 6 characters for the second. Display a maximum of four decimal places for each
quantity.
iii) (a+b)+ (a-c), with a minimum field width of 7characters for the first quantity and 6
characters for the second.
[7 marks]
Question Four
a) Write a for loop that will compute the sum of every third integer beginning with i=5 (i.e,
calculate the sum 5+8+11+…) for all values of i less than 40.
[4 marks]
e) i) Clearly explain the difference between the while loop and do while loop.
[2 marks]
4
COE 162: Introduction to Programming Semester II Academic year: 2010/11
ii) Rewrite the following while loop as a do while loop.
#include <stdio.h>
main()
{
int m = 0, n;
puts("Enter a small integer (e.g. 10): ");
scanf("%i", &n);
while (m < n)
{
putchar('*');
m++;
if (m % 20 == 0) /* newline after every 20 */
putchar('\n');
}
putchar('\n');
}
[2 marks]
c) Write a program to print out all the prime numbers up to 50. Reminder: prime numbers
are integers greater than one with no factors other than themselves and one.
HINT: use the % operator to determine whether one number is a factor of another.
[5 marks]
Question Five
a) Explain the relationship between the data item represented by a variable v and the
corresponding variable pv.
[2 marks]
b) An implementation of pointers in C is shown below.
Void funct(int *p)
main()
{
Static int a[5]= (20,30,40,50,60);
.....
funct(a);
.....
}
Void funct(int *p)
{
int i, sum=0;
for(i=0; i<5;++i)
sum+=*(p+1);
printf(“sum=%d”,sum);
}
5
COE 162: Introduction to Programming Semester II Academic year: 2010/11
Determine:
i) The kind of argument passed to funct
[1 mark]
ii) The kind of information returned by funct
[1 mark]
iii) The value displayed by printf statement within funct
[2 marks]
c) i) Define the following array concepts: subscript, dimensionality of an array and passing
an array to a function by reference.
[3 marks]
ii) Suppose y is an array of integers, and we have just executed this code:
for(i=0;i<5;i++)
y[i] = i*i;
Suppose that y[0] is stored at address 3500. What is the value of each of the following
expressions?
&y[0]
y[1]
&y[1]
*(&y[2] +1)
[4 marks]
Question Six
a) Below is a sketchy outline of a C program.
#include<stdio.h>
main()
{
file *pt1, *pt2;
int x;
float y;
char z;
fpt= fopen(“[Link]”,”r”);
fpt=fopen(‘[Link]”,”w”);
.........
fclose (pt1);
fclose(pt2);
}
Write C statements to do the following:
i) Read the values of x,y and z from the file [Link]
[1 mark]
ii) Display each value on the screen and enter an updated value.
[2 marks]
iii) Write the new values to the data file [Link]
[1 mark]
6
COE 162: Introduction to Programming Semester II Academic year: 2010/11
b) Correct the following code, if necessary, to remove run-time or compile-time errors.
Change the code as little as possible. If no change is needed, just say so.
void main(void)
{ char x[3];
strcpy(x,"dog");
}
[2 marks]
c) i) Explain the difference between predefined functions and user defined functions.
[2 marks]
ii) Suppose we have a user defined function, long square (long), which returns the
square of a number, show how you would go about its declaration, calling and
definition.
Question Seven
a) With the aid of an example, give a comprehensive definition of a structure and clearly
show its necessity and use in C programs.
[3 marks]
b) Explain why the following code, when executed, prints 420.
#include<stdio.h>
#define init_employee(X,Y) {(X),(Y),wage_emp}
typedef struct Employee Em;
struct Employee {int hours,salary;int (*wage)(Em*);};
int wage_emp(Em *ths) {return ths->hours*ths->salary;}
#define init_manager(X,Y,Z) {(X),(Y),wage_man,(Z)}
typedef struct Manager Mn;
struct Manager {int hours,salary;int (*wage)(Mn*);int bonus;};
int wage_man(Mn *ths) {return ths->hours*ths->salary+ths->bonus;}
int main(void) {
Mn m = init_manager(40,10,20);
Em *e= (Em *) &m;
printf("%d\n",e->wage(e));
return 0;
}
[4 marks]
c) i) Explain how a union differs from a structure.
[2 marks]
ii) Consider the following C program:
#include <stdio.h>
main ()
{
union id {
char name[40];
int number;
};
struct {
int salary;
union id description;
7
COE 162: Introduction to Programming Semester II Academic year: 2010/11
} student,faculty;
printf("%d\n", sizeof(union id));
[Link] ="Sam";
printf("%s %d\n",
[Link],[Link]);
[Link] = 12;
printf("%s %d\n",
[Link],[Link]);
Determine the output displayed by the first, second and third printf() statements.
[4 marks]