Scs202 Group a Assignment Doc.
Scs202 Group a Assignment Doc.
SCHOOL OF EDUCATION
PROGRAMME: BED SCIENCE
DEPARTMENT OF EDUCATION AND TECHNOLOGY
Course Code: SCS202
Course Title: OBJECT ORIENTED PROGRAMMING
Academic Year: Year 2 Semester 2, 2025/2026Academic Year
DATE:21/3/2025.
Pass by Value: A copy of the argument is made, and modifications inside the
function do not affect the original variable.
Pass by Reference: The function receives a reference to the actual variable, so
modifications affect the original variable.
Example Program:
#include <iostream>
int main() {
int a = 5, b = 5;
return 0;
}
Output:
Before passByValue, a = 5
Inside passByValue, x = 15
After passByValue, a = 5
Before passByReference, b = 5
Inside passByReference, x = 15
After passByReference, b = 15
Explanation:
int main() {
int number;
return 0;
}
4.As a software developer at ABC academy, you are required to develop a simple
App to be used by grade one pupils. The aim of the app is to help the pupils learn
about naming digits. The app should accept values from 0 to 9 and then display
the name on the screen based on the number entered. E.g., if a user keys in 5,
then the program will output Five. Write a C++ program to implement the app.
#include <iostream>
using namespace std;
int main() {
int digit;
// Introduction message
cout << "Welcome to the Number to Word App!" << endl;
cout << "Please enter a digit between 0 and 9: ";
cin >> digit;
5.Explain the differences between global and local variable. Give an example in
each case.
Global Variable:
A global variable is declared outside any function, typically at the top of a
program or file, and is accessible by all functions within the same file or program.
Its scope is global, meaning it can be accessed and modified by any part of the
code after its declaration.
The lifetime of a global variable is for the entire duration of the program. It is
created when the program starts and destroyed when the program terminates.
If a global variable is not initialized, it is automatically initialized to zero (or
NULL for pointers).
EXAMPLE:
#include <iostream>
using namespace std;
void printGlobal() {
cout << "Global Variable: " << globalVar << endl; // Accessing global variable
}
int main() {
printGlobal(); // Global variable is accessible here
globalVar = 20; // Modifying global variable
printGlobal(); // Accessing modified global variable
return 0;
}
Local Variable:
A local variable is declared inside a function or block, and its scope is limited to
the block or function in which it is declared. This means it cannot be accessed
from outside the function or block.
The lifetime of a local variable is limited to the execution of the block or function
in which it is declared. Once the function or block finishes executing, the local
variable is destroyed.
A local variable does not have a default value. It must be explicitly initialized
before use, otherwise, it may contain garbage data.
Example of Local Variable:
#include <iostream>
using namespace std;
void myFunction() {
int localVar = 5; // This is a local variable
cout << "Local Variable: " << localVar << endl;
}
int main() {
myFunction(); // localVar is accessible inside myFunction() only
// cout << localVar; // Error! localVar is not accessible here
return 0;
#include <iostream>
int main() {
// Array declaration
int arr[3] = {10, 20, 30};
// Pointer declaration
int* ptr = arr; // Pointer points to the first element of the array
return 0;
}
Output:
Explanation:
2. Pointer (ptr): Initially points to the first element of arr. It can be moved
to different memory locations (ptr = &arr[1])
sizeof(arr) gives total size of the array, but sizeof(ptr) only gives size of the
pointer (usually 4 or 8 bytes).
7.As a software developer at ABC academy, you are required to develop a C++
program to take the price and quantity of items as an input for a given customer
during checking out. The program should use four functions as follows:
i. Function called compute(), to calculate the total of the prices.
ii. Function called calcDiscount(), to calculate the discount according
to the following rules:
• Total less than 1000, No discount.
• Total more than 1000 but less than 10,000, discount 6%
• Total more than 10,000, discount 10%
iii. Function called Display(), to print the individual item prices, sub-
total, discount and the grad total.
iv. main() function where program execution begins.
#include <iostream>
using namespace std;
// User input
cout << "Enter price of the item: ";
cin >> price;
cout << "Enter quantity: ";
cin >> quantity;
return 0;
}
Explanation:
4. main() takes user input, processes it, and displays the output.