Vous êtes sur la page 1sur 10

INTRODUCTION TO PROGRAMMING

MOUNT KENYA UNIVERSITY

SCHOOL OF COMPUTING AND INFORMATICS

DEPARTMENT OF ICT

UNIT CODE: BIT1102

UNIT TITLE: INTRODUCTION TO PROGRAMMING CAT 2

LECTURE NAME: MERCY NJERI MAINA

NAME: CYRUS S MBITHI

REGISTRATION NUMBER: BIT/2017/76313


INTRODUCTION TO PROGRAMMING

QUESTION ONE

a) Design and write a program that reads the age of a person and outputs an appropriate

Message based on;

[6Marks]

(i) If age is less than 18 is output “Below 18 years”

#include <stdio.h>

int main ()

int age;

Printf ("Enter your age :");

Scanf ("%d", &age);

if (age 18=<)

Printf ("Below 18 years");

return 0;

(ii) If age is greater or equal to 18 outputs” Issue an ID”

#include <stdio.h>

int main()
INTRODUCTION TO PROGRAMMING

int age;

printf("Enter your age:");

scanf("%d",&age);

if(age >=18)

printf("age is greater or equal to 18");

else

printf("Issue an ID");

return 0;

b) Define the following terms6Marks]

(i) Pseudo code

Pseudo code is a text representation which is in grammatical language that

shows how software applications flow through code. The pseudo code uses

written language to display information. Pseudo code conveys programming

ideas that are easy to interpret. Pseudo code is a high-level description of

algorithms. Pseudo code is sometimes used as a detailed step in the process of

developing a program.

(ii) Portability
INTRODUCTION TO PROGRAMMING

This is an attributed characteristic of a program which possesses the actual

attribute of an operating system rather than the one intended to rework.

(iii) Repetition control structures

This are group of codes which are designed to execute a block of codes

repeatedly based on some conditions

c) Explain the types of operators used in C [8 Marks]

Operator Types Description


They are most used to perform mathematical
formulations, manipulations and functions such as
Arithmetic operators
subtraction, division multiplication, addition and also
modulus

Most of them are used to assign variables in the c


Assignment operators
program

Relational operators Used to relate or compare two variables in c program

They are specifically used to perform logical


Logical operators
manipulations to two given variables
They are specifically used to perform Bitwise
Bitwise operators
manipulations to two given variables.

They are used to check if a given condition is true to


Conditional operators
return true value and if it is false they return false value.

QUESTION TWO

a) Explain the types of Variables found in C program [4 Marks]

(i) Local variable


INTRODUCTION TO PROGRAMMING

This is a type of variable where the scope will be within the function only.

(ii) Global variable

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

b) Using an Example explain how the Switch Statement is used in C Program [8

Marks]

Switch case statements are used to execute only specific case statements based on the switch

expression.

Example of switch case statement

#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 :

printf(“Value is other than 1,2,3,4 \n” );

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;

float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");

scanf("%d", &rad);

area = PI * rad * rad;

printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;

printf("\nCircumference : %f ", ci);

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]

Explain any errors or unexpected performance that may be noticed


INTRODUCTION TO PROGRAMMING

(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

could not be met.

b) With an examples explain the difference between the following terms

[8Marks]

i. interpreters and compilers

An interpreter converts a program by taking a single line at a time while a

compiler converts the whole program at a go.

ii. local and global variables

Local variable scope is found within the function only while global variables the

scope is found throughout the entire program

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

iv. printf() and scanf()

printf () is to produce output for the user to read, whereas to scanf() is to read input from

the command line that the user types in.

c) Differentiate between a While ….. statement and Do…. statement in C

[6 Marks]

In while the given condition is checked at the start of the loop.

If the condition is false then the loop is not executed at all.

Only when the condition is true the loop block is executed.

In do while first the loop block is executed then condition is checked at the end of 1st time

execution.

Example for while:

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

the loop block (printf statement) is not executed.

Example for do while:

int a = 10;

do

Printf ("cyrus\n");

} while (a==0);

REFERENCES

1. Journal Article Genetic programming Koza, John R 1997 Citeseer.

2. T Mathematical programming: structures and algorithms Shapiro, Jeremy F Shapiro, JF


1979 Wiley New York