Exercise(3)

Chapter- 4

no.1

A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212

Answer

#include<iostream.h>
#include<conio.h>
struct phone
{
int areacode;
int exchange;
int number;
};
int main()
{
clrscr();
phone p1 = {212,767, 8900};
phone p2;
cout<<"\n Enter your area code, exchange and number: ";
cin>>p2.areacode>>p2.exchange>>p2.number;
cout<<"\n My number is ( "<<p1.areacode<<")"<<p1.exchange<<" - "<<p1.number;
cout<<"\n Your number is ( "<<p2.areacode<<")"<<p2.exchange<<" - "<<p2.number;
getch();
return 0;
}

no.2

Write a temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion. Use floating-point numbers. Interaction with the program might look like this:
Type 1 to convert Fahrenheit to Celsius
Type 2 to convert Celsius to Fahrenheit : 1
Enter temperature in Fahrenheit : 70
In Celsius that’s 21.111111

Answer

#include<iostream.h>
#include<conio.h>
struct point
{
int x;
int y;
};
int main()
{
clrscr();
point p1,p2,p3;
cout<<"\nEnter coordinates for p1: ";
cin>>p1.x>>p1.y;
cout<<"\nEnter coordinates for p2: ";
cin>>p2.x>>p2.y;
p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;
cout<<"\nCoordinates of p1+ p2 are: "<<p3.x<<" , "<<p3.y;
getch();
return 0;
}

no.3

Create a structure called Volume that uses three variables of type Distance ( from the ENGLSTRC example) to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents and print out the result. To calculate the volume, convert each dimension from a Distance variable to a variable of type float representing feet and fractions of a foot and then multiply the resulting three numbers

Answer

#include<iostream.h>
#include<conio.h>
struct distance
{ int feet;
float inches;
};
struct volume
{
distance length;
distance height;
distance width;
};
int main()
{
clrscr();
volume v1;
float total_length, total_height, total_width;
v1.length.feet=30; //initializing value
v1.length.inches=8; //initializing value
v1.height.feet=7; //initializing value
v1.height.inches=5; //initializing value
v1.width.feet=15; //initializing value
v1.width.inches=11; //initializing value
total_length=v1.length.feet+(v1.length.inches/12);
total_height=v1.height.feet+(v1.height.inches/12);
total_width=v1.width.feet+(v1.width.inches/12);
cout<<"\nRoom's length is 30' 8\""<<" = "<<total_length<<" feet.";
cout<<"\nRoom's length is 7' 5\""<<" = "<<total_height<<" feet.";
cout<<"\nRoom's length is 15'11\""<<" = "<<total_width<<" feet.";
cout<<"\n\n The Volume of Room is "<<total_length*total_height*total_width;
getch();
return 0;
}

Output is

no.4

Create a structure called employee that contains two members: an employee number (type int) and the employee’s compensation ( in dollars : type float ). Ask the user to fill in this data for three employees, store it in three variables of type struct employee and then display the information for each employee.

Answer

#include<iostream.h>
#include<conio.h>
struct employee{
int number;
float compensation;
};
int main()
{
clrscr();
employee e1,e2,e3;
cout<<"Enter employee number: ";
cin>>e1.number;
cout<<"Enter employee compensation: $";
cin>>e1.compensation;
cout<<"Enter employee number: ";
cin>>e2.number;
cout<<"Enter employee compensation: $";
cin>>e2.compensation;
cout<<"Enter employee number: ";
cin>>e3.number;
cout<<"Enter employee compensation: $";
cin>>e3.compensation;
cout<<"Employee number\t\tEmployee Compensation"<<endl;
cout<<e1.number<<"\t\t\t"<<e1.compensation<<endl;
cout<<e2.number<<"\t\t\t"<<e2.compensation<<endl;
cout<<e3.number<<"\t\t\t"<<e3.compensation<<endl;
getch();
return 0;
}

Output is

no.5

Create a structure of type date that contains three members: the month, the day of the month and the year, all of type int. (Or use day-month-year order if you prefer). Have the user enter a date in the format 12/31/2001, store it in a variable of type struct date, then retrieve the values from the variable and print them out in the same format.

Answer

#include<iostream.h>
#include<conio.h>
struct date{
int month;
int day;
int year;
};
int main()
{
clrscr();
char ch='/';
date d;
cout<<"Enter date in the format 12/31/2001: ";
cin>>d.month>>ch>>d.day>>ch<<d.year;
cout<<"Your entered date is : "<<d.month<<ch<<d.day<<ch<<d.year;
getch();
return 0;
}

Output is

no.8

Start with the fraction-adding program of Exercise 9 in Chapter-2, “C++ Programming Basics.” This program stores the numerator and denominator of two fractions before adding them, and may also the answer, which is also a fraction. Modify the program so that all fractions are stored in variables of type struct fraction, whose two members are the fraction’s numerator and denominator (both type int). All fraction-related data should be stored in structures of this type.

Answer

#include<iostream.h>
#include<conio.h>
#include<math.h>
struct fraction{
int numerator;
int denominator;
};
int main()
{
clrscr();
fraction f, s, ans;
char ch='/';
cout<<"Enter first fraction: ";
cin>>f.numerator>>ch>>f.denominator;
cout<<"\nEnter second fraction: ";
cin>>s.numerator>>ch>>s.denominator;
ans.denominator=f.denominator * s.denominator;
ans.numerator= (f.numerator * s.denominator) + (s.numerator * f.denominator);
cout<<"\nSum = "<<ans.numerator<<ch<<ans.denominator;
getch();
return 0;
}

Output is

no.9

Create a structure called time. Its three members, all type int, should be called hours, minutes and seconds . Write a program that prompts the user to enter a time value in hours, minutes and seconds. This can be in 12: 59: 59 format, or each number can be entered at a separate prompt (” Enter hours: ” and so forth). The program should then store the time in the variabe type struct time, and finally print out the total number of seconds represent by this time value:
long totalsec = t1.hours * 3600 + t1. seconds

Answer

#include<iostream.h>
#include<conio.h>
#include<math.h>
struct time
{
int hours;
int minutes;
int seconds;
};
int main()
{
clrscr();
char ch;
time t1;
cout<<"Enter time in format 12:59:59";
cin>>t1.hours>>ch>>t1.minutes>>ch>>t1.seconds;
long totalsecs= t1.hours*3600 + t1.minutes*60 + t1.seconds;
cout<<"\n Total number of second = "<<totalsecs;
getch();
return 0;
}

Output is

no.10

Create a structure called sterling that stores money amounts in the old-style British system discussed in Exercises 8 and 11 in Chapter 3, “Loops and Decisions.” The members could be called pounds, shillings, and pence, all of type int. The program should ask the user to enter a money amount in new-style decimal pounds ( type double ), convert it to the old-style system, store it in a variableof type struct sterling , and then display this amount in pounds-shilling-pence format.

no.11

Use the time structure from Exercise-9 and write a program that obtains two time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds ( type int) , adds these quantities , converts the result back to hours minutes-seconds, stores the results in a time structure and finally displays the results in 12:59:59 format.

no.12

Revise the four-function fraction calculator program of Exercise 12 in Chapter 3 so that each fraction is stored internally as a variable of type struct fraction, as discussed in Exercise 8 in this chapter.

Design a site like this with WordPress.com
Get started