//Ahmad Furqan Attari
//p7.4
#include <iostream>
#include <conio.h>
using namespace std;
const int SIZE = 10;
int maxint(int (&num)[SIZE],int max)
{
int large=num[0],index=0;
for (int i = 1; i < max; i++)
{
if (num[i]>large)
{
large = num[i];
index = i;
}
}
return index;
}
void main(void)
{
int num[SIZE];
char ch;
int i = 0;
do
{
cout << "Enter a number: ";
cin >> num[i];
i++;
cout << "Enter again?(y/n): ";
ch = _getche();
cout << endl;
} while (ch == 'y' && i < SIZE);
cout << "Greatest number entered: " << num[maxint(num,i)];
_getch();
}
Sunday, 30 November 2014
Chapter 7,Problem 4:Object Oriented Programming by Robert Lafore in C++ Solution Manual
Chapter 7,Problem 3:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Ahmad Furqan Attari
// P7.3
// averages an array of Distance objects input by user
#include <iostream>
#include <conio.h>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance // English Distance class
{
private:
int feet;
float inches;
public:
Distance() //constructor (no args)
{
feet = 0; inches = 0;
}
Distance(int ft, float in) //constructor (two args)
{
feet = ft; inches = in;
}
void getdist() //get length from user
{
cout << "\nEnter feet : "; cin >> feet;
cout << "Enter inches : "; cin >> inches;
}
void showdist() //display distance
{
cout << feet << "\' - " << inches << '\"';
}
void add_dist(Distance d2, Distance d3)
{
inches = d2.inches + d3.inches; //add the inches
feet = 0; //(for possible carry)
if (inches >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
inches -= 12.0; //by 12.0 and
feet++; //increase feet
} //by 1
feet += d2.feet + d3.feet; //add the feet
}
void div_dist(Distance d2, int divisor)
{
float fltfeet = d2.feet + d2.inches / 12.0; //convert to float
fltfeet /= divisor; //do division
feet = int(fltfeet); //get feet part
inches = (fltfeet - feet) * 12.0; //get inches part
}
};
void main(void)
{
Distance distarr[100]; //array of 100 Distances
Distance total(0, 0.0), average; //other Distances
int count = 0; //counts Distances input
char ch; //user response character
do {
cout << "\nEnter a Distance"; //get Distances
distarr[count++].getdist(); //from user, put
cout << "\nDo another(y / n) ? "; //in array
cin >> ch;
} while (ch != 'n');
for (int j = 0; j<count; j++) //add all Distances
total.add_dist(total, distarr[j]); //to total
average.div_dist(total, count); //divide by number
cout << "\nThe average is : "; //display average
average.showdist();
cout << endl;
_getch();
}
Chapter 7,Problem 2:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Ahmad Furqan Attari
// P7.2
// employee object uses a string as data
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
////////////////////////////////////////////////////////////////
class employee
{
private:
string name;
long number;
public:
void getdata() //get data from user
{
cout << "\nEnter name : "; cin >> name;
cout << "Enter number : "; cin >> number;
}
void putdata() //display data
{
cout << "\n Name : " << name;
cout << "\n Number : " << number;
}
};
void main(void)
{
employee emparr[100]; //an array of employees
int n = 0; //how many employees
char ch; //user response
do { //get data from user
cout << "\nEnter data for employee number " << n + 1;
emparr[n++].getdata();
cout << "Enter another(y / n) ? "; cin >> ch;
} while (ch != 'n');
for (int j = 0; j<n; j++) //display data in array
{
cout << "\nEmployee number " << j + 1;
emparr[j].putdata();
}
cout << endl;
_getch();
}
Chapter 7,Problem 1:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Ahmad Furqan Attari
// P7.1
// reverses a C-string
#include <iostream>
#include <cstring> //for strlen()
#include <conio.h>
using namespace std;
void reversit(char s[])
{
int len = strlen(s); //find length of string
for (int j = 0; j < len / 2; j++) //swap each character
{ // in first half
char temp = s[j]; // with character
s[j] = s[len - j - 1]; // in second half
s[len - j - 1] = temp;
}
}
void main(void)
{
const int MAX = 80; //array size
char str[MAX]; //string
cout << "\nEnter a string : "; //get string from user
cin.get(str, MAX);
reversit(str); //reverse the string
cout << "Reversed string is : "; //display it
cout << str << endl;
_getch();
}
Friday, 28 November 2014
Chapter 6,Problem 12:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 11
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cmath>
using namespace std;
class fraction
{
private:
int num, den;
public:
fraction()
{}
fraction(int n, int d) :num(n), den(d)
{
lowterms();
}
void take_frac(void)
{
char ch;
cin >> num >> ch >> den;
}
void show_frac(void)
{
cout << setw(2)<<num << "/" <<setw(2)<< den;
}
void lowterms(void) // change ourself to lowest terms
{
long tnum, tden, temp, gcd;
tnum = labs(num); // use non-negative copies
tden = labs(den); // (needs cmath)
if (tden == 0) // check for n/0
{
cout << "Illegal fraction : division by 0"; exit(1);
}
else if (tnum == 0) // check for 0/n
{
num = 0; den = 1; return;
}
// this ‘while’ loop finds the gcd of tnum and tden
while (tnum != 0)
{
if (tnum < tden) // ensure numerator larger
{
temp = tnum; tnum = tden; tden = temp;
} // swap them
tnum = tnum - tden; // subtract them
}
gcd = tden; // this is greatest common divisor
num = num / gcd; // divide both num and den by gcd
den = den / gcd; // to reduce frac to lowest terms
}
void add_frac(fraction a, fraction b)
{
num = a.num*b.den + a.den*b.num;
den = a.den*b.den;
lowterms();
}
void sub_frac(fraction a, fraction b)
{
num = a.num*b.den - a.den*b.num;
den = a.den*b.den;
lowterms();
}
void mul_frac(fraction a, fraction b)
{
num = a.num*b.num;
den = a.den*b.den;
lowterms();
}
void div_frac(fraction a, fraction b)
{
num = a.num*b.den;
den = a.den*b.num;
lowterms();
}
};
void main(void)
{
int den;
fraction f[9],res;
do
{
cout << "Enter a denominator(<10): ";
cin >> den;
} while (den > 9);
cout << " ";
for (int i = 0; i < den-1; i++)
{
f[i] = { i + 1, den };
f[i].show_frac();
cout << " ";
}
for (int i = 0; i < den - 1; i++)
{
cout << endl;
f[i].show_frac();
cout << " ";
for (int j = 0; j < den - 1; j++)
{
res.mul_frac(f[i], f[j]);
res.show_frac();
cout << " ";
}
}
_getch();
}
Chapter 6,Problem 11:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 11
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
class fraction
{
private:
int num, den;
public:
fraction()
{}
fraction(int n, int d):num(n), den(d)
{
}
void take_frac(void)
{
char ch;
cin >> num >> ch >> den;
}
void show_frac(void)
{
cout << num << "/" << den;
}
void lowterms(void) // change ourself to lowest terms
{
long tnum, tden, temp, gcd;
tnum = labs(num); // use non-negative copies
tden = labs(den); // (needs cmath)
if (tden == 0) // check for n/0
{
cout << "Illegal fraction : division by 0"; exit(1);
}
else if (tnum == 0) // check for 0/n
{
num = 0; den = 1; return;
}
// this ‘while’ loop finds the gcd of tnum and tden
while (tnum != 0)
{
if (tnum < tden) // ensure numerator larger
{
temp = tnum; tnum = tden; tden = temp;
} // swap them
tnum = tnum - tden; // subtract them
}
gcd = tden; // this is greatest common divisor
num = num / gcd; // divide both num and den by gcd
den = den / gcd; // to reduce frac to lowest terms
}
void add_frac(fraction a,fraction b)
{
num = a.num*b.den + a.den*b.num;
den = a.den*b.den;
lowterms();
}
void sub_frac(fraction a,fraction b)
{
num = a.num*b.den - a.den*b.num;
den = a.den*b.den;
lowterms();
}
void mul_frac(fraction a,fraction b)
{
num = a.num*b.num;
den = a.den*b.den;
lowterms();
}
void div_frac(fraction a, fraction b)
{
num = a.num*b.den;
den = a.den*b.num;
lowterms();
}
};
void main(void)
{
fraction a, b, c,d,e,f;
char ch;
do
{
cout << "Enter fraction a: ";
a.take_frac();
cout << "Enter fraction b: ";
b.take_frac();
c.add_frac(a, b);
a.show_frac();
cout << " + ";
b.show_frac();
cout << " = ";
c.show_frac();
cout << endl;
d.sub_frac(a, b);
a.show_frac();
cout << " - ";
b.show_frac();
cout << " = ";
d.show_frac();
cout << endl;
e.mul_frac(a, b);
a.show_frac();
cout << " * ";
b.show_frac();
cout << " = ";
e.show_frac();
cout << endl;
f.div_frac(a, b);
a.show_frac();
cout << " % ";
b.show_frac();
cout << " = ";
f.show_frac();
cout << endl << "Enter again? (y/n): ";
ch = _getche();
cout << endl;
} while (ch != 'n');
}
Chapter 6,Problem 10:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//28-11-2014
//Engr. Ahmad Furqan Attari
//p6.10
#include <iostream>
#include <conio.h>
using namespace std;
class angle
{
private:
int deg;
float min;
char dir;
public:
angle()
{}
angle(int d, float m, char di) : deg(d), min(m), dir(di)
{}
void get(void)
{
cout << "Enter degree: ";
cin >> deg;
cout << "Enter minute: ";
cin >> min;
cout << "Enter direction(E,W,S,N): ";
cin >> dir;
}
void show(void)
{
cout << deg << "\xF8" << min << "' " << dir;
}
};
class ship
{
private:
angle lati, longi;
int serial;
static int count;
public:
ship()
{
count++;
serial = count;
}
void get(void)
{
cout << "Enter ship"<<serial<<"'s latitude:" << endl;
lati.get();
cout << "Enter ship"<<serial<<"'s longitude:" << endl;
longi.get();
}
void show(void)
{
cout << "Ship number: " << serial << endl;
cout << "Ship position: " << endl;
cout << "latitude: ";
lati.show();
cout << endl << "longitude: ";
longi.show();
cout << endl;
}
};
int ship::count = 0;
void main(void)
{
ship s1, s2, s3;
s1.get();
s2.get();
s3.get();
s1.show();
s2.show();
s3.show();
_getch();
}
Chapter 5,Problem 1:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//28-11-2014
//Ahmad Furqan Attari
//p5.1
#include <iostream>
#include <conio.h>
using namespace std;
#define PI 3.14159;
float circarea(float r)
{
return r*r*PI;
}
void main(void)
{
float r;
cout << "Enter Radius: ";
cin >> r;
cout << "Area of the circle is: " << circarea(r);
_getch();
}
Chapter 5,Problem 12:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//p5.12
#include <iostream>
#include <conio.h>
using namespace std;
struct fraction
{
int num;
int den;
};
fraction fadd(fraction a,fraction b)
{
fraction f;
f.num = a.num*b.den + a.den*b.num;
f.den = a.den*b.den;
return f;
}
fraction fsub(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.den - a.den*b.num;
f.den = a.den*b.den;
return f;
}
fraction fmul(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.num;
f.den = a.den*b.den;
return f;
}
fraction fdiv(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.den;
f.den = a.den*b.num;
return f;
}
fraction take(void)
{
fraction f;
char dummy;
cout << "Enter a fraction (num/den): ";
cin >> f.num >> dummy >> f.den;
return f;
}
void main(void)
{
fraction a, b,add,sub,mul,div;
a = take();
b = take();
add = fadd(a, b);
sub = fsub(a, b);
mul = fmul(a, b);
div = fdiv(a, b);
cout << "Addition: " << add.num << "/" << add.den << endl;
cout << "Subtraction: " << sub.num << "/" << sub.den << endl;
cout << "Multiplication: " << mul.num << "/" << mul.den << endl;
cout << "Division: " << div.num << "/" << div.den << endl;
_getch();
}
//Ahmad Furqan Attari
//p5.12
#include <iostream>
#include <conio.h>
using namespace std;
struct fraction
{
int num;
int den;
};
fraction fadd(fraction a,fraction b)
{
fraction f;
f.num = a.num*b.den + a.den*b.num;
f.den = a.den*b.den;
return f;
}
fraction fsub(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.den - a.den*b.num;
f.den = a.den*b.den;
return f;
}
fraction fmul(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.num;
f.den = a.den*b.den;
return f;
}
fraction fdiv(fraction a, fraction b)
{
fraction f;
f.num = a.num*b.den;
f.den = a.den*b.num;
return f;
}
fraction take(void)
{
fraction f;
char dummy;
cout << "Enter a fraction (num/den): ";
cin >> f.num >> dummy >> f.den;
return f;
}
void main(void)
{
fraction a, b,add,sub,mul,div;
a = take();
b = take();
add = fadd(a, b);
sub = fsub(a, b);
mul = fmul(a, b);
div = fdiv(a, b);
cout << "Addition: " << add.num << "/" << add.den << endl;
cout << "Subtraction: " << sub.num << "/" << sub.den << endl;
cout << "Multiplication: " << mul.num << "/" << mul.den << endl;
cout << "Division: " << div.num << "/" << div.den << endl;
_getch();
}
Chapter 5,Problem 11:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//p5.11
#include <iostream>
#include <conio.h>
using namespace std;
struct brit_money
{
int pound;
int shelling;
int pence;
};
brit_money take(void)
{
brit_money m;
char dummy;
cout << "Enter money in old Britin style (5.3.2): ";
cin >> m.pound >> dummy >> m.shelling >> dummy >> m.pence;
return m;
}
brit_money add(brit_money m1, brit_money m2)
{
brit_money m;
m.pence = m1.pence + m2.pence;
m.shelling = m1.shelling + m2.shelling+m.pence/12;
m.pence = m.pence % 12;
m.pound = m1.pound + m2.pound + m.shelling / 20;
m.shelling = m.shelling % 20;
return m;
}
void show(brit_money m3)
{
cout << "\x9c" << m3.pound << "." << m3.shelling << "." << m3.pence;
}
void main(void)
{
brit_money mon1, mon2, mon3;
mon1 = take();
mon2 = take();
mon3 = add(mon1, mon2);
cout << "Sum is ";
show(mon3);
_getch();
}
//Ahmad Furqan Attari
//p5.11
#include <iostream>
#include <conio.h>
using namespace std;
struct brit_money
{
int pound;
int shelling;
int pence;
};
brit_money take(void)
{
brit_money m;
char dummy;
cout << "Enter money in old Britin style (5.3.2): ";
cin >> m.pound >> dummy >> m.shelling >> dummy >> m.pence;
return m;
}
brit_money add(brit_money m1, brit_money m2)
{
brit_money m;
m.pence = m1.pence + m2.pence;
m.shelling = m1.shelling + m2.shelling+m.pence/12;
m.pence = m.pence % 12;
m.pound = m1.pound + m2.pound + m.shelling / 20;
m.shelling = m.shelling % 20;
return m;
}
void show(brit_money m3)
{
cout << "\x9c" << m3.pound << "." << m3.shelling << "." << m3.pence;
}
void main(void)
{
brit_money mon1, mon2, mon3;
mon1 = take();
mon2 = take();
mon3 = add(mon1, mon2);
cout << "Sum is ";
show(mon3);
_getch();
}
Chapter 5,Problem 10:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//p5.10
#include <iostream>
#include <conio.h>
using namespace std;
int times = 0;
void g_fun(void)
{
times++;
cout << "I have been called " << times << " times." << endl;
}
void s_fun(void)
{
static int stimes = 0;
stimes++;
cout << "I have been called " << stimes << " times." << endl;
}
void main(void)
{
cout << "Global variable version!" << endl;
for (int i = 0; i < 10; i++)
{
g_fun();
}
cout << "Static variable version!" << endl;
for (int j = 0; j < 10; j++)
{
s_fun();
}
_getch();
}
//Ahmad Furqan Attari
//p5.10
#include <iostream>
#include <conio.h>
using namespace std;
int times = 0;
void g_fun(void)
{
times++;
cout << "I have been called " << times << " times." << endl;
}
void s_fun(void)
{
static int stimes = 0;
stimes++;
cout << "I have been called " << stimes << " times." << endl;
}
void main(void)
{
cout << "Global variable version!" << endl;
for (int i = 0; i < 10; i++)
{
g_fun();
}
cout << "Static variable version!" << endl;
for (int j = 0; j < 10; j++)
{
s_fun();
}
_getch();
}
Chapter 5,Problem 9:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//p5.9
#include <iostream>
#include <conio.h>
using namespace std;
struct time
{
int hours;
int mins;
int secs;
};
void swap(time& a, time& b)
{
int temp;
temp = a.hours;
a.hours = b.hours;
b.hours = temp;
temp = a.mins;
a.mins = b.mins;
b.mins = temp;
temp = a.secs;
a.secs = b.secs;
b.secs = temp;
}
void main(void)
{
time t1 = { 5, 42, 34 }, t2 = {6,26,56};
cout << "Befor swap" << endl;
cout<<"t1 = " << t1.hours << " hours " << t1.mins << " minutes " << t1.secs << " sec " << endl;
cout << "t2 = " << t2.hours << " hours " << t2.mins << " minutes " << t2.secs << " sec " << endl;
swap(t1,t2);
cout << "After swap" << endl;
cout << "t1 = " << t1.hours << " hours " << t1.mins << " minutes " << t1.secs << " sec " << endl;
cout << "t2 = " << t2.hours << " hours " << t2.mins << " minutes " << t2.secs << " sec " << endl;
_getch();
}
//Ahmad Furqan Attari
//p5.9
#include <iostream>
#include <conio.h>
using namespace std;
struct time
{
int hours;
int mins;
int secs;
};
void swap(time& a, time& b)
{
int temp;
temp = a.hours;
a.hours = b.hours;
b.hours = temp;
temp = a.mins;
a.mins = b.mins;
b.mins = temp;
temp = a.secs;
a.secs = b.secs;
b.secs = temp;
}
void main(void)
{
time t1 = { 5, 42, 34 }, t2 = {6,26,56};
cout << "Befor swap" << endl;
cout<<"t1 = " << t1.hours << " hours " << t1.mins << " minutes " << t1.secs << " sec " << endl;
cout << "t2 = " << t2.hours << " hours " << t2.mins << " minutes " << t2.secs << " sec " << endl;
swap(t1,t2);
cout << "After swap" << endl;
cout << "t1 = " << t1.hours << " hours " << t1.mins << " minutes " << t1.secs << " sec " << endl;
cout << "t2 = " << t2.hours << " hours " << t2.mins << " minutes " << t2.secs << " sec " << endl;
_getch();
}
Chapter 5,Problem 8:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//p5.8
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void main(void)
{
int a=5, b=8;
cout << "Befor Swap a=" << a << " , b= " << b << endl;
swap(a, b);
cout << "After Swap a=" << a << " ,b= " << b << endl;
_getch();
}
//Ahmad Furqan Attari
//p5.8
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void main(void)
{
int a=5, b=8;
cout << "Befor Swap a=" << a << " , b= " << b << endl;
swap(a, b);
cout << "After Swap a=" << a << " ,b= " << b << endl;
_getch();
}
Chapter 5,Problem 7:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//27-11-2014
//Ahmad Furqan Attari
//P5.7
#include <iostream>
#include "conio.h"
using namespace std;
double power(double num, int p=2)
{
double ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
int power(char num, int p = 2)
{
int ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
int power(int num, int p = 2)
{
int ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
long power(long num, int p = 2)
{
long ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
float power(float num, int p = 2)
{
float ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
void main(void)
{
char var_char=6;
int var_int=5;
long var_long = 7;
float var_float = 3;
double var_double = 9;
cout << "1 " << power(var_char,5)<<endl;
cout << "2 " << power(var_int, 5) << endl;
cout << "3 " << power(var_long, 5) << endl;
cout << "4 " << power(var_float, 5) << endl;
cout << "5 " << power(var_double, 5) << endl;
_getch();
}
//Ahmad Furqan Attari
//P5.7
#include <iostream>
#include "conio.h"
using namespace std;
double power(double num, int p=2)
{
double ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
int power(char num, int p = 2)
{
int ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
int power(int num, int p = 2)
{
int ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
long power(long num, int p = 2)
{
long ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
float power(float num, int p = 2)
{
float ans = 1;
for (int i = 0; i<p; i++)
{
ans *= num;
}
return ans;
}
void main(void)
{
char var_char=6;
int var_int=5;
long var_long = 7;
float var_float = 3;
double var_double = 9;
cout << "1 " << power(var_char,5)<<endl;
cout << "2 " << power(var_int, 5) << endl;
cout << "3 " << power(var_long, 5) << endl;
cout << "4 " << power(var_float, 5) << endl;
cout << "5 " << power(var_double, 5) << endl;
_getch();
}
Wednesday, 26 November 2014
Chapter 5,Problem 6:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//P5.6
//Ahmad Furqan
//1-3-2013
#include <iostream>
#include <conio.h>
using namespace std;
struct time
{
int hours;
int mins;
int secs;
};
long time_to_secs(time& );
time secs_to_time(long&);
void main(void)
{
time t;
long secs;
cout<<"Enter time in format (hh:mm:ss)"<<endl;
char ch;
cin>>t.hours>>ch>>t.mins>>ch>>t.secs;
cout<<"Equivalent time in seconds is "<<time_to_secs(t)<<" Seconds"<<endl;
cout<<"Enter time in seconds"<<endl;
cin>>secs;
time t1=secs_to_time(secs);
cout<<"Equivalent time is "<<t1.hours<<" Hours "<<t1.mins<<" Minutes "<<t1.secs<<" Seconds"<<endl;
_getch();
}
//time_to_secs
//converts time to secs
long time_to_secs(time& t)
{
return t.secs+t.mins*60+t.hours*60*60;
}
//secs_to_time
//converts secs to time
time secs_to_time(long& secs)
{
time t;
t.hours=secs/3600;
secs-=t.hours*3600;
t.mins=secs/60;
secs-=t.mins*60;
t.secs=secs;
return t;
} Chapter 5,Problem 5:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//P5.5
//Ahmad Furqan
//1-3-2013
#include <iostream>
#include <conio.h>
using namespace std;
long hms_to_secs(int& hours,int& mins,int& secs);
void main(void)
{
int h,m,s;
cout<<"Enter time "<<endl
<<"(Format hh:mm:ss)"<<endl;
char dummychar;
cin>>h>>dummychar>>m>>dummychar>>s;
cout<<"The equivalent time in seconds is "<<hms_to_secs(h,m,s)<<" seconds"<<endl;
_getch();
}
//hms_to_secs
//returns the time in secs
long hms_to_secs(int& hours,int& mins,int& secs)
{
return secs+mins*60+hours*60*60;
}
//Ahmad Furqan
//1-3-2013
#include <iostream>
#include <conio.h>
using namespace std;
long hms_to_secs(int& hours,int& mins,int& secs);
void main(void)
{
int h,m,s;
cout<<"Enter time "<<endl
<<"(Format hh:mm:ss)"<<endl;
char dummychar;
cin>>h>>dummychar>>m>>dummychar>>s;
cout<<"The equivalent time in seconds is "<<hms_to_secs(h,m,s)<<" seconds"<<endl;
_getch();
}
//hms_to_secs
//returns the time in secs
long hms_to_secs(int& hours,int& mins,int& secs)
{
return secs+mins*60+hours*60*60;
}
Chapter 5,Problem 4:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//P5.4
//Ahmad Furqan
//1-3-2013
#include <iostream>
#include <conio.h>
using namespace std;
struct Distance
{
int feet;
int inches;
};
Distance larger(Distance&,Distance&);
void main(void)
{
Distance d1={11,11},d2={5,9};
Distance d3=larger(d1,d2);
cout<<"larger is "<<d3.feet<<" feet "<<d3.inches<<" Inches"<<endl;
_getch();
}
//Larger
//Returns the larger distance
Distance larger(Distance& d1,Distance& d2)
{
if(d1.feet==d2.feet)
if(d1.inches>d2.inches)
return d1;
else
return d2;
else
if(d1.feet>d2.feet)
return d1;
else
return d2;
}
//Ahmad Furqan
//1-3-2013
#include <iostream>
#include <conio.h>
using namespace std;
struct Distance
{
int feet;
int inches;
};
Distance larger(Distance&,Distance&);
void main(void)
{
Distance d1={11,11},d2={5,9};
Distance d3=larger(d1,d2);
cout<<"larger is "<<d3.feet<<" feet "<<d3.inches<<" Inches"<<endl;
_getch();
}
//Larger
//Returns the larger distance
Distance larger(Distance& d1,Distance& d2)
{
if(d1.feet==d2.feet)
if(d1.inches>d2.inches)
return d1;
else
return d2;
else
if(d1.feet>d2.feet)
return d1;
else
return d2;
}
Chapter 5,Problem 3:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//zerosmaller
//Ahmad Furqan
//28-2-2013
#include <iostream>
#include <conio.h>
using namespace std;
void zerosmaller(int&,int&);
void main(void)
{
int a,b;
a=25;
b=20;
zerosmaller(a,b);
cout<<"a="<<a<<endl
<<"b="<<b<<endl;
_getch();
}
void zerosmaller(int& a,int& b)
{
if(a<b)
a=0;
else
b=0;
}
//Ahmad Furqan
//28-2-2013
#include <iostream>
#include <conio.h>
using namespace std;
void zerosmaller(int&,int&);
void main(void)
{
int a,b;
a=25;
b=20;
zerosmaller(a,b);
cout<<"a="<<a<<endl
<<"b="<<b<<endl;
_getch();
}
void zerosmaller(int& a,int& b)
{
if(a<b)
a=0;
else
b=0;
}
Chapter 5,Problem 2:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//P5.2
//Ahmad Furqan
//28-2-2013
#include <iostream>
#include <conio.h>
using namespace std;
double power(double num,int p);
double power(double num);
void main(void)
{
double num=3.154;
int p=4;
cout<<"1 "<<power(num,p)<<endl;
cout<<"2 "<<power(num)<<endl;
_getch();
}
double power(double num,int p)
{
double ans=1;
for(int i=0;i<p;i++)
{
ans*=num;
}
return ans;
}
double power(double num)
{
return num*num;
}
//Ahmad Furqan
//28-2-2013
#include <iostream>
#include <conio.h>
using namespace std;
double power(double num,int p);
double power(double num);
void main(void)
{
double num=3.154;
int p=4;
cout<<"1 "<<power(num,p)<<endl;
cout<<"2 "<<power(num)<<endl;
_getch();
}
double power(double num,int p)
{
double ans=1;
for(int i=0;i<p;i++)
{
ans*=num;
}
return ans;
}
double power(double num)
{
return num*num;
}
Chapter 6,Problem 9:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 9
#include <iostream>
#include <conio.h>
using namespace std;
class fraction
{
private:
int numerator,denomenator;
public:
void take_frac(void)
{
char ch;
cin>>numerator>>ch>>denomenator;
}
void show_frac(void)
{
cout<<numerator<<"/"<<denomenator;
}
void add_frac(fraction a,fraction b)
{
numerator=a.numerator*b.denomenator+a.denomenator*b.numerator;
denomenator=a.denomenator*b.denomenator;
}
};
void main(void)
{
fraction a,b,c;
char ch;
do
{
cout<<"Enter fraction a: ";
a.take_frac();
cout<<"Enter fraction b: ";
b.take_frac();
c.add_frac(a,b);
a.show_frac();
cout<<" + ";
b.show_frac();
cout<<" = ";
c.show_frac();
cout<<endl<<"Enter again? (y/n): ";
ch=getche();
cout<<endl;
}while(ch!='n');
}
//Chapter 6, Problem 9
#include <iostream>
#include <conio.h>
using namespace std;
class fraction
{
private:
int numerator,denomenator;
public:
void take_frac(void)
{
char ch;
cin>>numerator>>ch>>denomenator;
}
void show_frac(void)
{
cout<<numerator<<"/"<<denomenator;
}
void add_frac(fraction a,fraction b)
{
numerator=a.numerator*b.denomenator+a.denomenator*b.numerator;
denomenator=a.denomenator*b.denomenator;
}
};
void main(void)
{
fraction a,b,c;
char ch;
do
{
cout<<"Enter fraction a: ";
a.take_frac();
cout<<"Enter fraction b: ";
b.take_frac();
c.add_frac(a,b);
a.show_frac();
cout<<" + ";
b.show_frac();
cout<<" = ";
c.show_frac();
cout<<endl<<"Enter again? (y/n): ";
ch=getche();
cout<<endl;
}while(ch!='n');
}
Chapter 6,Problem 8:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Engr. Ahamad Furqan Attari
//Chapter 6, Problem 8
#include <iostream>
#include <conio.h>
using namespace std;
class Furqan
{
private:
int serial;
static int count;
public:
Furqan()
{
count++;
serial=count;
}
void report_serial(void)
{
cout<<"I'm object number "<<serial<<endl;
}
};
int Furqan::count=1;
void main(void)
{
Furqan f1,f2,f3;
f1.report_serial();
f2.report_serial();
f3.report_serial();
getch();
}
Chapter 6,Problem 7:Object Oriented Programming by Robert Lafore in C++ Solution Manual
//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 7
#include <iostream>
#include <conio.h>
using namespace std;
class angle
{
private:
int deg;
float min;
char dir;
public:
angle()
{}
angle(int d , float m , char di): deg(d),min(m),dir(di)
{}
void get(void)
{
cout<<"Enter degree: ";
cin>>deg;
cout<<"Enter minute: ";
cin>>min;
cout<<"Enter direction: ";
cin>>dir;
}
void show(void)
{
cout<<deg<<"\xF8"<<min<<"' "<<dir;
}
};
void main(void)
{
angle a(23,57,'E'),b;
a.show();
char ch;
do
{
cout<<endl<<"Enter an angle "<<endl;
b.get();
cout<<"You entered: ";
b.show();
cout<<endl<<"Enter again? ";
ch=getche();
}while(ch!='n');
}
Subscribe to:
Comments (Atom)