0% found this document useful (0 votes)
51 views7 pages

Assignment 4

The document contains details of 8 C++ programs: 1) Demonstrates namespaces by defining swap functions for integers and characters in different namespaces. 2) Defines inline cube and square functions in a class. 3) Modifies program 2 to define cube and square functions outside the class using scope resolution operator. 4) Demonstrates call by value, reference, and address by passing values to swap functions. 5) Uses manipulators setw(), setprecision(), and setfill(). 6) Converts a decimal number to binary and applies iterations to transform bits. 7) Removes common characters between strings and concatenates uncommon characters. 8) Computes a recursively defined function F(m,n).

Uploaded by

fsfsfs
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)
51 views7 pages

Assignment 4

The document contains details of 8 C++ programs: 1) Demonstrates namespaces by defining swap functions for integers and characters in different namespaces. 2) Defines inline cube and square functions in a class. 3) Modifies program 2 to define cube and square functions outside the class using scope resolution operator. 4) Demonstrates call by value, reference, and address by passing values to swap functions. 5) Uses manipulators setw(), setprecision(), and setfill(). 6) Converts a decimal number to binary and applies iterations to transform bits. 7) Removes common characters between strings and concatenates uncommon characters. 8) Computes a recursively defined function F(m,n).

Uploaded by

fsfsfs
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/ 7

Name: Mandip Sah

Roll no: 21053289


Section: CSE-16
Date: 01-08-2022
1. Write a program in c++ to demonstrate namespace working principle with same
functions swap() in different namespaces.
#include <iostream>
using namespace std;
int a, b;
char c[1];
char d[1];
class swap_value
{

public:
void get1()
{
cout << "Enter the two integer values: ";
cin >> a >> b;
}
void get2()
{
cout << "Enter the two character values: ";
cin >> c >> d;
}
};
namespace first
{
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
cout << "The values after swapping are: " << *a << " " << *b << endl;
}
}
namespace second
{
void swap(char *c, char *d)
{
char temp = *c;
*c = *d;
*d = temp;
cout << "The values after swapping are: " << *c << " " << *d << endl;
}
}
int main()
{
swap_value f1, f2;
f1.get1();
f2.get2();
first::swap(&a, &b);
second::swap(c, d);
return 0;
}
OUTPUT:

2. Write a program in C++ to demonstrate inline function to find cube and square of a
number in a class.

#include <iostream>
using namespace std;
class demonstrate
{
int num;

public:
void get()
{
cout << "Enter the number:";
cin >> num;
}
inline int cube()
{
return num * num * num;
}
inline int square()
{
return num * num;
}
};
int main()
{
demonstrate d1;
d1.get();
cout << "The cube of entered number is:" << d1.cube() << endl;
cout << "The square of entered number is:" << d1.square() << endl;
return 0;
}

OUTPUT:

3. Modify program 2 and define the same functions outside the class iwith the help of
scope resolution operator.

#include <iostream>
using namespace std;
class demonstrate
{
int num;

public:
void get();
int cube();
int square();
};
inline void demonstrate::get()
{
cout << "Enter the number:";
cin >> num;
}
inline int demonstrate::cube()
{
return num * num * num;
}
inline int demonstrate::square()
{
return num * num;
}
int main()
{
demonstrate d1;
d1.get();
cout << "The cube of entered number is:" << d1.cube() << endl;
cout << "The square of entered number is:" << d1.square() << endl;
return 0;
}

OUTPUT:

4. Write a programin C++ to demonstrate call by value, call by reference and call by
address for swapping two numbers.

#include <iostream>
using namespace std;
void value(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void address(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void reference(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
int a, b;
cout << "Enter the two numbers: ";
cin >> a >> b;
value(a, b);
cout << "Change in value during call by address: " << a << " " << b << endl;
address(&a, &b);
cout << "No change in value during call by value: " << a << " " << b << endl;
reference(a, b);
cout << "Change in value during call by reference: " << a << " " << b << endl;
return 0;
}

OUTPUT:

5. Write a program in C++ to demonstrate setw(), setprecision () and setfill()


manipulators.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.141592653589;
cout << setprecision(5.4) << pi << endl;
cout << "Mandip" << setw(8) << "sah" << endl;
cout << "Mandip" << setfill('*') << setw(8) << "sah" << endl;
return 0;
}

OUTPUT:

6. WAP to input a decimal number m. Convert it in binary string and apply n


iterations, in each iteration 0 becomes 01 and 1 becomes 10. Find kth character in the
string after nth iteration.

#include <iostream>
#include <string>
using namespace std;
int decimalToBinary(int n)
{
int ans = 0;
int x = 1;
int lastdigit;
while (n >= x)
x = x * 2;
x = x / 2;
while (n > 0)
{
lastdigit = n / x;
n = n - lastdigit * x;
x = x / 2;
ans = ans * 10 + lastdigit;
}
return ans;
}
int main()
{
int n;
cout << "Enter the number: ";
cin >> n;
int ans = decimalToBinary(n);
string str = to_string(ans);
int counter = 0;
int i = 0;
string str2;
while (str[counter] != '\0')
{
if (str[counter] == '0')
{
str2[i] = '1';
i++;
str2[i] = '0';
i++;
}
if (str[counter] == '1')
{
str2[i] = '0';
i++;
str2[i] = '1';
i++;
}
counter++;
}
int key;
cout << "Enter the key:";
cin >> key;
cout << "The character at the requires position is: " << str2[key];
return 0;
}

OUTPUT:

7. WAP to input two strings. Modify 1st string such that all the common characters of
the 2nd strings have to be removed and the uncommon characters of the 2nd string
have to be concatenated with uncommon characters of the 1st string. If the modified
string is empty then print '-1'.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char s1[50];
char s2[50];
cout << "Enter the first and second string respectively: ";
cin >> s1 >> s2;
int n1 = strlen(s1);
int n2 = strlen(s2);
int arr1[26] = {0};
int arr2[26] = {0};
char s3[50];
char s4[50];
int curr1 = 0;
int curr2 = 0;
for (int i = 0; i < n1; i++)
arr1[s1[i] - 'a'] = -1;
for (int i = 0; i < n2; i++)
arr2[s2[i] - 'a'] = -1;
for (int i = 0; i < n1; i++)
if (arr2[s1[i] - 'a'] != -1)
{
s3[curr1] = s1[i];
curr1++;
}
s3[curr1] = '\0';
for (int i = 0; i < n1; i++)
if (arr1[s2[i] - 'a'] != -1)
{
s4[curr2] = s2[i];
curr2++;
}
s4[curr2] = '\0';
string s5, s6;
s5 = string(s3);
s6 = string(s4);
string s_res = s5 + s6;
cout << "The required string is " << s_res;
return 0;
}

OUTPUT:

8. WAP tocompute F(m, n) where F(m, n) can be recursively defined as F(m, n) = 1 if


m=0 or m>= n >=1 F(m, n) = F(m, n-1) + F(m-1, n-1) otherwise
#include <iostream>
using namespace std;
int define(int m, int n)
{
if (m == 0 || (m >= n && n >= 1))
{
return 1;
}
return define(m, n - 1) + define(m - 1, n - 1);
}
int main()
{
int m, n;
cout << "Enter the value of m and n respectively: ";
cin >> m >> n;
int result = define(m, n);
cout << "F(m,n) = " << result;
return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy