BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/35-object-oriented-programming/)
-   -   Solution of Ex 5,6,2 & 4 Ch# 5 Robert Lafore OOP c++ (http://bzupages.com/f35/solution-ex-5-6-2-4-ch-5-robert-lafore-oop-c-721/)

BSIT07-01 11-09-2008 09:06 PM

Solution of Ex 5,6,2 & 4 Ch# 5 Robert Lafore OOP c++
 

Solution of Exercise 5


Code:

#include
using namespace std;


long hms_to_secs(int h,int m,int s)

{
    long seconds;

    seconds=h*3600+m*60+s;
    return seconds;

}

void main()


{

char c;
int h,m,s;
for(int n=1;n<10;n++)
{
cout<<"enter time in this formate 12:12:49  :  ";

cin>>h>>c>>m>>c>>s;

cout< }

}




Solution of Exercise 6


Code:

#include
using namespace std;

struct time
{
    int hours,minutes,seconds;

};

long time_to_secs(time);

time secs_to_time(long);

int main()


{
    time time1,time2;
   
    char colon;

    cout<<"enter First time in this format (12:30:30):";
    cin>>time1.hours>>colon>>time1.minutes>>colon>>time1.seconds;

    cout<<"enter Second time in this format (12:30:30):";
    cin>>time2.hours>>colon>>time2.minutes>>colon>>time2.seconds;

    long totalseconds1=time_to_secs(time1);

    long totalseconds2=time_to_secs(time2);

    long totalseconds=totalseconds1+totalseconds2;

    time totaltime=secs_to_time(totalseconds);


    cout<<"\nTotal Time: "<
    return 0;

}

long time_to_secs(time time1)

{


long totalseconds=time1.hours*3600 + time1.minutes*60 + time1.seconds;

return totalseconds;
}


time secs_to_time(long totalseconds)

{
    int temp;
    time time3;

    time3.hours=totalseconds/3600;

    temp=totalseconds%3600;

    time3.minutes=temp/60;

    time3.seconds=temp%60;

    return time3;

}



Ahmad ne kaha tha mein kar doon ga post...
Ahmad ne tu post karnay hi nahin hain, Main he kar daita hoon :(

BSIT07-01 11-09-2008 09:29 PM

Re: Solution of Ex 5 & 6 Ch# 5 Robert Lafore OOP c++
 
Exercise 2

Code:


#include
  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         result *= n;               
      return result;
      }



Exercise 4

Code:


#include
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 << "\””;
      }


.BZU. 11-09-2008 10:44 PM

Re: Solution of Ex 5 & 6 Ch# 5 Robert Lafore OOP c++
 
http://cs.bzupages.com/../../images/...ost_thanks.gifThanks for sharing the solution..
Soooooooper.....:smitten:
Here are the Questions Assigned By Madam : :book1:e:

Question 2. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power() that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets values from the user to test this function.

Question
4. Write a function that takes two Distance values as arguments and returns the larger one. Include a main() program that accepts two Distance values from the user, compares them, and displays the larger. (See the retstrc program for hints.)

Question 5. Write a function called hms_to_secs() that takes three int values—for hours, minutes, and seconds—as arguments, and returns the equivalent time in seconds (type long). Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.

Question
6. Start with the program from Exercise 11, Chapter 4, “Structures,” which adds two struct time values. Keep the same functionality, but modify the program so that it uses two functions. The first, time_to_secs(), takes as its only argument a structure of type time, and returns the equivalent in seconds (type long). The second function, secs_to_time(), takes as its only argument a time in seconds (type long), and returns a structure of type time.

BSIT07-01 17-09-2008 07:32 PM

Re: Solution of Ex 5,6,2 & 4 Ch# 5 Robert Lafore OOP c++
 
Exercise # 4
(: A different Approach :)


Code:

#include
using namespace std;

int count=1;
struct dist
    {
    int feet;
    int inches;
    };

dist largedist(dist, dist);
dist correctdist(dist);
void display(dist);
   
void main()
{
    dist a,b,larger;
    cout<<"\nEnter Feet of First Distace: ";
    cin>>a.feet;
    cout<<"\nEnter Inches of First Distace: ";
    cin>>a.inches;
    cout<<"\nEnter Feet of 2nd Distace: ";
    cin>>b.feet;
    cout<<"\nEnter Inches of 2nd Distace: ";
    cin>>b.inches;
    if (a.inches>=12)
        a=correctdist(a);
    if(b.inches>=12)
        b=correctdist(b);


    larger=largedist(a,b);

    cout<<"\nLarger distace = ";
    display(larger);

   
   
}



dist largedist(dist a, dist b)
{
    dist large={12,3};
    if(a.feet>b.feet)
        large=a;
    else if (a.feet         large=b;
    else if(a.feet==b.feet)
    {
        if(a.inches>b.inches)
            large= a;
        else if(a.inches             large = b;
       
    }

return large;
}

dist correctdist(dist a)
{
    dist correct;
    correct.feet=a.feet+a.inches/12;
    correct.inches=a.inches%12;
   
   
    cout<<"\nNote: The Distance Value "<


return correct;
}


void display(dist a)

{
cout<

}



All times are GMT +5. The time now is 03:47 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.