[go: up one dir, main page]

0% found this document useful (0 votes)
33 views5 pages

Bbit 222 Assignment

This document contains a student's responses to exam questions about structured programming concepts in C++. The student provides definitions and examples for key terms like functions, arrays, pointers, and structures. They also explain looping structures like while, for, and do-while loops. Control structures like if/else and if-then are described. The differences between library vs user functions, pass by value vs reference, and local vs global variables are outlined.

Uploaded by

Kiki Kyms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Bbit 222 Assignment

This document contains a student's responses to exam questions about structured programming concepts in C++. The student provides definitions and examples for key terms like functions, arrays, pointers, and structures. They also explain looping structures like while, for, and do-while loops. Control structures like if/else and if-then are described. The differences between library vs user functions, pass by value vs reference, and local vs global variables are outlined.

Uploaded by

Kiki Kyms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NAME : AWIL MAALIM ABDULLAI

REG NO: BIT-1-9952-3-2018


UNIT CODE :BBIT 222

(a) State FOUR advantages of structured programming. (4 Marks)

 Easier to read and understand


 User Friendly
 Easier to Maintain
 Mainly problem based instead of being machine based

(b) With illustrations describe the following terms as used in structured programming.
(8 Marks)
(i) Function- allows programmers to pass a single or entire structure information to or
from a function. Example ;

#include <stdio.h>
#include <string.h>
Struct student
{
Int id;
Char name[20];
Float percentage;
};

Void func(struct student record);

Int main()
{
Struct student record;

Page 1 of 5
Record.id=1;
Strcpy(record.name, “Raju”);
Record.percentage = 86.5;

Func(record);
Return 0;
}

Void func(struct student record)


{
Printf(“ Id is: %d \n”, record.id);
Printf(“ Name is: %s \n”, record.name);
Printf(“ Percentage is: %f \n”, record.percentage);
}
(ii) Array - Arrays a kind of data structure that can store a fixed-size sequential collection
of elements of the same type. Example

#include <stdio.h>
Int main () {
Int n[ 10 ];
Int I,j;
For ( I = 0; I < 10; i++ ) {
N[ I ] = I + 100; /* set element at location I to I + 100 */
}
For (j = 0; j < 10; j++ ) {
Printf(“Element[%d] = %d\n”, j, n[j] );
}

Return 0;

Page 2 of 5
(iii) Pointer - A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location. Example

#include <stdio.h>
Int main () {

Int var = 20; /* actual variable declaration */


Int *ip; /* pointer variable declaration */

Ip = &var; /* store address of var in pointer variable*/

Printf(“Address of var variable: %x\n”, &var );

/* address stored in pointer variable */


Printf(“Address stored in ip variable: %x\n”, ip );

/* access the value using the pointer */


Printf(“Value of *ip variable: %d\n”, *ip );

Return 0;
}
(iv) Structure - The struct statement defines a new data type, with more than one
member. Example sytax
Struct Books{
Char title[50];
Char author[50];
Char subject[50];
Int book_id;
}
book;

Page 3 of 5
(c) Explain THREE ways of implementing the looping structure in C++ programming language.
(6 Marks)
 While loop- Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
 for loop - Execute a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
 do...while loop - Like a ‘while’ statement, except that it tests the condition at the end of
the loop body.

(d) Differentiate between the following terms as used in structured programming.


(6 Marks)

(i) Library and user defined functions


 Library a library in C is a collection of header files, exposed for use by other programs
while A function is a block of code that performs a specific task. C allows you to define
functions according to your need.
(ii) Pass by value and pass by reference
 Pass by value means that a copy of the data is made and stored by way of the name of the
parameter while Pass by reference an argument in the calling function to the
corresponding formal parameter of the called function so that a copy of the address of the
actual parameter is made in memory.
(iii) Local and global variable
 Local variable is Variables that are declared within or inside a function block while
global variable Global variables are those variables which are declared outside of all the
functions or block and can be accessed globally in a program.

Page 4 of 5
(e) Describe any THREE program control structures as used in structured programming.
(6 Marks)

 If else -If the Boolean expression evaluates to true, then the if block will be executed,
otherwise, the else block will be executed.
 If-then - statement is the most basic of all the control flow statements. It tells your
program to execute a certain section of code only if a particular test evaluates to true
 Do…while - is similar to a while loop, except the fact that it is guaranteed to execute at
least one time.

Page 5 of 5

You might also like