View Single Post
  #2  
Old 11-09-2008, 09:29 PM
BSIT07-01's Avatar
BSIT07-01 BSIT07-01 is offline
Addicted to Computer


 
Join Date: Sep 2007
Location: ------------
Age: 34
Posts: 1,309
Contact Number: ---------------
Program / Discipline: BSIT
Class Roll Number: 07-01
BSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant futureBSIT07-01 has a brilliant future
Default Re: Solution of Ex 5 & 6 Ch# 5 Robert Lafore OOP c++

Exercise 2

Code:
#include <iostream>
   using namespace std;
   double power( double n, int p=2);   
   
   int main()
      {
      double number, answer;
      int pow;
      char yeserno;
   
      cout << "\nEnter number: ";     
      cin >> number;
      cout << "Want to enter a power (y/n)? ";
      cin >> yeserno;
      if( yeserno == ‘y’ )           
         {
         cout << "Enter power: ";
         cin >> pow;
         answer = power(number, pow);  
         }
      else
         answer = power(number);      
      cout << "Answer is " << answer << endl;
      return 0;
      }


   double power( double n, int p )
      {
      double result = 1.0;          
      for(int j=0; j<p; j++)          
         result *= n;                 
      return result;
      }


Exercise 4

Code:
#include <iostream>
using namespace std;
 
   struct Distance               
      {
      int feet;
      float inches;
      };
  
   Distance bigengl(Distance, Distance);  
   void engldisp(Distance);
   
   int main()
      {
      Distance d1, d2, d3;         
                                 
      cout << "\nEnter feet: ";  cin >> d1.feet;
      cout << "Enter inches: ";  cin >> d1.inches;
                                
      cout << "\nEnter feet: ";  cin >> d2.feet;
      cout << "Enter inches: ";  cin >> d2.inches;
   
      d3 = bigengl(d1, d2);      
                                  
      cout << "\nd1=”; engldisp(d1);
      cout << "\nd2=”; engldisp(d2);
      cout << "\nlargest is "; engldisp(d3); cout << endl;
      return 0;
      }

   Distance bigengl( Distance dd1, Distance dd2 )
      {
      if(dd1.feet > dd2.feet)      
         return dd1;               
      if(dd1.feet < dd2.feet)
         return dd2;
      if(dd1.inches > dd2.inches)           return dd1;               
      else                         
         return dd2;
      }

   void engldisp( Distance dd )
      {
      cout << dd.feet << "\’-” << dd.inches << "\””;
      }

Reply With Quote