Menu

Wednesday, 13 May 2015

Chapter 5,Problem k: Let Us C

Problem Statement:

Write a function to compute the greatest common divisor
given by Euclid’s algorithm, exemplified for J = 1980, K =
1617 as follows:
1980 / 1617 = 1 1980 – 1 * 1617 = 363
1617 / 363 = 4 1617 – 4 * 363 = 165
363 / 165 = 2 363 – 2 * 165 = 33
5 / 33 = 5 165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.

Solution:

//Ahmad Furqan
//P5.k
#include <iostream>
#include <conio.h>
using namespace std;
int gcd_euclid(int j, int k)
{
int a, b;
while (k > 0)
{
a = j / k;
b = k;
k = j - a*k;
j = b;
}
return j;
}
void main(void)
{
int j, k;
cout << "Enter a number: ";
cin >> j;
cout << "Enter second number: ";
cin >> k;
cout << "Greatest common divisor of " << j << " and " << k << " is: " << gcd_euclid(j, k);
_getch();
}

Chapter 5,Problem j: Let Us C

Problem Statement:

Write a function to compute the distance between two points
and use it to develop another function that will compute the
area of the triangle whose vertices are A(x1, y1), B(x2, y2),
and C(x3, y3).

Solution:

//Ahmad Furqan
//P5.j
#include <iostream>
#include <conio.h>
using namespace std;
float pdist(int x1, int y1, int x2, int y2)
{
float dist;
dist = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
return dist;
}
float triarea(float a, float b, float c)
{
float s;
float area;
s = (a + b + c) / 2;
area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
void main(void)
{
int x1, x2, x3, y1, y2, y3;
float a, b, c;
cout << "Enter point 1: ";
cout << "Enter x: ";
cin >> x1;
cout << "Enter y:";
cin >> y1;
cout << "Enter point 2:";
cout << "Enter x:";
cin >> x2;
cout << "Enter y";
cin >> y2;
cout << "Enter point 3:";
cout << "Enter x:";
cin >> x3;
cout << "Enter y:";
cin >> y3;
a = pdist(x1, y1, x2, y2);
b = pdist(x2, y2, x3, y3);
c = pdist(x1, y1, x3, y3);
cout << "Area of triangle bounded by these points is: " << triarea(a, b, c);
_getch();
}

Chapter 5,Problem i: Let Us C

Problem Statement:

If the lengths of the sides of a triangle are denoted by a, b,
and c, then area of triangle is given by
area = S(S a)(S b)(S c)
where, S = ( a + b + c ) / 2

Solution:

//Ahmad Furqan
//P5.i
#include <iostream>
#include <conio.h>
using namespace std;
float triarea(int a, int b, int c)
{
int s;
float area;
s = (a + b + c) / 2;
area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
void main(void)
{
int a, b, c;
cout << "Enter triangle side lengths:" << endl;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;
cout << "Area of the triangle is: " << triarea(a, b, c);
_getch();

}

Chapter 5,Problem h: Let Us C

Problem Statement:

Write a function to find the binary equivalent of a given
decimal integer and display it.

Solution:

//Ahmad Furqan
//P5.h
#include <iostream>
#include <conio.h>
using namespace std;
void num2bin(int num)
{
bool bin[25];
int i = 0;
for (i = 0; i < 25; i++)
{
bin[i] = num % 2; //divide number by 2 successively and save remainders
if (num < 2)
break;
num /= 2;
}
for (i; i >= 0; i--)
{
cout << bin[i];  //show remainders in reverse order
}
}
void main(void)
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "The binary equivalent of this number is: ";
num2bin(num);
_getch();
}

Chapter 5, Problem g: Let Us C

Problem Statement:

Given three variables x, y, z write a function to circularly shift
their values to right. In other words if x = 5, y = 8, z = 10 after
circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8
and x = 10. Call the function with variables a, b, c to
circularly shift values.

Solution:

//Ahmad Furqan
//P5.g
#include <iostream>
#include <conio.h>
using namespace std;
void rotate(int &a, int &b, int &c)
{
int temp;
temp = c; //store c to a temporary variable
c = b; //move b to c
b = a; //move a to b
a = temp; //move temp to a,where temp contains c's original value
return;
}
void main(void)
{
int a, b, c;
a = 15;
b = 13;
c = 53;
cout << "Before Rotation:" << endl;
cout << "a=" << a << " ,b=" << b << " ,c=" << c << endl;
rotate(a, b, c);
cout << "After Rotation:" << endl;
cout << "a=" << a << " ,b=" << b << " ,c=" << c << endl;
_getch();
}

Chapter 5,Problem f: Let Us C

Problem Statement:

Write a C function to evaluate the series 
sin(x) = x − (x 3 / 3!) + (x 5 / 5!) − (x 7 / 7!) +Lto five significant digits.

Solution:

//Ahmad Furqan
//P5.f
#include <iostream>
#include <conio.h>
using namespace std;
int fact(int num)
{
int f = num;
for (int i = num-1; i > 0; i--)
f = f*i;
return f;
}
int pow(int a, int b)
{
int p = a;
for (int i = 1; i < b; i--)
p *= a;
return p;
}
float sin_ser(float ang)
{
float s;
s = ang - (pow(ang, 3) / fact(3)) + (pow(ang, 5) / fact(5)) - (pow(ang, 7) / fact

(7))+(pow(ang,9)/fact(9));
return s;
}
void main(void)
{
float ang;
cout << "Enter an angle: ";
cin >> ang;
cout << "sin(" << ang << ")= " << sin_ser(ang);
_getch();
}

Chapter 5,Problem e:Let Us C by Yashawant Kanetkar Solution manual

Problem Statement:

Write a recursive function to obtain the running sum of first
25 natural numbers.

Solution:

//Ahmad Furqan
//P5.e
#include <iostream>
#include <conio.h>
using namespace std;
int sum_nat(int num)
{
if (num == 25)
return num;
return num + sum_nat(num + 1);  //return current num + sum of numbers above this num and upto 25
}
void main(void)
{
cout << "Sum of first 25 Natural Numbers is: ";
cout << sum_nat(1);
_getch();
}