Objective:
- Functions in C++
Activity 1:
Write C++ program
that will ask the user to enter the width and length of a rectangle and then display
the rectangle’s area. The program calls the following functions, which have not
been written:
- getLength: This
function should ask the user to enter the rectangle’s length and then return
that value as a double .
- getWidth: This
function should ask the user to enter the rectangle’s width and then return
that value as a double .
- getArea: This
function should accept the rectangle’s length and width as arguments and return
the rectangle’s area. The area is calculated by multiplying the length by the
width.
- displayData: This function should accept the rectangle’s length, width, and area as arguments and display them in an appropriate message on the screen.
Solution:
#include<iostream>
using namespace std;
double getLength()
{
double length;
cout << "Enter Length of Rectangle: " << endl;
cin >> length;
return length;
}
double getWidth()
{
double width;
cout << "Enter Width of Rectangle: " << endl;
cin >> width;
return width;
}
double getArea(double i, double j)
{
double Area;
Area = i * j;
return Area;
}
void displayData(double A)
{
cout << "Area of Rectangle is: " << A << endl;
}
int main()
{
double x, y, area;
x = getLength();
y = getWidth();
area = getArea(x, y);
displayData(area);
return 0;
}
Activity 2:
Write a
function called displayShape that
accepts size and shapetype as parameters and displays a square or a triangle of
the required size.
So e.g. if
the function is called with the following parameters:
displayShape(‘S’, 5); a square of
size 5 is displayed as given
1 |
2 |
3 |
4 |
5 |
2 |
4 |
6 |
8 |
10 |
3 |
6 |
9 |
12 |
15 |
4 |
8 |
12 |
16 |
20 |
5 |
10 |
15 |
20 |
25 |
displayShape(‘T’, 6); a triangle
of size 6 is displayed as given
1
1
1
1
2 1
1
3 3 1
1
4 6 4 1
1
5 10
10 5 1
Solution:
#include<iostream>
using namespace std;
void print_square(int Size)
{
int num, i, j, res;
for (i = 1; i <= Size; i++)
{
num = i;
for (j = 1; j <= Size; j++)
{
res = num * j;
cout << res << "\t";
}
cout << endl;
}
cout << endl;
}
void print_triangle(int Size)
{
int coef = 1;
for (int i = 0; i < Size; i++)
{
for (int space = 1; space <= Size - i; space++)
{
cout << " ";
}
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{
coef = 1;
}
else
{
coef = coef * (i - j + 1) / j;
}
cout << coef << " ";
}
cout << endl;
}
}
int main()
{
char choice2 = 'y';
do {
int size;
char choice;
cout << "Enter Size of Shape: " << endl;
cin >> size;
cout << "Enter s for Square-shape or t for triangle-shape: " << endl;
cin >> choice;
if (choice == 's')
{
print_square(size);
}
if (choice == 't')
{
print_triangle(size);
}
cout << "If you want to Repeat then press y. " << endl;
cin >> choice2;
} while (choice2 == 'y');
return 0;
}
Activity 3:
Write a program that converts a binary number into decimal number.
Solution:
#include<iostream>
#include<cmath>
using namespace std;
int decimalConverter(int num)
{
int dec = 0, i = 0, rem;
bool possible = true;
while (num != 0)
{
rem = num % 10;
num /= 10;
if (rem == 0 || rem == 1)
{
dec += rem * pow(2, i);
++i;
}
else
{
cout << "Conversion not possible: it is not a binary number." << endl;
bool possible = false;
break;
}
}
if (possible == true)
{
return dec;
}
}
int main()
{
int n;
cout << "Enter a Binary Number: ";
cin >> n;
cout << "Decimal Number: " << decimalConverter(n) << endl;
return 0;
}