



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A brif document on everything you need to know about computer programming
Typology: Essays (university)
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Answer All Questions in Section A (Compulsory) and any other TWO Questions from Section B
Question 1 (a) Explain what is wrong with the following variable/constant declarations and correct them. (i) integer sum; to hold an integer variable called sum (ii) #define TRUE = 1 ; which defines a constant called TRUE with a value of 1 (iii) float arctan ; which holds scientific notation values (+e) (iv) #define GST 0.125 ; to define a constant gst with a value 0. (v) char grade = ‘FAIL’ to hold a string of value FAIL (5 marks)
(b) Write code in C to perform the following assignment operations (i) Assign the value of the variable number1 to the variable total (ii) Assign the sum of the two variables loop_count and petrol_cost to the variable sum (iii) Divide the variable total by the value 10 and leave the result in the variable discount (iv) Assign the character W to the char variable letter (v) Assign the result of dividing the integer variable sum by 3 into the float variable costing. Use type casting to ensure that the remainder is also held by the float variable (5 marks)
(c) Write C statements which will perform the following scanf and printf operations (i) Print out the text string "Welcome", followed by a newline. (ii) Print out the character variable letter (iii) Print out the float variable dump using two decimal places (iv) Read a decimal value from the keyboard, into the integer variable sum (v) Read a single character from the keyboard into the variable operator, skipping leading blanks, tabs and newline characters. (5 marks)
Question 2
(a) (i) Write a for loop C program section which will produce the following output using
two nested for loops. (4 marks) 1 22 333 4444 55555 (ii) Modify the for loop above to use the while loop (2 marks)
(b) Below is a C program which checks a value of char variable letter and performs different
actions depending on the value of letter.
if( letter == 'X' ) sum = 0; else if ( letter == 'Z' ) valid_flag = 1; else if( letter == 'A' ) sum = 1; else printf("Unknown letter -->%c\n", letter );
Rewrite this code using a CASE statement. (4 marks)
(c) Perform the following activities on arrays (5 marks)
(i) Declare a character based array called letters of ten elements (ii) Assign the character value 'Z' to the fourth element of the letters array above (iii) (^) Use a for loop to total the contents of an integer array called numbers which has five elements. Store the result in an integer called total. (iv) Declare a multi-dimensional array of floats called balances having three rows and five columns. (v) Write a for loop to total the contents of the multi-dimensional float array balances.
Question 3
(a) Below is a c program which incorporates a function. The function uses parameter passing and performs the addition of three numbers. The result is then printed in the main section of the program. Use it to answer the questions below:
#include <stdio.h> int calc_result( int, int, int );
int calc_result( int var1, int var2, int var3 ) { int sum;
sum = var1 + var2 + var3;
time -= 5; a *= b + c; (5 marks)
Question 4
(a) Differentiate between the following in C
(i) long int and int variables (2 marks) (ii) float and double variables (2 marks) (iii) char and string variables (2 marks)
(b) Write down the result after the following printf operations are performed:
(i) printf("%d", sum); (1 mark)
(ii) int sum = 50; float modulus; modulus = sum % 10; printf("The %% of %d by 10 is %.2f\n", sum, modulus); (2 marks)
(iii) int value1 = 12, value2 = 5; float answer = 0; answer = value1 / value2; printf("The value of %d divided by %d is%f\n",value1,value2, answer); (2 marks)
(iv) #include <stdio.h>
#define TAX_RATE 0.
main() { float balance; float tax;
balance = 72.10; TAX_RATE = 0.15; tax = balance * TAX_RATE; printf("The tax on %.2f is %.2f\n", balance, tax ); }
(c) A section of code is to written to check if marks entered on keyboard obeys the following rules. Write down the Boolean expression for the rules in C;
(i) 0 ≤marks≤ 100 (1 mark) (ii) 39< marks ≤ 50 and catmarks>20 (2 marks) (iii) 74<marks≤100 and exammark≥ 40 (2 marks)
Question 5
(a) Details of a student are kept in a record called student with fields admin_no,
name, course, year and sex(M,F):
(i) Define the structure called student to hold these values (3 marks) (ii) Using appropriate C statements assign the values “1508, TOM ONYANGO, MBA, 2, M” to the fields admin_no, name, course, year and sex respectively. (3 marks) (iii) Redefine the student record above as an array studarray to hold the details of the students shown in the table below: ( 2 marks)
admin_no name course year sex 1202 TOM CHOMLEY BUS 2 M 0082 RECHO KAMAU BBIT 1 F 1304 BRAMUEL KARANI BDIT 3 M 0045 FLORENCE MENYA BCOM 2 F 2477 RICHARD KIPLIMO MBA 2 M 0942 GREGORY WAMALWA BIT 4 M
(iv) Initialize the student array above with the table values shown above (5 marks) (v) Assuming the values were entered from the keyboard into the array using a for loop with an index J, write down the C code for the loop. (3 marks)
(b) What is the output of the following program segment?
int count = 10, *temp, sum = 0;
temp = &count; *temp = 20; temp = ∑ *temp = count; printf("count = %d, *temp = %d, sum = %d\n", count, *temp, sum ); (4 marks)
Question 6
(a) Answer the following question on file handling in C
(i) Define an input file handle called input_file , which is a pointer to a type FILE (1 mark) (ii) Using input_file , open the file results.dat for read mode as a text file. (1 mark) (iii) Write C statements which tests to see if input_file has opened the data file successfully. If not, print an error message and exit the program. (2 marks) (iv) Close the file associated with input_file. (1 mark)