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

Computer Programming Exam Mark Sheet

Computer Programming exam mark sheet
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 views12 pages

Computer Programming Exam Mark Sheet

Computer Programming exam mark sheet
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/ 12

COLLEGE OF SCIENCE & TECHNOLOGY

SCHOOL OF ICT

Department of COMPUTER SCIENCE

Instruction to Module Leader


Tick/Highlight the campus where the module is delivered.
a. HUYE c. MUSANZE
b. NYARUGENGE

NOTE: For a common module highlight all the campuses where the module is delivered.
__________________________________________________________________________________

END OF SEMESTER EXAMINATIONS


ACADEMIC YEAR 2016 – 2017

YEAR OF STUDY SEMESTER :2 GROUP: CS, IS, & IT

MODULE CODE CSC 1261

MODULE TITLE : Computer Programming

DATE OF EXAM :

TOTAL DURATION : 02 HOURS

MAXIMUM MARKS : 50 MARKS

__________________________________________________________________________________

INSTRUCTIONS TO THE STUDENT

1. This question paper contains FOUR (4) questions.


2. Answer THREE (3) questions only:
a. Question ONE (1) from Section A is COMPULSORY.
b. Answer ANY TWO (2) questions from Section B.
3. No study material is allowed into the examination hall.
4. Do not forget to write your Registration Number.
5. Write all your answers in the booklet provided. Do not write any answers on this question paper.
6. SWITCH OFF all mobile phones.
7. Start each question on a NEW page.
MODULE CODE & TITLE: CSC 1261 COMPUTER PROGRAMMING

SECTION A
COMPULSORY QUESTION
Question 1 (20 marks)

1.1 Developing a program in a compiled language like C requires at least four steps, explain them (3marks)
1.2 By compiling your program written in C language, errors come in three different ways, explain them.
(3marks)
1.3 C language supports two different ways to insert comments, write or develop a program to find roots of a
quadratic equation when coefficients are entered by user and after apply one of those comments on each
line.
(4 marks)
1.4 Define with example the following terms as applied in computer programming. (4 marks)
a) Pre-processor directives b)Head Files c)Source Code d) Object Code
1.5 Using Switch case statement, develop a program in compiled language like C that asks user an arithmetic
operator (for example '+','-','*' or '/') and two operands and perform the corresponding calculation of that
operands. (6 marks)

SECTION B

Answer ANY TWO


Question 2 (15 marks)
1.1 Describe the meaning of the following statements written in C language: (5 marks)
a) %.4f b) %6.2f c) %ld d) c=(c>0)? 10:-10; e) int A [4][6];

1.2 Make a C program to calculate the perimeter and area of the rectangle where reads values for the length and
width of a rectangle and returns the perimeter and area of the rectangle. Ending the operation with the
message “Thank you for displaying the result!’’.
Note: Area= length*widith and Perimeter= 2*(length+width) (5 marks)

1.3 Write a c program that displays the following output . (5 marks)


(Note that n=5 and the table is up 7as indicated by horizontal line)
5*0=0
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Question 3 (15 marks)

3.1 Use C decision control statement to write a program that will prompt a user to enter marks of students
between 0 and 100 and make decision as follow : (5 marks)
100-80 : Upper Division
79.9-60 : Lower Division
59.9-50 : Pass
<50 : Retake
3.2 By applying the working of break statement, write a C program to find average of maximum of n positive
numbers entered by user. If the input is negative, display the average (excluding the average of negative
input) and end the program. (5 marks)

3.3 Make or debug the following program as efficient as possible. And by comments explain the debugs made
and don’t forget to give its corresponding output. (5 marks)

#include <stdio.h>
int main()
{
int base, exp;
long long int value=1;
printf("Enter base number and exponent respectively: ");
printf("Enter the value of variable respectively: ");
scanf("%f%f", &base, &exp);
scanf("%f%f", &value, &variable);
while (exp!)
{
value*=base;
value = value*base;
}
++exp;
}}
printf("Answer = %d", value);
}
}
Question 4 (15 marks)
4.1 a) Explain two types of programming functions in C. ( 3marks)
b) Give the reason of using functions in C. (2marks)
4.2. a) Define the term recursion in C programming and state its advantages and disadvantage. (3marks)
b) Give an example of a C program that using a recursion technique. (2marks)

4.3 .Write a C program to read elements in array from user and count total number of even and odd elements in
a given array. Note that the array size and its elements should be entered from the keyboard
(5marks)

***GOOD LUCK***
MARKING SCHEME

Q1
1.1 The four (4) four steps are the following: 3marks
Step 1:
A programmer uses an editor to write the source code (the program). By tradition C source code files have
the extension .c that means that you have to save the source code with .c extension. ex: Hello.c
Step 2:
Compile the program using a compiler. If the compiler doesn’t find any errors in the program, it produces
an abject file. The compiler produces object files with an .obj extension and the same name as the source
code file (for example Hello.c to Hello.obj). If the compiler finds errors, it responds (reports) them, you
must return to step 1 to make the corrections in your source code.
Step 3:
Link the program with a Linker. If no errors occur, the Linker produces an executable program with an .exe
extension and the same name as the object file (for example Hello.obj is linked to create Hello.exe).
Steps 4:
Execute the program to see the results. You should test to determine whether it functions properly, if no
start again with step 1 and make necessary modifications.

1.2 Programming errors come in the following 3 varieties: 3 marks


Compile (syntax) error: you get it when you’ve broken the rules of computer programming language.
e.g: spelling printf as prinntf or printif
You also receive a compile error, if accidentally use the wrong punctuation or place punctuation in the
wrong place.
Run-time error: it can be caused by attempting to do impossible arithmetic operations, such calculating
non-numeric data, dividing a number by zero, or find the square root of a negative number.
Logic error: with this, your application runs but produces incorrect result. Perhaps the results of calculation
are incorrect or the wrong text appears, or the text is ok but appears in the wrong location.

1.3 A program to find roots of a quadratic equation when coefficients are entered by user. 4 marks
[Check how a student apply comments and balance the marks accordingly]
To solve this program, library function sqrt() is used.
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}

1.4: Definition of the following terms. 4marks


a) Pre-processor directives: Pre-processor directives are lines included in the code of our programs that are
not program statements but directives for the pre-processor. These lines are always preceded by a hash sign
(#).
The pre-processor is executed before the actual compilation of code begins, therefore the pre-processor
digests all these directives before any code is generated by the statements.
Example: #include <stdio.h>

b) Head Files: Header files contain definitions of functions and variables which can be incorporated into
any C program by using the pre-processor #include statement. Standard header files are provided with each
compiler, and cover a range of areas.
To use any of the standard functions, the appropriate header file should be included.
This is done at the beginning of the C source file.
For example, to use the function printf() in a program, the line #include <stdio.h> should be at the
beginning of the source file, because the declaration for printf() is found in the file stdio.h.
c) Source Code: The source code is a series of statements or commands that are used to instruct the
computer to perform your desired task. To get from source code to machine language, the source code must
be translated by a compiler.
d) Object code: The object code it is often the same or similar to computer machine’s language like the code
produced by a compiler. Programmers write programs in a form called source code and the source code
consists of instructions in a particular language, like C or FORTRAN but Computers however, can only
execute instructions written in a low-level language called machine language or object code.

1.5 Developing a program in compiled language like C that asks user an arithmetic operator ('+','-
','*' or '/') using switch case statement (6marks)
The break statement at the end of each case cause switch statement to exit. If break statement is not used, all
statements below that case statement are also executed.
# include <stdio.h>
int main() {
char o;
float num1,num2;
printf("Select an operator either + or - or * or / \n");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Output
Enter operator either + or - or * or / *
Enter two operands: X and Y
X*Y=Z
Q2
2.1 Description of the meaning of the following statements written in C language: (5 marks)
a) %.4f : Print as floating point, four (4) characters after decimal point
b) %6.2f: Print as floating point, at least six (6) wide and two (2) after decimal point
c) %ld: Print long integer
d) c=(c>0)? 10:-10; If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10
e) int A [4][6]; Declaration of an array of integer numbers comprise with 4 rows and 6 columns. Each row
has 6elements; therefore, the array is comprised of 24 elements

2.2 A program to calculate the perimeter and area of the rectangle where reads values for the length
and width of a rectangle and returns the perimeter and area of the rectangle: (5marks)
#include <stdio.h>
int main()
{
int length, width; // declaration of input variables
int perimeter, area; // declaration of output variables
printf( "Length = "); // prompt user
scanf(“ %d”,&length); // enter length
printf( "Width = "); // prompt user
scanf(“ %d”,&width); // input width
perimeter = 2*(length+width); // compute perimeter
area = length*width; // compute area
//output results
printf(“\nPerimeter is %d meters\n", perimeter);//output perimeter
printf(“\nArea is %d square meters\n,area");// output area
printf(”Thank you for displaying the result! \n”);
return 0;
} // end of main program
2.3. The produced output program can be made more user friendly by giving option to user to enter
number up to which, he/she want to generate multiplication table(the example given is up to 7as
range according to the horizontal line). The source code below will perform this task. (5marks)
#include <stdio.h>
int main()
{
int n, range, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
printf("Enter range of multiplication table: ");
scanf("%d",&range);
for(i=1;i<=range;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}
Output

Enter an integer to find multiplication table: 5


Enter range of multiplication table: 7

Q3.
3.1 The use C decision control statement to write a program and grade the marks of students
(5marks)
# include <stdio.h>
int main()
{
int marks;
printf("enter the range of marks of students\n");
scanf("%d",&marks);
if (marks>80)
{
printf("upper division\n");
}
else if (marks>60)
{
printf("lower division\n");
}
else if (marks>50)
{
printf("pass\n");
}
Else
{
printf("retake\n");
}
return 0;
}
3.2 Applying the working of break statement to write a program to find average of maximum of n
positive numbers entered by user. (5marks)
In this program, when the user inputs number less than zero, the loop is terminated using break statement.
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Output
Maximum no. of inputs: 4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average=7.07

3.3 After analysing or debugging, the program source is look like the following and the purpose of the
program is takes two integers from user, a base number and an exponent and calculates the value of
base exponent. (Check the debugs made by student and adjust mark accordingly). (5marks)
/* C program to calculate the power of an integer*/
#include <stdio.h>
int main()
{
int base, exp;
long long int value=1;
printf("Enter base number and exponent respectively: ");/* this program takes base number and exponent
from user and stores in variable base and exp respectively*/
scanf("%d%d", &base, &exp); /* this program can only calculate the power if base power and exponent
are integers*/
while (exp!=0) /* while loop is executed with test condition exp!=0*/
{
value*=base; /* value = value*base; */
--exp;
}
printf("Answer = %d", value);
}
Output
Enter base number and exponent respectively: 3
4
Answer = 81(means 3*3*3*3)

Q4.
4.1 a) basically, there are two types of functions in C, Library function and User defined function
respectively. (3marks)
Library function
Library functions are the in-built function in C programming system.
For example: main() The execution of every C program starts from this main() function.
printf() is used for displaying output in C, scanf() is used for taking input in C.
User defined function
C provides programmer to define their own function according to their requirement known as user defined
functions.
For example: Suppose a programmer wants to find factorial of a number and check whether it is prime or
not in same program. Then, he/she can create two separate user-defined functions in that program: one for
finding factorial and other for checking whether it is prime or not.

b) The following are the reasons of using function in C (2marks)


-By function, no need to repeat the same block of code many times in your code. Make that code block a
function and call it when needed.
– Function portability: useful functions can be used in a number of programs.
– Supports the top-down technique for devising a program algorithm. Make an outline and hierarchy of the
steps needed to solve your problem and create a function for each step.
– Easy to debug. Get one function working well then move on to the others.
– Easy to modify and expand. Just add more functions to extend program capability
– Make program self-documenting and readable.
4.2.a) Defining the term recursion in C programming and state its advantages and disadvantage
(3marks)
Recursion is a programming technique that allows the programmer to express operations in terms of
themselves. In C, this takes the form of a function that calls itself.
Advantages and Disadvantage
Recursion is more elegant and requires few variables which make program clean. Recursion can be used to
replace complex nesting code by dividing the problem into same problem of its sub-type.
In other hand as disadvantage, it is hard to think the logic of a recursive function. It is also difficult to debug
the code containing recursion.
b) Example of a C program to calculate factorial of a number using a recursion technique. (2marks)
/* Source code to find factorial of a number. */
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
Output
Enter a positive integer: 6
Factorial of 6 = 720

4.3 A C program to read elements in array entered by user and count total number of even and odd
(5marks)

/*C program to count total number of even and odd elements in an array*/
#include <stdio.h>
int main()
{
int arr[max_size];
int i, n, even, odd;
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter %d elements in array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
/* Assuming that there are 0 even and odd elements */
even = 0;
odd = 0;

for(i=0; i<n; i++)


{
/* If the current element of array is even then increment even count */
if(arr[i]%2 == 0)
{
even++;
}
else
{
odd++;
}
}
printf("total even elements: %d\n", even);
printf("total odd elements: %d\n", odd);
return 0;
}

Output
Enter size of the array: 10
Enter 10 elements in array: 5 6 4 12 19 121 1 7 9 63
Total even elements: 3
Total odd elements: 7

You might also like