Vous êtes sur la page 1sur 10

# INTRODUCTION TO PROGRAMMING

## SCHOOL OF COMPUTING AND INFORMATICS

DEPARTMENT OF ICT

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

[6Marks]

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

#include <stdio.h>

int main ()

int age;

if (age 18=<)

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;

scanf("%d",&age);

if(age >=18)

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

else

printf("Issue an ID");

return 0;

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

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

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

developing a program.

(ii) Portability
INTRODUCTION TO PROGRAMMING

## (iii) Repetition control structures

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

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

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

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

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

[8Marks]

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

[6 Marks]

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

## Example for do while:

int a = 10;

do

Printf ("cyrus\n");

} while (a==0);

REFERENCES

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

1979 Wiley New York