Menu

Sunday, 30 November 2014

Chapter 7,Problem 4:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//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();
}

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();
}