0% found this document useful (0 votes)
6 views12 pages

C++ Exp 1a To 1d

The document outlines a series of C++ programming exercises focused on using functions with default arguments, specifically for calculating interest on fixed deposit accounts and swapping integers using different methods. It includes detailed descriptions of basic Linux commands, sample code, and expected outputs for each exercise. Additionally, it emphasizes the implementation of functions to handle user inputs and default values effectively.

Uploaded by

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

C++ Exp 1a To 1d

The document outlines a series of C++ programming exercises focused on using functions with default arguments, specifically for calculating interest on fixed deposit accounts and swapping integers using different methods. It includes detailed descriptions of basic Linux commands, sample code, and expected outputs for each exercise. Additionally, it emphasizes the implementation of functions to handle user inputs and default values effectively.

Uploaded by

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

CS22203 - OBJECT ORIENTED PROGRAMMING

IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY


EX NO : 1 a)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


QUESTION :
AIM: Functions with default arguments
You are tasked with developing a C++ program for a bank. The program should
You are tasked
calculate with developing
the interest for a fixedadeposit
C++ program
account.forThe
a bank. The is
interest program should
calculated based
calculate the
COMMANDS interest
AND for a
THEIR DESCRIPTION:
on the principal amount, rate of interest, and the duration of the deposit. However,
fixed
if thedeposit account.and
rate of interest Thetheinterest is calculated
duration based on
are not provided bythe
theprincipal
user, theamount,
program
rate of 1. Description:
interest, and
should use default values.theCreating a directory
duration ofCommand:
the deposit. $mkdir ppsl147if the rate of interest and the duration are not
However,
provided
Create a by the user,
function thatthewill calculate the interest, where:
program 2. should
Description: Changing
use default directory
values.
Create Command:
a function that$cd
willppsl147
● The principal amount iscalculate
mandatory the(must
interest, where: by the user).
be provided
● The principal amount is mandatory (must be provided by the user).
●●The 3. Description: FindthePresent Working Directory (the program should use
Therate of
ofinterest
rateCommand: and
interest$pwd and theduration
durationareareoptional
optional (the program should use
default
defaultvalues
values ififthey
they are not provided).
Output: /home/cs23c/cs23c147/ppsl147
are not provided).
Write
Writeaa4.C++
C++ program
programthat that uses
uses functions with default arguments to
tosolve
solvethis
Description: List all filesfunctions with
& directories defaultworking
in present arguments
directory this
problem.
problem Command: $ls
Output: ex1
AIM:
5. Description:
To develop a C++ programReturning to home directory
that calculates the interest for a fixed deposit account using
Command: $cd ..
functions with default arguments.
6. Description: Create new file
ALGORITHM :
Command: $ cat >f1
Output: this is sidharth
1. Start ^c
2. Input the principal amount.
3. Calculate interest using
7. Description: Viewthefile
default rate (5.0%) and time (1 year).
4. Display the interest.
Command: $cat f1
5. CalculateOutput:
interestthis
forisrate = 6.5% and default time (1 year).
sidharth
6. Display the interest.
7. Calculate interest for Appending
8. Description: rate = 7.0% and time
content = 2 years.
to existing file
8. Display the interest. $cat >>f1
Command:
9. End Output: I am a cs student
^z
[1]+stopped cat>>f1

REGISTRATION NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE 1


CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 a)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS
PROGRAM :
BASIC LINUX COMMANDS
PROGRAM :
AIM:
#include<iostream>
using namespace std;
void COMMANDS
calculateintrestAND
(floatTHEIR
principal, float rate = 5.0, int time=1)
DESCRIPTION:
{
1. Description:
float intrest = (principalCreating a directory
*rate*time)/100;
Command: $mkdir
cout<<"Intrest:"<<intrest<<endl; ppsl147
}
2. Description: Changing directory
int main()
Command: $cd ppsl147
{
float principal;
3. Description: Find Present Working Directory
cout<<"Enter principal
Command: $pwdamount:";
cin>>principal;
Output: /home/cs23c/cs23c147/ppsl147
calculateintrest (principal);
calculateintrest (principal,
4. Description: 6.5);
List all files & directories in present working directory
Command:
calculateintrest $ls 7.0,2);
(principal,
return 0; Output: ex1
}
SAMPLE5.INPUT Description: Returning: to home directory
AND OUTPUT
Command: $cd ..
[cs24b077@cslinux ~]$ vi la.cpp
6. Description:
[cs24b077@cslinux ~]$ Create new file
c++ 1a.cpp
Command:
[cs24b077@cslinux ~]$ ./a.out$ cat >f1
Enter principalOutput: this is sidharth
amount:10000
Intrest:500 ^c
Intrest:650
7. Description: View file
Command: $cat f1
Output: this is sidharth

8. Description: Appending content to existing file


Command: $cat >>f1
Output: I am a cs student
RESULT : ^z
[1]+stopped cat>>f1
Thus the C++ Program "Functions with Default Arguments " is executed and the output is
verified successfully NUMBER: 2127230501082
REGISTRATION 1|Page
Registration Number : 2127240501081 Page 1

REGISTRATION NUMBER : 2127240501077 PAGE 2

PAGE 1
CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 b)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


QUESTION :
AIM:
Implementation of Call by Value, Call by Address and Call by Reference
COMMANDS AND THEIR DESCRIPTION:
You
You are
are developing
developing aa utility
utility for
for aa programming
programming course coursetotodemonstrate
demonstratedifferent
different
ways to 1. Description:
swap two Creating a directory
ways to swap two integers. The goal is to write a C++ program that swaps the
integers.
values ofThe Command:
two goal is to$mkdir
integers write
in threeppsl147
a C++ program
different ways:that swaps the values of two
integers in three different ways:
1. Call by2. Value:
Description: Changing
The function directory
should take the values of the integers and attempt
to swap Command: $cd ppsl147
them.
1. Call by Value: The function should take the values of the integers and attempt to
2. Call them.
swap by Address: The function should use pointers to swap the integers by
modifying 3. their
Description: Find Present Working Directory
memory Command: $pwd
2. Call byaddresses.
Address: The function should use pointers to swap the integers by
Output: /home/cs23c/cs23c147/ppsl147
3. Call by Reference: The function should swap the integers by directly
modifying their memory addresses.
accessing them using
4. Description: List all files & directories in present working directory
references.
3. Call by Reference:
Command: The $ls function should swap the integers by directly accessing
Write a C++ program
them usingOutput:
references.to perform the swapping of two integers using all three
ex1
methods: call by value, call
by address,
Write a C++ and call by
program reference.
toReturning
performtothe swapping
5. Description: home directory of two integers using all three
methods: call by value,
Command: $cd .. call by address, and call by reference.
AIM : To implement a C++ program that demonstrates swapping of two integers using
6. Description: Create new file
three different methods:
Command: $ cat >f1
Call by Value , Call by
thisAddress (using pointers), Call by Reference
Output: is sidharth
ALGORITHM : ^c
ALGORITHM :
7. Description: View file
1. Start Command: $cat f1
2. InputOutput: this isxsidharth
two numbers, and y.
3. Display the numbers
8. Description: before swapping.
Appending content to existing file
Command: $cat >>f1
4. Swap using call by value (no actual change in x and y) and display the result.
Output: I am a cs student
5. Swap using call^zby address (pointers) and display the result.
[1]+stopped
6. Swap using call cat>>f1
by reference and display the result.
7. End
REGISTRATION NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE


PAGE 31
CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 b)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


PROGRAM :
AIM:
#include<iostream>
using namespace std;
void swapbyvalue
COMMANDS(int a, int
AND b)
THEIR DESCRIPTION:
{
1. Description: Creating a directory
int temp =a;
Command: $mkdir ppsl147
a=b; b=temp;
2. Description: Changing directory
cout<<"Inside swap by value: a="<<<a<<" b="<<b<<endl;
Command: $cd ppsl147
}
void swapbyadd (int *a, int
3. Description: *b)Present Working Directory
Find
{ Command: $pwd
Output: /home/cs23c/cs23c147/ppsl147
int temp = *a;
*a=b; 4. Description: List all files & directories in present working directory
Command: $ls
*b=temp;
Output: ex1
}
5. Description:
void swapbyref (int &a, intReturning
&b) to home directory
Command: $cd ..
{
int temp 6.
= a;Description: Create new file
Command: $ cat >f1
a=b;
Output: this is sidharth
b=temp; ^c
}
7. Description: View file
int main() Command: $cat f1
int x,y; Output: this is sidharth
cout<<"Enter two numbers:";
8. Description: Appending content to existing file
cin>>y; Command: $cat >>f1
Output:
cout<<"Before I am a cs student
swapping:x="<<x<<”\t”<<y<<endl;
^z
swapbyvalue (x, y); [1]+stopped cat>>f1
cout<<"After swap by value:x="<<x<<",y="<<y<<endl;
REGISTRATION
swapbyadd (&x, &y); NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE


PAGE 1 4
CS22203 - OBJECT ORIENTED PROGRAMMING
1|Page
EX NO : 1 b)

DATE :
PROGRAMS USING FUNCTIONS

cout<<"After swap by address:x="<<x<<",y="<<y<<endl;


swapbyref (x, y);
cout<<"After swap by reference:x="<<x<<",y="<<y<<endl;
return 0;
}

SAMPLE INPUT AND OUTPUT :


[cs24b077@cslinux ~]$ vi 1b.cpp
[cs24b077@cslinux ~]$ c++ 1b.cpp
[cs24b077@cslinux ~]$ ./a.out
Enter two numbers:10
20
Before swapping: x = 10 , y = 20
Inside swap by value: a = 20 , b = 10
After swap by value: x = 10 , y = 20
After swap by address: x = 20 , y = 10
After swap by reference: x = 10 , y = 20

RESULT :

Thus the C++ Program " Implementation of Call by Value, Call by Address and Call by
Reference " is executed and the output is verified successfully

PAGE 1
REGISTRATION NUMBER : 2127240501077 PAGE 5
CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 c)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


QUESTION :
AIM: Volume of a Box with Default Arguments

You are developing a 3D modeling application that allows users to calculate the
volume of various AND
COMMANDS shapes, including
THEIR rectangular boxes. To make the calculation
DESCRIPTION:
process easier for users, the system should automatically use default dimensions
for a box1.if Description:
the user does not provide
Creating specific values for its length, width, and
a directory
height. Command: $mkdir ppsl147

Your task 2. isDescription:


to write a C++Changing
programdirectory
that calculates the volume of a rectangular box.
The program Command: $cd ppsl147
should allow the user to input the dimensions (length, width, and
height) of the box. If any of these dimensions are not provided by the user, the
3. Description:
system should Find Present
use default values.Working Directory
Command: $pwd
Write a C++Output:
program /home/cs23c/cs23c147/ppsl147
that implements a function to calculate the volume, with default
values for length, width, and height. If the user provides some or all of the
4. Description:
dimensions, the program Listshould
all files & directories
use in present
those values; working directory
otherwise, it should use the
default ones Command: $ls
Output: ex1
AIM :
AIM :
To write 5.
a C++Description:
program Returning to home
that calculates thedirectory
volume of a rectangular box using a function
with default Command:
arguments.$cd The.. program should allow the user to input length, width, and
height. If the user does not provide specific values, the system should use default values.
6. Description: Create new file
ALGORITHM :
Command: $ cat >f1
ALGORITHM :
Output: this is sidharth
^c
1. Start
7. Description: View file
2. Define a function
Command: volume(length,
$cat f1 width, height) with default values:
Output: this is sidharth
3. length = 10, width = 5, height = 2
8. Description:
4. Calculate Appending
volume as length * widthcontent
* heighttoand
existing
returnfile
it.
Command: $cat >>f1
5. Call volume() withIdefault
Output: am a csvalues and display the result.
student
6. Call volume(8,6,4)^zwith user values and display the result.
7. End [1]+stopped cat>>f1

REGISTRATION NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE 6


1
CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 c)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


PROGRAM :
AIM:
#include<iostream>
usingCOMMANDS
namespace std;AND THEIR DESCRIPTION:
float volume (float length=10,float width=5, float height =2)
{ 1. Description: Creating a directory
return length*width*height;
Command: $mkdir ppsl147
}
int main()
2. Description: Changing directory
{ Command: $cd ppsl147
cout<<"Volume with default values: "<<volume()<<endl;
3. Description:
cout<<"Voume with userFind Present
value (8,6,Working Directory
4) "<<volume (8,6,4)<<endl;
return 0; Command: $pwd
} Output: /home/cs23c/cs23c147/ppsl147

SAMPLE4.INPUT
Description: List all files
AND OUTPUT : & directories in present working directory
Command: $ls
[cs24b077@cslinux ~]$ vi 1c.cpp
Output: ~]$
[cs24b077@cslinux ex1 c++ 1c.cpp
[cs24b077@cslinux ~]$ ./a.out
5. Description:
Volume with Returning
default values: 100 to home directory
Voume with Command: $cd .. 19
user value (8,6,4)
6. Description: Create new file
Command: $ cat >f1
Output: this is sidharth
^c

7. Description: View file


Command: $cat f1
Output: this is sidharth

8. Description: Appending content to existing file


Command: $cat >>f1
Output: I am a cs student
RESULT : ^z
[1]+stopped cat>>f1 PAGE 1
Thus the C++ Program " Volume of a Box with Default Arguments " is executed and the
output is verified successfully
REGISTRATION NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE 7


CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 d)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


QUESTION :
AIM: Call by Address using Default Arguments

You are developing a program for a financial application where the system needs to
COMMANDS
perform AND THEIR The
division calculations. DESCRIPTION:
program will divide two numbers, but in some
cases, the user might not provide the second number. If the second number is not
1. the
provided, Description: Creatingdefault
system should a directory
to dividing by 1 to avoid errors.
Command: $mkdir ppsl147
Your task is to write a C++ program where a function takes pointers to modify the
2. Description: Changing directory
values of the numbers.
Command: $cd ppsl147
If the user
3. provides both
Description: numbers,
Find the function
Present Working should divide them. However, if the
Directory
second number is not$pwd
Command: provided, the function should default to using 1 as the divisor.
Output: /home/cs23c/cs23c147/ppsl147
Write a C++ function that performs the division operation with the flexibility to
handle a4.default value for
Description: Listthe second
all files numberinusing
& directories presentpointers.
working directory
Command: $ls
AIM : Output: ex1
To write a C++ program that performs division using call by address and default
5. Description: Returning to home directory
arguments, ensuring that if the second number is not provided, it defaults to 1.
Command: $cd ..
ALGORITHM :
ALGORITHM6. Description:
: Create new file
1. Start Command: $ cat >f1
Output: this is sidharth
2. Input an integer a.
^c
3. Ask the
7. user if they want
Description: Viewtofile
enter a second value (b).
Command: $cat f1
4. If "yes", input b; otherwise, set b = 1.
Output: this is sidharth
5. In the divide() function:
8. Description: Appending content to existing file
6. If b == 0,Command:
set b = 1 to$cat >>f1
avoid division by zero.
Output: I am a cs student
7. Compute c = a / b. ^z
[1]+stopped cat>>f1
8. Print the result.
REGISTRATION NUMBER: 2127230501082 1|Page
9. End

REGISTRATION NUMBER : 2127240501077 PAGE 8


CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 d)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS


PROGRAM :
AIM:
#include<iostream>

using namespace std;


COMMANDS
void divide(int a, int *b) AND THEIR DESCRIPTION:
{
1. Description: Creating a directory
int c;
Command: $mkdir ppsl147
if (*b==0)
{
*b=1; 2. Description: Changing directory
} Command: $cd ppsl147
C=a / *b;
cout<<"The divided value is:"<<c;
} 3. Description: Find Present Working Directory
int main() Command: $pwd
int a,b; Output: /home/cs23c/cs23c147/ppsl147
cout<<"Enter the value of a:";
cin>>a;
string flag; 4. Description: List all files & directories in present working directory
cout<<"Do you wantCommand: $lssecond value: (yes/no)";
to enter the
cin>>flag; Output: ex1
if (flag=="yes")
{
cout<<"Enter 5. theDescription:
value of b:"; Returning to home directory
cin>>b; Command: $cd ..
}
else{
b=1; 6. Description: Create new file
} Command: $ cat >f1
divide (&a, &b);Output: this is sidharth
return 0; ^c
} Registration Number : 2127240501081 Page 1
7. Description: View file
Command: $cat f1
Output: this is sidharth

8. Description: Appending content to existing file


Command: $cat >>f1
Output: I am a cs student
^z
[1]+stopped cat>>f1

REGISTRATION NUMBER: 2127230501082 1|Page

REGISTRATION NUMBER : 2127240501077 PAGE


PAGE
9 1
CS22203 - OBJECT ORIENTED PROGRAMMING
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
EX NO : 1 d)
EX.NO:1
DATE :
DATE: 9-10-2023
PROGRAMS USING FUNCTIONS

BASIC LINUX COMMANDS

SAMPLE
AIM: INPUT AND OUTPUT :
[cs24b077@cslinux ~]$ ./a.out
EnterCOMMANDS
the value of a:10
AND THEIR DESCRIPTION:
Do you want to enter the second value:(yes/no)yes
1. Description: Creating a directory
Enter the value of b:2 $mkdir ppsl147
Command:
The divided value is:5
2. Description: Changing directory
Command: $cd ppsl147

3. Description: Find Present Working Directory


Command: $pwd
Output: /home/cs23c/cs23c147/ppsl147

4. Description: List all files & directories in present working directory


Command: $ls
Output: ex1

5. Description: Returning to home directory


Command: $cd ..

6. Description: Create new file


Command: $ cat >f1
Output: this is sidharth
^c

7. Description: View file


Command: $cat f1
Output: this is sidharth

8. Description: Appending content to existing file


Command: $cat >>f1
RESULT : Output: I am a cs student
^z
Thus the C++ Program " Call by
[1]+stopped Address using Default Arguments " is executed and the
cat>>f1
output is verified successfully
Registration Number
REGISTRATION : 2127240501081
NUMBER: 2127230501082 1Page
| P a g1e

PAGE 1
REGISTRATION NUMBER : 2127240501077 PAGE 10

You might also like