Introduction To Programming and Algorithms Cat 2
Introduction To Programming and Algorithms Cat 2
DEPARTMENT OF ICT
QUESTION ONE
a) Design and write a program that reads the age of a person and outputs an appropriate
[6Marks]
#include <stdio.h>
int main ()
int age;
if (age 18=<)
return 0;
#include <stdio.h>
int main()
INTRODUCTION TO PROGRAMMING
int age;
scanf("%d",&age);
if(age >=18)
else
printf("Issue an ID");
return 0;
shows how software applications flow through code. The pseudo code uses
developing a program.
(ii) Portability
INTRODUCTION TO PROGRAMMING
This are group of codes which are designed to execute a block of codes
QUESTION TWO
This is a type of variable where the scope will be within the function only.
This is a type of variable where the scope will be throughout the entire program. The
scope of the global variable can be accessed through the entire program
Marks]
Switch case statements are used to execute only specific case statements based on the switch
expression.
#include <stdio.h>
int main ()
int value = 3;
switch(value)
case 1:
printf(“Value is 1 \n” );
break;
case 2:
INTRODUCTION TO PROGRAMMING
printf(“Value is 2 \n” );
break;
case 3:
printf(“Value is 3 \n” );
break;
case 4:
printf(“Value is 4 \n” );
break;
default :
return 0;
c) Design and write a C program to compute the area and circumference of a circle
[8 Marks]
#include<stdio.h>
int main()
INTRODUCTION TO PROGRAMMING
int rad;
scanf("%d", &rad);
ci = 2 * PI * rad;
return (0);
QUESTION THREE
a) The following C program is supposed to accept three integer digits entered from the
keyboard and prints their average. Study it and answer the question below.
[6 Marks]
(i) Syntax error – This is an error which occurs when you violate the rules of writing c
program. After line 2 of writing the main function a parenthesis is missing so a syntax
will occur.
(ii) Run –time errors – this errors occur during program execution
(iii) Logical errors – During compilation and execution of the program the desired output
[8Marks]
Local variable scope is found within the function only while global variables the
iii. %d and %f
%d stands for decimal and it expects an argument of type int (or some smaller signed
integer type that then gets promoted). Floating-point types float and double both get passed
the same way (promoted to double) and both of them use %f.
INTRODUCTION TO PROGRAMMING
printf () is to produce output for the user to read, whereas to scanf() is to read input from
[6 Marks]
In do while first the loop block is executed then condition is checked at the end of 1st time
execution.
int a=10;
While (a==10)
Printf ("HEllo\n");
a++;
}
INTRODUCTION TO PROGRAMMING
In this 1st the condition is checked: is a equal to 10? YES so the printf statement is executed.
Now a is incremented to 11. Now again when it checks if a is equal to 10, it is false and hence
int a = 10;
do
Printf ("cyrus\n");
} while (a==0);
REFERENCES