0% found this document useful (0 votes)
6 views

CCS0007- Laboratory Exercise 1

The document outlines a laboratory exercise for a Computer Programming 2 course, focusing on user-defined functions and parameters in C++. It includes program outcomes, learning objectives, grading rubrics, and instructions for coding exercises that involve creating functions for mathematical operations and calculating areas of various polygons. Additionally, it provides background information on functions and a Q&A section addressing the advantages of using functions and the differences between predefined and user-defined functions.
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)
6 views

CCS0007- Laboratory Exercise 1

The document outlines a laboratory exercise for a Computer Programming 2 course, focusing on user-defined functions and parameters in C++. It includes program outcomes, learning objectives, grading rubrics, and instructions for coding exercises that involve creating functions for mathematical operations and calculating areas of various polygons. Additionally, it provides background information on functions and a Q&A section addressing the advantages of using functions and the differences between predefined and user-defined functions.
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/ 14

COLLEGE OF COMPUTER STUDIES

CCS007L
(COMPUTER PROGRAMMING 2)

EXERCISE

1
User Defined Functions and Parameters

Student Name / Group


Name:
Name Role
Cedrick Nicolas C. Valera
Members (if Group):

Section:
BSITWMA-TW01

Professor:
John Benedic Enriquez
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
 Analyze a complex problem and identify and define the computing requirements
appropriate to its solution. [PO: B]

II. COURSE LEARNING OUTCOME/S (CLO)ADDRESSED BY THE LABORATORY EXERCISE


 Understand the fundamental principles of C-string manipulations, pointer and memory
allocation and structures using C++ [CLO: 1]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Identify User-Defined Functions
 Identify Types of User-defined Functions in C++
 Create User defined functions in a C++ program
 Understand Parameters and Arguments
 Understand Function Overloading

IV. BACKGROUND INFORMATION

Functions:
 Procedure, subprograms, and method
 A function may return a value (produce a value) or may perform some action without
returning a value.
 Functions that do not return a value are called void functions.

User defined functions


 You can define your own functions, either in the same file as main part of your program
or in a separate file so that the functions can be used by several different programs.

CCS007L-Computer Programming 2 Page 2 of


14
V. GRADING SYSTEM/ RUBRIC

Trait (Excellent) (Good) (Fair) (Poor)


Able to identify Able to identify Able to identify only Unable to
correctly all input correctly all input one input or output identify any input
and output and and output (22-14pts) and output
Requirement
provide alternative. (25-17pts) (20-11pts)
Specification(30pts)
(28-20pts)

Able to apply Able to apply Able to identify Unable to


required data type required data type required data type or identify required
or data structure or data structure data structure but data type
Data type(20pts)
and produce and produce does apply correctly (9-11pts)
correct results (18- partially correct (12-14pts)
20pts) results (15-17pts)
The program works The program works The program The program
and meets all and meets all produces correct produce s
specifications. Does specifications. results but does not incorrect results
Input
exception al Does some display correctly Does (9-11pts)
Validation(20pts)
checking for errors checking for errors not check for errors
and out-of- range and out of range and out of range data
data (18-20pts) data (15-17pts) (12-14pts)
Unable to run Able to run Able to run program Able to run
program (10pts) program but have correctly without any program
Free from syntax, logic error (8-9pts) logic error and correctly without
logic, and runtime display inappropriate any logic error
errors (10pts) output (6-7pts) and display
appropriate
output (5pts)
The program was The program was The program was The program was
delivered on time delivered after 5 delivered after 10 delivered after
(10pts) minutes from the minutes from the 15 (or more)
Delivery (10pts)
time required. (8- time required. (6- minutes from the
9pts) 7pts) time required.
(5pts)
Use of Comments Specific purpose is Specific purpose is Purpose is noted for No comments
(10pts) noted for each noted for each each function. (6- included. (5pts)
function, control function and 7pts)
structure, input control structure.
requirements, and (8-9pts)
output results.
(10pts)

CCS007L-Computer Programming 2 Page 3 of


14
VI. LABORATORY ACTIVITY
INSTRUCTIONS:
Copy your source codes to be pasted in this document as well as a screen shot of your running
output.

Activity 1.1 User defined Functions


The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the
user to input two integer vales for the calculation

Example Program Output:

ACTIVITY 1.2: User-defined Functions (Passing by value)


Create a program with a user-defined function that compute the area of the following polygons
and circle. You are to create one user-defined function for every computation. The functions
must accept parameters by reference.

• Area of square given the side.


• Area of rectangle given the length and width.
• Area of triangle given the base and height.
• Area of circle given the radius.

CCS007L-Computer Programming 2 Page 4 of


14
EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 5 of


14
Snip and paste your source codes here. Snip it directly from the IDE so that colors of the codes are
preserved for readability. Include additional pages if necessary.

CCS007L-Computer Programming 2 Page 6 of


14
#include <iostream>
#include <string>
#include<windows.h>
using namespace std;

int main() {
int choice;
int number1, number2, ans;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(h, 10); // Set text color to green (10 is the color code for green)

do {
cout << "\n--------------------------------------------------------------------------\n"
<< " WELCOME TO MATHEMATICS CALCULATOR APPLICATION OPERATORS! \n"
<< "--------------------------------------------------------------------------\n";

cout << "Choose your operators to use in solving:\n";


cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multipllication\n";
cout << "4. Division\n";
cout << "5. Modulos\n";
cout << "6. Exit instead?\n";

cout << "\nEnter your choice from 1-6: ";


cin >> choice;

switch (choice)
{
//Addition Operator
case 1:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 + number2;
cout << "The sum of the addition of two numbers are: " << ans << endl;
break;

CCS007L-Computer Programming 2 Page 7 of


14
//Subtraction Operator
case 2:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 - number2;
cout << "The difference of the subtraction of two numbers are: " << ans << endl;
break;

//Multiplication Operator
case 3:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 * number2;
cout << "The product of the mulitplication of two numbers are: " << ans << endl;
break;

//Division Operator
case 4:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 / number2;
cout << "The quotient of the division of two numbers are: " << ans << endl;
break;

//Modulos Operator
case 5:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 % number2;
cout << "The remainder of the modulos of two numbers are: " << ans << endl;
break;

//Exit Operation directly


case 6:
cout << "You have chosen to exit." << endl;
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
return 0; // Ends the program directly

default:
SetConsoleTextAttribute(h, 12); // Set text color to red
cout << "Invalid choice. Please try again.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
break; // Break out of the switch case
}

} while (choice < 1 || choice > 6); // Ensures the user enters a valid choice (1-6)

char continueChoice;

CCS007L-Computer Programming 2 Page 8 of


14
while (true) {
cout << "\nDo you want to continue? (Y/N): ";
cin >> continueChoice;
continueChoice = toupper(continueChoice); // Convert to uppercase for consistency

if (continueChoice == 'Y') {
main(); // Restart the program
}
else if (continueChoice == 'N') {
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
break;
}

else
{
SetConsoleTextAttribute(h, 12); // Set text color to red (12 is the color code for red)
cout << "Invalid input. Please enter 'Y' or 'N' only.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
}
}

return 0;
}

/*Formative1*/
//Deadline 01-31-25
/*CCS0007L*/

CCS007L-Computer Programming 2 Page 9 of


14
#include <iostream>
#include <string>
#include <windows.h>
CCS007L-Computer Programming 2 Page 10 of
14
using namespace std;

int main() {
int choice;
int number1, number2, ans;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(h, 10); // Set text color to green (10 is the color code for green)

do {
cout << "\n--------------------------------------------------------------------------\n"
<< " WELCOME TO AREA OF POLYGONS CALCULATOR APPLICATION! \n"
<< "--------------------------------------------------------------------------\n";

cout << "Choose your area of polyons to use in solving:\n";


cout << "1. Area of Square\n";
cout << "2. Area of Rectangle\n";
cout << "3. Area of Triangle\n";
cout << "4. Area of Circle\n";
cout << "5. Exit instead?\n";

cout << "\nEnter your choice from 1-5: ";


cin >> choice;

switch (choice)
{
//Area of Square
case 1:
int number1; int squared;
cout << "\nEnther the side of the Square:\n";
cin >> number1;
cout << "\n===========================";
cout << "\n AREA OF SQUARE ";
cout << "\n===========================";
squared = number1 * number1;
cout << "\nThe area is " << " " << squared << " " << "sq. units" << endl;
break;

//Area of Rectangle
case 2:
int rectangle, length, width1;
cout << "\nEnther the length and width of the Rectangle:\n";
cin >> length >> width1;
cout << "\n===========================";
cout << "\n AREA OF RECTANGLE ";
cout << "\n===========================";
rectangle = length * width1;
cout << "\nThe area is " << " " << rectangle << " " << "sq. units" << endl;
break;

//Area of Triangle
case 3:
CCS007L-Computer Programming 2 Page 11 of
14
int base, height;
cout << "\nEnther the length and width of the Rectangle:\n";
cin >> base >> height;
cout << "\n===========================";
cout << "\n AREA OF TRIANGLE ";
cout << "\n===========================";
cout << "\nThe area is " << (base * height) / 2 << " sq. units" << endl;
break;

//Area of Circle
case 4:
int radius; float circle;
cout << "\nEnther the radius of the Circle:\n";
cin >> radius;
circle = 3.14159265359 * radius * radius;
cout << "\n===========================";
cout << "\n AREA OF CIRCLE ";
cout << "\n===========================";
cout << "\nThe area is " << " " << circle << " " << "sq. units" << endl;
break;

//Exit Operation directly


case 5:
cout << "You have chosen to exit." << endl;
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
return 0; // Ends the program directly

default:
SetConsoleTextAttribute(h, 12); // Set text color to red
cout << "Invalid choice. Please try again.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
break;
}

} while (choice < 1 || choice > 5); // Restricts the user on choosing only from 1-5

char continueChoice;

while (true) {
cout << "\nDo you want to continue? (Y/N): ";
cin >> continueChoice;
continueChoice = toupper(continueChoice); // Convert to uppercase for consistency

if (continueChoice == 'Y') {
main(); // Restart the program
}
else if (continueChoice == 'N') {
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
CCS007L-Computer Programming 2 Page 12 of
14
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
break;
}

else
{
SetConsoleTextAttribute(h, 12); // Set text color to red (12 is the color code for red)
cout << "Invalid input. Please enter 'Y' or 'N' only.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
}
}

return 0;
}

/*Formative1*/
//Deadline 01-31-25
/*CCS0007L*/

CCS007L-Computer Programming 2 Page 13 of


14
VII. QUESTION AND ANSWER

Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.

 What is a Function? What are the advantages of using it?


A function is a small code snippet created for a specific purpose, such as arranging data,
finding an element, or reversing a matrix. Once tested, it can be used repeatedly,
enhancing software stability and speeding up coding. This makes the program also more
reliable once tested by the developer.

 What is the difference between predefined function and user defined function?
Predefined functions are used in conducting common operations such as mathematical
computation like sqrt. Another predefined function is cin and cout input and output. User
defined function on the other hand is created by the programmer/developer using their own
terms to perform a specific task.

VIII. REFERENCES
 Zak, Dianne (2016). An Introduction to Programming with C++
 Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
 https://www.cprogramming.com/tutorial/lesson4.html

CCS007L-Computer Programming 2 Page 14 of


14

You might also like