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

Course Title: CP - LAB: Bahria University, Islamabad

The document is a lab journal for a student's CP-LAB course. It contains 3 tasks completed by the student: 1) A function to swap 3 numbers passed by reference, 2) A function to set the smaller of two numbers passed by reference to 0, 3) A function to add 13 to a number if it is even, otherwise leave it unchanged. The student provides the objectives, tools used, procedures, and outputs for each task.

Uploaded by

Abdul Sammad
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)
72 views

Course Title: CP - LAB: Bahria University, Islamabad

The document is a lab journal for a student's CP-LAB course. It contains 3 tasks completed by the student: 1) A function to swap 3 numbers passed by reference, 2) A function to set the smaller of two numbers passed by reference to 0, 3) A function to add 13 to a number if it is even, otherwise leave it unchanged. The student provides the objectives, tools used, procedures, and outputs for each task.

Uploaded by

Abdul Sammad
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/ 6

Course Title: CP -LAB

Course Code: CSL-113

Lab Journal: 08

Student Name: Abdul Sammad Khan.


Enrolment No: 01-135201-004.
Class and Section: BS(IT)-1B.

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Lab #: 08

Objectives:

Functions

Tools Used:

Visual studio, snipping tool

Submission Date: 22ndApril,2020.

Evaluation: Signatures of Lab Engineer:


Task # 1:
Write a program in C++ that have a function Swap(). Take three numbers from user, pass these
numbers in function Swap() which will swap first value with second, second value with third
value and third value by first value. Then display these swapped values in main(). Think yourself
how should you pass these values? By value or reference?

Procedure/Program:
#include<iostream>
#include<conio.h>
using namespace std;
int Swap (int* a, int* b, int* c);
int main ()
{
int a, b, c; a = 5, b = 3, c = 4 ;
cout << "Enter value of a:" << a << endl;
cout << "Enter value of b:" << b << endl;
cout << "Enter value of c:" << c << endl;
Swap(&a, &b, &c);

cout << "Value after swapping numbers : " << endl;


cout << "value of a : " << a << endl;
cout << "value of b : " << b << endl;
cout << "value of c : " << c << endl;

return 0;
}

int Swap(int* a, int* b, int* c)


{
int temp;
temp = *b;
*b = *a;
*a = *c;
*c = temp;
return 0;
}

Result/Output:
Task # 2:
Write a function called zero Smaller (int&,int&) that will be passed two int arguments by
reference and then sets the smaller of the two numbers to 0. Write main() to display changed
values.

Procedure/Program:
#include<iostream>
#include<conio.h>
using namespace std;

int zero_small(int&, int&);

int main()
{
int x, y;
cout << "Enter first number : ";
cin >> x;
cout << "Enter second number : ";
cin >> y;
zero_small(x, y);
cout << "First number is : " << x;
cout << "\nSecond number is : " << y;

return 0;
}

int zero_small(int &a, int &b)


{
if (a < b)
a = 0;
else
b = 0;
return 0;
}

Result/Output:
Task # 3:
Write a function change Value() which takes one int argument. Check the value if it is even then
add 13 in the value and if it is odd do not change anything in it. Display the changed value in
main().

Procedure/Program:
#include<iostream>
#include<conio.h>
using namespace std;
int value(int);
int main()
{
int a;
cout << "Enter a number: ";
cin >> a;
int r = changevalue(a);
}
int value(int a)
{
if (a % 2 == 0)
{
cout << "Number is even." << endl;
int r = a + 13;
cout << "changed value is " << r << endl;
}
else
cout << "Number is odd." << endl;
return a;
}

Result/Output:

Even:

ODD:

You might also like