Monday, July 17, 2017

Mozila Firefox free download

Mozila Firefox free download


Mozilla Firefox is a fast, light and tidy open source web browser. At its public launch in 2004 Mozilla Firefox was the first browser to challenge Microsoft Internet Explorer’s dominance. Since then, Mozilla Firefox has consistently featured in the top 3 most popular browsers globally. The key features that have made Mozilla Firefox so popular are the simple and effective UI, browser speed and strong security capabilities. The browser is particularly popular with developers thanks to its open source development and active community of advanced users.
Easier Browsing
Mozilla put of a lot of resources into creating a simple but effective UI aimed at making browsing quicker and easier. They created the tab structure that has been adopted by most other browsers. In recent years Mozilla has also focused on maximizing browsing area by simplifying toolbar controls to just a Firefox button (which contains settings and options) and back/forward buttons. The URL box features direct Google searching as well as an auto predict/history feature called Awesome Bar. On the right side of the URL box there are bookmarking, history and refresh buttons. To the right of the URL box is a search box which allows you to customize your search engine options. Outside of that a view button controls what you see below the URL. Next to that you have the download history and home buttons.
Speed
Mozilla Firefox boasts impressive page load speeds thanks to the excellent JagerMonkey JavaScript engine. Start up speed and graphics rendering are also among the quickest in the market. Firefox manages complex video and web content using layer-based Direct2D and Driect3D graphics systems. Crash protection ensures only the plugin causing the issue stops working, not the rest of the content being browsed. Reloading the page restarts any affected plugins. The tab system and Awesome Bar have been streamlined to launch/get results very quickly too.
Security
Firefox was the first browser to introduce a private browsing feature which allows you to use the internet more anonymously and securely. History, searches, passwords, downloads, cookies and cached content are all removed on shutdown. Minimizing the chances of another user stealing your identity or finding confidential information. Content security, anti-phishing technology and antivirus/antimalware integration ensures your browsing experience is as safe as possible.
Personalisation & Development
One of the best features of the Firefox UI is customization. Simply right click on the navigation toolbar to customize individual components or just drag and drop items you want to move around. The inbuilt Firefox Add-ons Manager allows you to discover and install add-ons within the browser as well as view ratings, recommendations and descriptions. Read more about the top recommended add-ons for Mozilla Firefox. Thousands of customizable themes allow you to customize the look and feel of your browser. Site authors and developers can create advanced content and applications using Mozilla’s open source platform and enhanced API.
Please note: from version 53.0 onward Windows XP and Vista are no longer supported.


 Click Here

Share:

Notepad++ free download


Notepad++ is a free source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL Licence.
Based on a powerful editing component Scintilla, Notepad++ is written in C++ and uses pure Win32 API and STL which ensures a higher execution speed and smaller program size. By optimizing as many routines as possible without losing user friendliness, Notepad++ is trying to reduce the world carbon dioxide emissions. When using less CPU power, the PC can throttle down and reduce power consumption, resulting in a greener environment.
  • Syntax Highlighting and Syntax Folding
  • WYSIWYG
  • User Defined Syntax Highlighting
  • Auto-completion
  • Multi-Document
  • Multi-View
  • Regular Expression Search/Replace supported
  • Full Drag 'N' Drop supported
  • Dynamic position of Views
  • File Status Auto-detection
  • Zoom in and zoom out
  • Multi-Language environment supported
  • Bookmark
  • Brace and Indent guideline Highlighting
  • Macro recording and playback
 Click Here

Share:

Saturday, July 15, 2017

Robert Lafore 4th Edition Solution Manual Chapter 5 - Functions

CODE:

1. Refer to the CIRCAREA program in Chapter 2, “C++ Programming Basics.” Write a function

called circarea() that finds the area of a circle in a similar way. It should take an argument of type float and return an argument of the same type. Write a main() function that gets a radius value from the user, calls circarea(), and displays the result. #include<iostream> #include<conio.h> using namespace std; float circarea(float radius); int main() { float rad; cout<<"Enter radius of circle: "; cin>> rad; cout <<"Area is "<<circarea(rad); } float circarea(float radius) { return 3.14159F*radius*radius;}

CODE:

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. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); int main(void) { double n; int p=2; cout<<"Enter n: "; cin>> n; cout<<"Enter p: "; cin>> p; cout <<"The power is "<<power(n, p); } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

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. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); int main(void) { double n; int p=2; cout<<"Enter n: "; cin>> n; cout<<"Enter p: "; cin>> p; cout <<"The power is "<<power(n, p); } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

CODE:

3. Write a function called zeroSmaller() that is passed two int arguments by reference and then sets the smaller of the two numbers to 0. Write a main() program to exercise this function. #include<iostream> #include<conio.h> using namespace std; int main() { int n1, n2; cout<<"Enter n1: "; cin>> n1; cout<<"Enter n2: "; cin>> n2; cout <<"The number assigned to zero is "; if(zeroSmaller(n1, n2)) cout<<"n1"; else cout<<"n2"; } bool zeroSmaller(int& n1, int& n2) { if(n1<n2) {n1=0; return true;} else {n2=0;return false;} }

the smaller of the two numbers to 0. Write a main() program to exercise this function. #include<iostream> #include<conio.h> using namespace std; int main() { int n1, n2; cout<<"Enter n1: "; cin>> n1; cout<<"Enter n2: "; cin>> n2; cout <<"The number assigned to zero is "; if(zeroSmaller(n1, n2)) cout<<"n1"; else cout<<"n2"; } bool zeroSmaller(int& n1, int& n2) { if(n1<n2) {n1=0; return true;} else {n2=0;return false;} }

CODE:

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. #include<iostream> #include<conio.h> using namespace std; int distWatch(int d1, int d2); int main() { int d1, d2; cout<<"Enter d1: "; cin>> d1; cout<<"Enter d2: "; cin>> d2; cout <<"The largest distance is "<<distWatch(d1, d2); } int distWatch(int d1, int d2) { if(d1<d2) return d2; else return d1;}

CODE:

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. #include<iostream> #include<conio.h> using namespace std; long hms_to_secs(int hours, int minutes, int seconds); int main() { int h, m, s; char sep; cout<<"Enter the time in format hh:mm:ss : "; cin>>h>>sep>>m>>sep>>s; cout <<"The equivalent time in seconds is : "<<hms_to_secs(h, m, s); } long hms_to_secs(int hours, int minutes, int seconds) { return seconds+minutes*60+hours*3600;}

CODE:

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. #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; long time_to_secs(time t); time secs_to_time(long s); int main() { time t1, t2, t3; char c; cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; t3=secs_to_time(time_to_secs(t1)+time_to_secs(t2)); cout<<"The result is: "<<t3.hours<<":"<<t3.minutes<<":"<<t3.seconds; } long time_to_secs(time t){ return t.hours*3600+t.minutes*60+t.seconds;} time secs_to_time(long s){ time t; t.seconds=s%60; t.minutes=((s-t.seconds)%3600)/60; t.hours=s/3600; if(t.seconds>59) {t.seconds-=59; t.minutes++;} //Check seconds over. if(t.minutes>59) {t.minutes-=59; t.hours++;} //Check minutes over. return t; }

CODE:

7.Start with the power () function of Exercise 2, which works only with type double. Create a series of overloaded functions with the same name that, in addition to double, also work with types char, int, long, and float. Write a main() program that exercises these overloaded functions with all argument types. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); char power(char n, int p=2); int power(int n, int p=2); long power(long n, int p=2); float power(float n, int p=2); int main() { char sep; int p=2; double d_n; char c_n; int i_n; long l_n; float f_n; cout<<"In [n^p] format;\n"; cout<<"Enter a double type n : "; cin >>d_n>>sep>>p; cout<<"The power is "<<power(d_n, p)<<endl; cout<<"Enter a char type n : "; cin >>c_n>>sep>>p; cout<<"The power is "<<power(c_n, p)<<endl; cout<<"Enter a int type n : "; cin >>i_n>>sep>>p; cout<<"The power is "<<power(i_n, p)<<endl; cout<<"Enter a long type n : "; cin >>l_n>>sep>>p; cout<<"The power is "<<power(l_n, p)<<endl; cout<<"Enter a float type n : "; cin >>f_n>>sep>>p; cout<<"The power is "<<power(f_n, p)<<endl; } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} char power(char n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} int power(int n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} long power(long n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} float power(float n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

of overloaded functions with the same name that, in addition to double, also work with types char, int, long, and float. Write a main() program that exercises these overloaded functions with all argument types. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); char power(char n, int p=2); int power(int n, int p=2); long power(long n, int p=2); float power(float n, int p=2); int main() { char sep; int p=2; double d_n; char c_n; int i_n; long l_n; float f_n; cout<<"In [n^p] format;\n"; cout<<"Enter a double type n : "; cin >>d_n>>sep>>p; cout<<"The power is "<<power(d_n, p)<<endl; cout<<"Enter a char type n : "; cin >>c_n>>sep>>p; cout<<"The power is "<<power(c_n, p)<<endl; cout<<"Enter a int type n : "; cin >>i_n>>sep>>p; cout<<"The power is "<<power(i_n, p)<<endl; cout<<"Enter a long type n : "; cin >>l_n>>sep>>p; cout<<"The power is "<<power(l_n, p)<<endl; cout<<"Enter a float type n : "; cin >>f_n>>sep>>p; cout<<"The power is "<<power(f_n, p)<<endl; } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} char power(char n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} int power(int n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} long power(long n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} float power(float n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

CODE:

8. Write a function called swap() that interchanges two int values passed to it by the calling program.

(Note that this function swaps the values of the variables in the calling program, not those in the function.) You’ll need to decide how to pass the arguments. Create a main() program to exercise the function. #include<iostream> #include<conio.h> using namespace std; void swap(int& a, int& b); int main() { int a, b; cout<<"Enter a : "; cin >>a; cout<<"Enter b : "; cin >>b; swap(a, b); cout<<"Now a value is : "<<a<<" and b value is : "<<b; } void swap(int& a, int& b){int c=a; a=b; b=c;}

CODE:

9. This exercise is similar to Exercise 8, except that instead of two int variables, have the swap()

function interchange two struct time values (see Exercise 6). #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; void swap(time& t1, time& t2); int main() { time t1, t2; char c; cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; swap(t1, t2); cout<<"Now first time is : " <<t1.hours<<c<<t1.minutes<<c<<t1.seconds <<" and second time is : "<<t2.hours<<c<<t2.minutes<<c<<t2.seconds; } void swap(time& a, time& b){time c=a; a=b; b=c;}

CODE:

10. Write a function that, when you call it, displays a message telling how many times it has been

called: “I have been called 3 times”, or whatever. Write a main() program that calls this function at least 10 times. Try implementing this function in two different ways. First, use an external variable to store the count. Second, use a local static variable. Which is more appropriate? Why can’t you use an automatic variable? #include<iostream> #include<conio.h> using namespace std; void caller_counter(void); int main() { int outer_counter=0; outer_counter++; caller_counter(); cout<<"\nThe main programme counter value is: "<<outer_counter; } void caller_counter(void) { static int inner_counter=0; inner_counter++; cout<<"I have been called "<<inner_counter<<" times"; }

CODE:

11. Write a program, based on the sterling structure of Exercise 10 in Chapter 4, “Structures,” that obtains from the user two money amounts in old-style British format (£9:19:11), adds them, and displays the result, again in old-style format. Use three functions. The first should obtain a pounds- shillings-pence value from the user and return the value as a structure of type sterling. The second should take two arguments of type sterling and return a value of the same type, which is the sum of the arguments. The third should take a sterling structure as its argument and display its value.*/ #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; sterling psp_to_sterling(int pounds, int shillings, int pence); sterling sterling_add(sterling s1, sterling s2); void sterling_disp(sterling s); char c; int main() { int x, y, z; sterling x1, x2; cout<<"In [9:19:11] format;\n"; cout<<"Enter first money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x1=psp_to_sterling(x, y, z); cout<<"Enter second money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x2=psp_to_sterling(x, y, z); sterling_disp(sterling_add(x1, x2)); } sterling psp_to_sterling(int pounds, int shillings, int pence) { sterling x; x.pounds=pounds; x.shillings=shillings; x.pence=pence; return x; } sterling sterling_add(sterling s1, sterling s2) { s1.pounds += s2.pounds; s1.shillings += s2.shillings; s1.pence += s2.pence; if(s1.pence>11){s1.shillings += static_cast<int>(s1.pence/12); s1.pence %= 12;} if(s1.shillings>19){s1.pounds += static_cast<int>(s1.shillings/20); s1.shillings %= 20;} return s1;} void sterling_disp(sterling s){ cout<<"Total is : \x9c" <<s.pounds<<c<<s.shillings<<c<<s.pence;}

obtains from the user two money amounts in old-style British format (£9:19:11), adds them, and displays the result, again in old-style format. Use three functions. The first should obtain a pounds- shillings-pence value from the user and return the value as a structure of type sterling. The second should take two arguments of type sterling and return a value of the same type, which is the sum of the arguments. The third should take a sterling structure as its argument and display its value.*/ #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; sterling psp_to_sterling(int pounds, int shillings, int pence); sterling sterling_add(sterling s1, sterling s2); void sterling_disp(sterling s); char c; int main() { int x, y, z; sterling x1, x2; cout<<"In [9:19:11] format;\n"; cout<<"Enter first money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x1=psp_to_sterling(x, y, z); cout<<"Enter second money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x2=psp_to_sterling(x, y, z); sterling_disp(sterling_add(x1, x2)); } sterling psp_to_sterling(int pounds, int shillings, int pence) { sterling x; x.pounds=pounds; x.shillings=shillings; x.pence=pence; return x; } sterling sterling_add(sterling s1, sterling s2) { s1.pounds += s2.pounds; s1.shillings += s2.shillings; s1.pence += s2.pence; if(s1.pence>11){s1.shillings += static_cast<int>(s1.pence/12); s1.pence %= 12;} if(s1.shillings>19){s1.pounds += static_cast<int>(s1.shillings/20); s1.shillings %= 20;} return s1;} void sterling_disp(sterling s){ cout<<"Total is : \x9c" <<s.pounds<<c<<s.shillings<<c<<s.pence;}

CODE:

12. Revise the four-function fraction calculator from Exercise 12, Chapter 4, so that it uses

functions for each of the four arithmetic operations. They can be called fadd(), fsub(), fmul(), and fdiv (). Each of these functions should take two arguments of type struct fraction, and return an argument of the same type. #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; fraction fadd(fraction a, fraction b); fraction fsub(fraction a, fraction b); fraction fmul(fraction a, fraction b); fraction fdiv(fraction a, fraction b); int main() { fraction f[3]; char c, op; cout<<"Enter your task : "; cin >>f[0].numerator>>c>>f[0].denominator>>op>>f[1].numerator>>c>>f[1].denominator; if(!f[0].denominator || !f[1].denominator) {cout<<"Illeagle fraction !"<<endl; op=false;} switch(op) { case '+': f[2]=fadd(f[0], f[1]); break; case '-': f[2]=fsub(f[0], f[1]); break; case '*': f[2]=fmul(f[0], f[1]); break; case '/': f[2]=fdiv(f[0], f[1]); break; default: cout<<"Unknow operator please try again !"<<endl;} cout<<"Answer = "<<f[2].numerator<<c<<f[2].denominator; } fraction fadd(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.denominator+a.denominator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fsub(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.denominator-a.denominator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fmul(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fdiv(fraction a, fraction b) { fraction f; if(b.numerator != 0) { f.numerator =a.numerator*b.denominator; f.denominator=b.numerator*a.denominator; } else cout<<"Math error !"<<endl; return f; }
Share:

Robert Lafore 4th Edition Solution Manual Chapter 4 Structures

CODE:

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 three 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 #include<iostream.h> #include<conio.h> using namespace std; struct phone{int area_code, exchange, number;}; int main() { phone input, mine={212, 767, 8900}; cout<<"Enter your area code, exchange, and number: "; cin >>input.area_code>>input.exchange>>input.number; cout<<"My number is ("<<mine.area_code<<") "<<mine.exchange<<"-"<<mine.number<<endl; cout<<"Your number is ("<<input.area_code<<") "<<input.exchange<<"-"<<input.number; }

code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three 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 #include<iostream.h> #include<conio.h> using namespace std; struct phone{int area_code, exchange, number;}; int main() { phone input, mine={212, 767, 8900}; cout<<"Enter your area code, exchange, and number: "; cin >>input.area_code>>input.exchange>>input.number; cout<<"My number is ("<<mine.area_code<<") "<<mine.exchange<<"-"<<mine.number<<endl; cout<<"Your number is ("<<input.area_code<<") "<<input.exchange<<"-"<<input.number; }

CODE:

2. A point on the two-dimensional plane can be represented by two numbers: an x coordinate and

a y coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this: Enter coordinates for p1: 3 4 Enter coordinates for p2: 5 7 Coordinates of p1+p2 are: 8, 11 #include<iostream.h> #include<conio.h> using namespace std; struct point{int x, y;}; int main() { point p1, p2, sum; cout<<"Enter coordinates for p1: "; cin >>p1.x>>p1.y; cout<<"Enter coordinates for p2: "; cin >>p2.x>>p2.y; sum.x=p1.x+p2.x; sum.y=p1.y+p2.y; cout<<"Coordinates of p1+p2 are: "<<sum.x<<", "<<sum.y; }

CODE:

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. #include<iostream.h> #include<conio.h> using namespace std; struct Distance{int feet; float inches;}; struct Volume{Distance x, y, z;}; int main() { Volume dimension; char c; cout<<"Enter x, y & z ...\n(EX: 3.6=3 feet and 6 inches, default: 'x.0 y.0 z.0' x, y & z are in feet) : \n"; cin >>dimension.x.feet>>c>>dimension.x.inches >>dimension.y.feet>>c>>dimension.y.inches >>dimension.z.feet>>c>>dimension.z.inches; /*float result=(dimension.x.feet+dimension.x.inches/12)* (dimension.y.feet+dimension.y.inches/12)* (dimension.z.feet+dimension.z.inches/12);*/ cout<<"the volume is : " <<(dimension.x.feet+dimension.x.inches/12)* (dimension.y.feet+dimension.y.inches/12)* (dimension.z.feet+dimension.z.inches/12); }

CODE:

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. #include<iostream.h> #include<conio.h> #include<iomanip.h> using namespace std; struct employee{int number; float compensation;}; int main() { employee e[3]; int i; for(i=0; i<3; i++) { cout<<"Enter the number of employee number "<<i+1<<" : "; cin>>e[i].number; cout<<"Enter the compensation of employee number "<<i+1<<" : "; cin>>e[i].compensation;} cout<<"Employee number"<<" Employee compensation\n"; for(i=0; i<3; i++) cout<<setw(15)<<e[i].number<<setw(24)<<e[i].compensation<<endl; }

CODE:

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. #include<iostream.h> #include<conio.h> #include<iomanip.h> using namespace std; struct date{int day; int month; int year;}; int main() { date x; char c; cout<<"Enter the date : "; cin >>x.day>>c>>x.month>>c>>x.year; cout<<"The date is : "; cout<<x.day<<c<<x.month<<c<<x.year; }

CODE:

6. We said earlier that C++ I/O statements don’t automatically understand the data types of

enumerations. Instead, the (>>) and (<<) operators think of such variables simply as integers. You can overcome this limitation by using switch statements to translate between the user’s way of expressing an enumerated variable and the actual values of the enumerated variable. For example, imagine an enumerated type with values that indicate an employee type within an organization: enum etype { laborer, secretary, manager, accountant, executive, researcher }; Write a program that first allows the user to specify a type by entering its first letter (‘l’, ‘s’, ‘m’, and so on), then stores the type chosen as a value of a variable of type enum etype, and finally displays the complete word for this type. Enter employee type (first letter only) laborer, secretary, manager, accountant, executive, researcher): a Employee type is accountant. You’ll probably need two switch statements: one for input and one for output.*/ #include<iostream> using namespace std; #include<conio.h> enum etype{laborer, secretary, manager, accountant, executive, researcher}; int main() { etype x; char *ret; cout<<"Enter employee type (first letter only)"<<endl <<"(laborer, secretary, manager, accountant, executive, researcher): "; switch(getche()) { case 'l': x=laborer ; break; case 's': x=secretary ; break; case 'm': x=manager ; break; case 'a': x=accountant; break; case 'e': x=executive ; break; case 'r': x=researcher; } switch(x) { case 0: ret = "laborer" ; break; case 1: ret = "secretary" ; break; case 2: ret = "manager" ; break; case 3: ret = "accountant"; break; case 4: ret = "executive" ; break; case 5: ret = "researcher"; } cout<<"\nEmployee type is "<<ret<<"."; }

CODE:

7. Add a variable of type enum etype (see Exercise 5), and another variable of type struct date (see

Exercise 3) to the employee class of Exercise 4. Organize the resulting program so that the user enters four items of information for each of three employees: an employee number, the employee’s compensation, the employee type, and the date of first employment. The program should store this information in three variables of type employee, and then display their contents.*/ #include<iostream> #include<conio.h> #include<iomanip> using namespace std; enum etype{laborer, secretary, manager, accountant, executive, researcher}; struct date{int day; int month; int year;}; struct employee{int number; float compensation; date d; char *ret;}; int main() { employee e[3]; int i; char c; etype x; for(i=0; i<3; i++) { cout<<"\nEnter the number of employee number "<<i+1<<" : "; cin>>e[i].number; cout<<"Enter the compensation of employee number "<<i+1<<" : "; cin>>e[i].compensation; cout<<"Enter employee type (first letter only) of employee number "<<i+1<<" : "<<endl <<"(laborer, secretary, manager, accountant, executive, researcher): "; switch(getche()) { case 'l': x=laborer ; break; case 's': x=secretary ; break; case 'm': x=manager ; break; case 'a': x=accountant; break; case 'e': x=executive ; break; case 'r': x=researcher; } switch(x) { case 0: e[i].ret = "laborer" ; break; case 1: e[i].ret = "secretary" ; break; case 2: e[i].ret = "manager" ; break; case 3: e[i].ret = "accountant"; break; case 4: e[i].ret = "executive" ; break; case 5: e[i].ret = "researcher"; break; default: e[i].ret = "Unknow";} cout<<"\nEnter the date of employee number "<<i+1<<" : "; cin >>e[i].d.day>>c>>e[i].d.month>>c>>e[i].d.year;} cout<<"\nEmployee number"<<" compensation"<<" type"<<" date of first employment"<<endl; for(i=0; i<3; i++) cout<<setw(15)<<e[i].number <<setw(15)<<e[i].compensation <<setw(15)<<e[i].ret <<setw(21)<<e[i].d.day<<c<<e[i].d.month<<c<<e[i].d.year<<endl; }

CODE:

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 store 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. #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; int main() { fraction equ[2]; char operation; cout<<"Enter first fraction: "; cin >>equ[0].numerator>>operation>>equ[0].denominator; //if (operation != '/') {raise error event} cout<<"Enter second fraction: "; cin >>equ[1].numerator>>operation>>equ[1].denominator; //if (operation != '/') {raise error event} cout<<"Sum = "<<(equ[0].numerator*equ[1].denominator+equ[0].denominator*equ[1].numerator) <<operation<<(equ[0].denominator*equ[1].denominator)<<endl; }

This program stores the numerator and denominator of two fractions before adding them, and may also store 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. #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; int main() { fraction equ[2]; char operation; cout<<"Enter first fraction: "; cin >>equ[0].numerator>>operation>>equ[0].denominator; //if (operation != '/') {raise error event} cout<<"Enter second fraction: "; cin >>equ[1].numerator>>operation>>equ[1].denominator; //if (operation != '/') {raise error event} cout<<"Sum = "<<(equ[0].numerator*equ[1].denominator+equ[0].denominator*equ[1].numerator) <<operation<<(equ[0].denominator*equ[1].denominator)<<endl; }

CODE:

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 a variable of type struct time, and finally print out the total number of seconds represented by this time value: long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; int main() { time t1; char c; cout<<"Enter a time value in hours, minutes, and seconds [hh:mm:ss] format: "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"The total number of seconds is: "<<t1.hours*3600 + t1.minutes*60 + t1.seconds; }

CODE:

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 request 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 variable of type struct sterling, and then display this amount in pounds-shillings-pence format. #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; int main() { float decpounds; float decfrac; //From old programme. sterling s1; cout<<"Enter a money amount in new-style decimal pounds: "; cin >>decpounds; s1.pounds = static_cast<int>(decpounds); decfrac = 240*(decpounds-s1.pounds); s1.shillings = (static_cast<int>(decfrac))%12; //Ignore fracions in pence. decfrac = static_cast<int>((decfrac-s1.shillings)/12); //Ignore fracions in pence. cout<<"Equivalent in old notation = \x9c"<<s1.pounds<<"."<<decfrac<<"."<<s1.shillings<<endl; }

CODE:

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 result in a time structure, and finally displays the result in 12:59:59 format. #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; int main() { time t1, t2, t3; char c; long tmp; //t3 coz he want to store the result in variable. cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; tmp=t1.hours*3600+t1.minutes*60+t1.seconds+t2.hours*3600+t2.minutes*60+t2.seconds; t3.seconds=tmp%60; t3.minutes=((tmp-t3.seconds)%3600)/60; t3.hours=tmp/3600; //those lines for true input at first then true output. if(t3.seconds>59) {t3.seconds-=59; t3.minutes++;} //Check seconds over. if(t3.minutes>59) {t3.minutes-=59; t3.hours++;} //Check minutes over. //hours check not needed .. at 25 hours I haven't a format for days. cout<<"The result is: "<<t3.hours<<":"<<t3.minutes<<":"<<t3.seconds; }

CODE:

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. #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; int main() { fraction f[2]; char c, op; cout<<"Enter your task : "; cin >>f[0].numerator>>c>>f[0].denominator>>op>>f[1].numerator>>c>>f[1].denominator; if(!f[0].denominator || !f[1].denominator) {cout<<"Illeagle fraction !"<<endl; op=false;} switch(op) { case '+': cout<<"Answer = "<<(f[0].numerator*f[1].denominator+f[0].denominator*f[1].numerator)<<c <<(f[0].denominator*f[1].denominator); break; case '-': cout<<"Answer = "<<(f[0].numerator*f[1].denominator-f[0].denominator*f[1].numerator)<<c <<(f[0].denominator*f[1].denominator); break; case '*': cout<<"Answer = "<<f[0].numerator*f[1].numerator<<c<<f[0].denominator*f[1].denominator; break; case '/': if(f[0].numerator != 0) cout<<"Answer = "<<f[0].numerator*f[1].denominator<<c <<f[1].numerator*f[0].denominator; else cout<<"Math error !"<<endl; break; default: cout<<"Unknow operator please try again !"<<endl;} }
Share:

Robert Lafore 4th Edition Solution Manual Chapter 3 Loops and Decisions

Code:

1.  Assume you want to generate a table of multiples of any given number. Write a program that
  allows the user to enter the number, and then generates the table, formatting it into 10 columns and
  20 lines. Interaction with the program should look like this (only the first three lines are shown): 
  Enter a number: 7

     7   14   21   28   35   42   49   56   63   70
    77   84   91   98  105  112  119  126  133  140
   147  154  161  168  175  182  189  196  203  210


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 
 int i, j, entred_int;

 
 cout<<"Enter a number: "; cin >>entred_int;
 for(i=0; i<20; i++)
        {for(j=0; j<10; j++) 
           cout<<setw(7)<<(entred_int*(10*i+j+1)); 
            cout<<endl;}
 
}
 Code:


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,
       2 to convert Celsius to Fahrenheit: 1
  Enter temperature in Fahrenheit: 70
  In Celsius that’s 21.111111


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 int choise; float temp;

 
 cout<<"Type 1 to convert Fahrenheit to Celsius,\n     2 to convert Celsius to Fahrenheit: ";
 cin >>choise;
 switch(choise){ //replacable by if ... else .
  case   2:
   cout<<"Enter temperature in Celsius: ";    cin >>temp;
   cout<<"In Fahrenheit that's "<<(9*temp/5)+32;
   break;
  case   1:
   cout<<"Enter temperature in Fahrenheit: "; cin >>temp;
   cout<<"In Celsius that's "   <<((temp-32)*5)/9;
   break;
  default :
   cout<<"Invalid choise try again !";}
 
}
  Code:

3.  Operators such as >>, which read input from the keyboard, must be able to convert a series of
  digits into a number. Write a program that does the same thing. It should allow the user to type up
  to six digits, and then display the resulting number as a type long integer. The digits should be read
  individually, as characters, using getche(). Constructing the number involves multiplying the existing
  value by 10 and then adding the new digit. (Hint: Subtract 48 or ‘0’ to go from ASCII to a
  numerical digit.) 
  Here’s some sample interaction: 
  Enter a number: 123456
  Number is: 123456


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
 
 int i; long _output; char char_input;
 cout<<"Enter a number (Maximum six characters): ";
 _output = 0; i=0;
 //ASCII('\r') == 13
 while((char_input=getche()) != 13 && i<6){
  _output = 10*_output + (char_input - 48);
  i++;}
 if(i == 6) cout<<"\b ";
 cout<<"\nNumber is: "<<_output;
 
}
 Code:

4.  Create the equivalent of a four-function calculator. The program should request the user to
  enter a number, an operator, and another number. (Use floating point.) It should then carry out the
  specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a
  switch statement to select the operation. Finally, display the result. 
  When it finishes the calculation, the program should ask if the user wants to do another calculation.
  The response can be ‘y’ or ‘n’. Some sample interaction with the program might look like this:
  Enter first number, operator, second number: 10 / 3
  Answer = 3.333333
  Do another (y/n)? y
  Enter first number, operator, second number: 12 + 100
  Answer = 112
  Do another (y/n)? n


#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>

void calcul(void); //calculations function.
void asking(void); //asking to continue function.
intt main()
{
 
 do{
 calcul();
 asking();  
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}

void calcul(void)
{
 float num[2]; char operation;

 cout<<"\nEnter first number, operator, second number: ";
 cin >>num[0]>>operation>>num[1];
 switch(operation){
 case '+':
  cout<<"Answer = "<<num[0] + num[1]<<endl;
  break;
 case '-':
  cout<<"Answer = "<<num[0] - num[1]<<endl;
  break;
 case '*':
  cout<<"Answer = "<<num[0] * num[1]<<endl;
  break;
 case '/':
  if(num[1] != 0) cout<<"Answer = "<<num[0] / num[1]<<endl;
  else           {cout<<"Math error !"<<endl; calcul();}
  break;
 default:
  cout<<"Unknow operator please try again !"<<endl;
  calcul();}
}

void asking()
{
 cout<<"\nDo another (y/n)? ";
 switch(toupper(getche())){
 case 'Y':
  calcul();
  asking();
  break;
 case 'N':
  exit(1);
  break;
 default:
  asking();}
}
  Code:

5.  Use for loops to construct a program that displays a pyramid of Xs on the screen. The pyramid
  should look like this 

      X
     XXX
    XXXXX
   XXXXXXX
  XXXXXXXXX



  except that it should be 20 lines high, instead of the 5 lines shown here. One way to do this is to nest
  two inner loops, one to print spaces and one to print Xs, inside an outer loop that steps down the
  screen from line to line.


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 
 
 
 for(int i=0; i<20; i++)
               {
  for(int X=0   ; X<20   ; X++) cout<<" "; //This line optional to make the pyramid on the midlle.
  for(int j=20-i; j>0    ; j--) cout<<" ";
  for(int k=0   ; k<2*i+1; k++) cout<<"X";
  cout<<endl;}
 
}
  6.  Modify the FACTOR program in this chapter so that it repeatedly asks for a number and
  calculates its factorial, until the user enters 0, at which point it terminates. You can enclose the
  relevant statements in FACTOR in a while loop or a do loop to achieve this effect.*/
#include<iostream>
using namespace std;
#include<conio.h>

// The FACTOR program:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!#include <iostream>
!using namespace std;
!
!int main()
!   {
!   unsigned int numb;
!   unsigned long fact=1;             //long for larger numbers
!
!   cout << “Enter a number: ”;
!   cin >> numb;                      //get number
!
!   for(int j=numb; j>0; j--)         //multiply 1 by
!   fact *= j;                     //numb, numb-1, ..., 2, 1
!   cout << “Factorial is ” << fact << endl;
!   return 0;
!   }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void main(void)
{
 cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 cout<<"-------------------------------------------------------------------------------\n"<<endl;

 unsigned int numb, i;
 unsigned long fact;

 cout<<"Enter a number: ";
 cin >>numb;
 do{
 fact=1; i=0;
 for(i=numb; i>0; i--) fact *= i;
 cout<<"\n\nFactorial is "<<fact<<endl;
 cout<<" !Enter 0 to exit or any other number to calculate it's factorial: ";
 cin >>numb;
 }while(numb!=0);
}
  Code:

7.  Write a program that calculates how much money you’ll end up with if you invest an amount of
  money at a fixed interest rate, compounded yearly. Have the user furnish the initial amount, the
  number of years, and the yearly interest rate in percent. Some interaction with the program might
  look like this: 
  Enter initial amount: 3000
  Enter number of years: 10
  Enter interest rate (percent per year): 5.5
  At the end of 10 years, you will have 5124.43 dollars.
  At the end of the first year you have 3000 + (3000 * 0.055), which is 3165. At the end of the
  second year you have 3165 + (3165 * 0.055), which is 3339.08. Do this as many times as there
  are years. A for loop makes the calculation easy.


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{

 int num_year; float init_amount, intrst_rate;

 do{
 cout<<"Enter initial amount                  : "; 
 cin >>init_amount;
 cout<<"Enter number of years                 : "; 
 cin >>num_year   ;
 cout<<"Enter interest rate (percent per year): "; 
 cin >>intrst_rate;
 for(int i=0; i<num_year; i++) init_amount += init_amount*intrst_rate/100;
 cout<<"At the end of "<<num_year
  <<" years, you will have "<<init_amount
  <<" dollars.";
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:


8.  Write a program that repeatedly asks the user to enter two money amounts expressed in old-
  style British currency: pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2, “++
  Programming Basics.”) The program should then add the two amounts and display the answer,
  again in pounds, shillings, and pence. Use a do loop that asks the user if the program should be
  terminated. Typical interaction might be 
  Enter first amount: £5.10.6
  Enter second amount: £3.2.6
  Total is £8.13.0
  Do you wish to continue (y/n)?
  To add the two amounts, you’ll need to carry 1 shilling when the pence value is greater than 11, and
  carry 1 pound when there are more than 19 shillings.*/



#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 
 int m[2][3]; char sep; //m (money), sep (char_separator).

 do{
 cout<<"Enter first amount : \x9c";
 cin >>m[0][0]>>sep>>m[0][1]>>sep>>m[0][2];
 cout<<"Enter second amount: \x9c";
 cin >>m[1][0]>>sep>>m[1][1]>>sep>>m[1][2];
 m[0][0] += m[1][0]; m[0][1] += m[1][1]; m[0][2] += m[1][2];
 if(m[0][2]>11){m[0][1] += static_cast<int>(m[0][2]/12); m[0][2] %= 12;}
 if(m[0][1]>19){m[0][0] += static_cast<int>(m[0][1]/20); m[0][1] %= 20;}
 cout<<"Total is           : \x9c"<<m[0][0]<<sep<<m[0][1]<<sep<<m[0][2];
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
 Code:

9.  Suppose you give a dinner party for six guests, but your table seats only four. In how many ways
  can four of the six guests arrange themselves at the table? Any of the six guests can sit in the first
  chair. Any of the remaining five can sit in the second chair. Any of the remaining four can sit in the
  third chair, and any of the remaining three can sit in the fourth chair. (The last two will have to
  stand.) So the number of possible arrangements of six guests in four chairs is 6*5*4*3, which is
  360. Write a program that calculates the number of possible arrangements for any number of guests
  and any number of chairs. (Assume there will never be fewer guests than chairs.) Don’t let this get
  too complicated. A simple for loop should do it.



#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 
 int ch_num, gs_num, result;
        do{
 cout<<"Enter number of guests                 : \xdb "; cin >>gs_num;
 cout<<"Enter number of chairs                 : \xdb "; cin >>ch_num;
 result=1; for(int i=0; i<ch_num; i++) result*=(gs_num-i);
 cout<<"The number of possible arrangements is : \xdb "<<result;
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:

10.  Write another version of the program from Exercise 7 so that, instead of finding the final
  amount of your investment, you tell the program the final amount and it figures out how many years it
  will take, at a fixed rate of interest compounded yearly, to reach this amount. What sort of loop is
  appropriate for this problem? (Don’t worry about fractional years; use an integer value for the year.)



#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{

 int i; float init_amount, intrst_rate, finl_amount;

 do{
 cout<<"Enter initial amount                  : "; cin >>init_amount;
 cout<<"Enter interest rate (percent per year): "; cin >>intrst_rate;
 cout<<"Enter final amount                    : "; cin >>finl_amount;
 i=0;
 while(finl_amount>=init_amount) {finl_amount -= finl_amount*intrst_rate/100; i++;}
 cout<<"Number of years is                    : "<<i;
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:

11.  Create a three-function calculator for old-style English currency, where money amounts are
  specified in pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2.) The calculator
  should allow the user to add or subtract two money amounts, or to multiply a money amount by a
  floating-point number. (It doesn’t make sense to multiply two money amounts; there is no such thing
  as square money. We’ll ignore division. Use the general style of the ordinary four-function
  calculator in Exercise 4 in this chapter.)


#include<iostream.h>
#include<conio.h>
using namespace std;
void chk_overs(void);
int m[3][3]; char c[3];

int main()
{
 
 do{
 cout<<"Enter the currency opperation: ";
 cin >>m[0][0]>>c[0]>>m[0][1]>>c[1]>>m[0][2]>>c[2];
 switch(c[2]){
 case '+':
  cin >>m[1][0]>>c[0]>>m[1][1]>>c[1]>>m[1][2];
  m[0][0] += m[1][0]; m[0][1] += m[1][1]; m[0][2] += m[1][2];
  chk_overs();
  cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
  break;
 case '-':
  cin >>m[1][0]>>c[0]>>m[1][1]>>c[1]>>m[1][2];
  m[0][0] -= m[1][0]; m[0][1] -= m[1][1]; m[0][2] -= m[1][2];
  chk_overs();
  cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
  break;
 case '*':
  cin >>m[1][0];
  m[0][0] *= m[1][0]; m[0][1] *= m[1][0]; m[0][2] *= m[1][0];
  chk_overs();
  cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
  break;
 default :
  cout<<"Syntex or operation error, check your inputs again.";}
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}

void chk_overs(void)
{
 if(m[0][2]>11){m[0][1] += static_cast<int>(m[0][2]/12); m[0][2] %= 12;}
 if(m[0][1]>19){m[0][0] += static_cast<int>(m[0][1]/20); m[0][1] %= 20;}
}
 Code:

12.  Create a four-function calculator for fractions. (See Exercise 9 in Chapter 2, and Exercise 4 in
  this chapter.) Here are the formulas for the four arithmetic operations applied to fractions:
  Addition:         a/b + c/d = (a*d + b*c) / (b*d)  
  Subtraction:      a/b - c/d = (a*d - b*c) / (b*d)  
  Multiplication:   a/b * c/d = (a*c) / (b*d)  
  Division:         a/b / c/d = (a*d) / (b*c)  

  The user should type the first fraction, an operator, and a second fraction. The program should then
  display the result and ask if the user wants to continue.


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()


{
 
 int first[2], last[2];
 char op[2];

 do{
 cout<<"Enter your task : ";
 cin >>first[0]>>op[0]>>last[0]>>op[1]
  >>first[1]>>op[0]>>last[1];
 if(!last[0] || !last[1]) {cout<<"Illeagle fraction !"<<endl; op[1] = false;}
 switch(op[1]) {
 case '+':
  cout<<"Answer = "<<(first[0]*last[1] + last[0]*first[1])<<op[0]<<(last[0]*last[1]);
  break;
 case '-':
  cout<<"Answer = "<<(first[0]*last[1] - last[0]*first[1])<<op[0]<<(last[0]*last[1]);
  break;
 case '*':
  cout<<"Answer = "<<first[0]*first[1]<<op[0]<<last[0]*last[1];
  break;
 case '/':
  if(first[1] != 0) cout<<"Answer = "<<first[0]*last[1]<<op[0]<<first[1]*last[0];
  else              cout<<"Math error !"<<endl;
  break;
 default:
  cout<<"Unknow operator please try again !"<<endl;}
 cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Share:

Friday, July 14, 2017

Robert Lafore 4th edition Solution Manual Chapter 2 Programming Basics

Code:

1.  Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a
  number of gallons, and then displays the equivalent in cubic feet.


#include<iostream.h>
using namespace std;
#define g_per_f 7.481

int main()
{
 

 float n_gallons;
 cout<<"Enter the number of gallons  : \xdb\t";
 cin >>n_gallons;
 cout<<"The equivalent in cubic feet : \xdb\t"<<n_gallons / g_per_f<<endl;
}
 Code:

2.  Write a program that generates the following table: 
    1990      135
    1991     7290
    1992    11300
    1993    16200


Use a single cout statement for all output.


#include<iostream.h>
using namespace std;

int main()
{

 int i, i_arr[4]={135,7290,11300,16200};

 for(i=1990;i<1994;i++) 
        {
         cout<<i<<setw(7)<<i_arr[i-1990]<<endl;
        }
 
 
}
  Code:

3.  Write a program that generates the following output: 
    10
    20
    19


  Use an integer constant for the 10, an arithmetic assignment operator to generate the 20, and a
  decrement operator to generate the 19. 
#include<iostream.h>
using namespace std;
#define ten 10

int main()
{

 //int ten = 10; //### use this line in the place of "define"
 //int second, third;
 //second = 2*ten; third = second-1;
 //cout<<ten<<endl<<second<<endl<<third<<endl;
 do{
 cout<<ten<<endl<<2*ten<<endl<<2*ten-1<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
  Code:

4.  Write a program that displays your favorite poem. Use an appropriate escape sequence for the
  line breaks. If you don’t have a favorite poem, you can borrow this one by Ogden Nash: 

    Candy is dandy,
    But liquor is quicker.

#include<iostream.h>
#include<conio.h>
using namespce std;
int main()
{
 
 do{
 cout<<"\tCandy is dandy\n\tBut liquor is quicker"<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
   Code:

5.  A library function, islower(), takes a single character (a letter) as an argument and returns a
  nonzero integer if the letter is lowercase, or zero if it is uppercase. This function requires the header
  file CTYPE.H. Write a program that allows the user to enter a letter, and then displays either zero or
  nonzero, depending on whether a lowercase or uppercase letter was entered.


#include<iostream.h>
#include<conio.h>
#include<ctype.h>
using namespace std;
int main()
{
 
 do{
 cout<<"Enter a letter : \xdb\t"<<endl;
 cout<<islower(getch())<<"\nthis value must be zero if you entred an uppercase letter and nonzero case else."<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
   Code:

6.  On a certain day the British pound was equivalent to $1.487 U.S., the French franc was $0.172,
  the German deutschemark was $0.584, and the Japanese yen was $0.00955. Write a program that 
  allows the user to enter an amount in dollars, and then displays this value converted to these four
  other monetary units.
#include<iostream.h>
#include<conio.h>
using namespace std;
#define pound         1.487
#define franc         0.172
#define deutschemark  0.584
#define yen           0.00955

int main()
{ 
 
 int dollars;

 do{
 cout<<"Enter the U.S. amout : \xdb\t";
 cin >>dollars;
 cout<<endl;
 cout<<"British  pound         =\t" << dollars / pound        <<endl;
 cout<<"French   franc         =\t" << dollars / franc        <<endl;
 cout<<"German   deutschemark  =\t" << dollars / deutschemark <<endl;
 cout<<"Japanese yen           =\t" << dollars / yen          <<endl;
 //cout<<"\nAnother operation ? (y/n) : ";  //use this line to continue using the programme.
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
  Code:

7.  You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5
  and adding 32. Write a program that allows the user to enter a floating-point number representing
  degrees Celsius, and then displays the corresponding degrees Fahrenheit.



#include<iostream.h>
#include<conio.h>
using namespace std:;
int main()
{
 
 float c_temp;

 do{
 cout<<"Enter the degrees Celsius\t\t\t\xdb ";
 cin >>c_temp;
 cout<<"The corresponding degrees Fahrenheit is :\t\xdb "<<((9/5)*c_temp)+32<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:

8.  When a value is smaller than a field specified with setw(), the unused locations are, by default,
  filled in with spaces. The manipulator setfill() takes a single character as an argument and causes this
  character to be substituted for spaces in the empty parts of a field. Rewrite the WIDTH program so
  that the characters on each line between the location name and the population number are filled in
  with periods instead of spaces, as in Portcity.....2425785


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
using namespace std;
int main()
{
 long pop1=2425785, pop2=47, pop3=9761;
 
 do{
 cout << setw(8) << "LOCATION" << setw(12)
      << "POPULATION" << endl
         << setw(8) << "Portcity" << setw(12) << setfill('.') << pop1 << endl
         << setw(8) << "Hightown" << setw(12) << setfill('.') << pop2 << endl
         << setw(8) << "Lowville" << setw(12) << setfill('.') << pop3 << endl;
    cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
  Code:

9.  If you have two fractions, a/b and c/d, their sum can be obtained from the formula 

  a      c      a*d + b*c
 --- + ---  =  -----------
  b      d         b*d



 For example, 1/4 plus 2/3 is 
  1     2       1*3 + 4*2       3 + 8       11
 --- + ---  =  -----------  =  -------  =  ----
  4     3          4*3            12        12



 Write a program that encourages the user to enter two fractions, and then displays their sum in
 fractional form. (You don’t need to reduce it to lowest terms.) The interaction with the user might
 look like this: 

 Enter first fraction: 1/2
     Enter second fraction: 2/5
     Sum = 9/10


 You can take advantage of the fact that the extraction operator (>>) can be chained to read in more
 than one quantity at once: 
 cin >> a >> dummychar >> b;


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 
 int first[2], last[2];
 char operation; //In this sample programme not needed but generally for error detection only!
 
 do{
 cout<<"Enter first  fraction: ";
 cin >>first[0]>>operation>>last[0]; //if (operation != '/') {raise error event}
 cout<<"Enter second fraction: ";
 cin >>first[1]>>operation>>last[1]; //if (operation != '/') {raise error event}
 cout<<"Sum = "<<(first[0]*last[1] + last[0]*first[1])<<operation<<(last[0]*last[1])<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:

10.  In the heyday of the British empire, Great Britain used a monetary system based on pounds,
  shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for
  this old system used the pound sign, £, and two decimal points, so that, for example, £5.2.8 meant
  5 pounds, 2 shillings, and 8 pence. (Pence is the plural of penny.) The new monetary system,
  introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S.
  dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is
  £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-
  shillings-pence format to decimal pounds. An example of the user’s interaction with the program
  would be 

    Enter pounds: 7
    Enter shillings: 17
    Enter pence: 9
    Decimal pounds = £7.89
  In both Borland C++ and Turbo C++, you can use the hex character constant ‘\x9c’ to represent the
  pound sign (£). In Borland C++, you can put the pound sign into your program directly by pasting it
  from the Windows Character Map accessory.



#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
 int pounds, shillings, pence;
 do{
 cout<<"Enter pounds     : ";
 cin >>pounds;
 cout<<"Enter shillings  : ";
 cin >>shillings;
 cout<<"Enter pence      : ";
 cin >>pence;
 pence = ((shillings*12)+pence)*100/240;
 //To make the programme more really (pence must not pass value 100).
 if (pence >= 100){
  //shillings here is only a gate, not by it's mean at all.
  shillings = pence%100;
  pounds += (pence-shillings)/100;
  pence = shillings;}
 cout<<"Decimal pounds   = \x9c"<<pounds<<"."<<pence<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:

11.  By default, output is right-justified in its field. You can left-justify text output using the
  manipulator setiosflags(ios::left). (For now, don’t worry about what this new notation means.) Use
  this manipulator, along with setw(), to help generate the following output:
  
  Last name    First name    Street address    Town    State
      -----------------------------------------------------------
      Jones    Bernard    109 Pine Lane    Littletown    MI
      O’Brian    Coleen    42 E. 99th Ave.    Bigcity    NY
      Wong    Harry    121-A Alabama St.    Lakeville    IL


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
using namespace std;
int main()
{
 
 char *l_name[3] = {"Jones", "O'Brian", "Wong"},
   *f_name[3] = {"Bernard", "Coleen", "Harry"},
   *adress[3] = {"109 Pine Lane", "42 E. 99th Ave.", "121-A Alabama St."},
   *town[3]   = {"Littletown", "Bigcity", "Lakeville"},
   *state[3]  = {"MI", "NY", "IL"};
 
 do{
 cout<<setiosflags(ios::left)<<setw(11)<<"Last name"
                          <<setw(12)<<"First name"
        <<setw(20)<<"Street adress"
        <<setw(12) <<"Town"
        <<setw(7) <<"State"
        <<endl;
 for(int i=0;i<60;i++) cout<<"-";
 for(int j=0;j<3;j++){
  cout<<endl<<setiosflags(ios::left)<<setw(11)<<l_name[j]
            <<setw(12)<<f_name[j]
            <<setw(20)<<adress[j]
            <<setw(12)<<town[j]
            <<setw(7) <<state[j]
            <<endl;}
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
  12.  Write the inverse of Exercise 10, so that the user enters an amount in Great Britain’s new
  decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-
  shillings-pence notation. An example of interaction with the program might be
  
    Enter decimal pounds: 3.51
    Equivalent in old notation = £3.10.2.



  Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the
  decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler
  warning. You can use statements like 

    float decpounds;    // input from user (new-style pounds)
    int pounds;         // old-style (integer) pounds
    float decfrac;      // decimal fraction (smaller than 1.0)

  pounds = static_cast<int>(decpounds); // remove decimal fraction
    decfrac = decpounds - pounds;  // regain decimal fraction
You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence.


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
  float decpounds;    // input from user (new-style pounds)
    int pounds, shillings;          // old-style (integer) pounds & shillings
    float decfrac;     // decimal fraction (smaller than 1.0)
 
 
  cout<<"Enter decimal pounds: ";
 //cin >>pounds>>var_char>>decfrac; //I think this way is better!
 cin >>decpounds;
 pounds = static_cast<int>(decpounds); // remove decimal fraction
 //user should enter valid data , pence entred smaller than 100.
    decfrac = 240*(decpounds - pounds);  // regain decimal fraction
 shillings = (static_cast<int>(decfrac))%12;                  //Ignore fracions in pence.
 decfrac = static_cast<int>((decfrac-shillings)/12);          //Ignore fracions in pence.
 cout<<"Equivalent in old notation = \x9c"<<pounds<<"."<<decfrac<<"."<<shillings<<endl;
 
Share: