Assignment 4
Assignment 4
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:
OUTPUT:
#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:
OUTPUT: