Showing posts with label C Primer Plus. Show all posts
Showing posts with label C Primer Plus. Show all posts

Saturday, April 2, 2016

Compound Types (C++ Primer Plus Sixth Edition Solution Manual)

Leave a Comment

Chapter Review

1. How would you declare each of the following?

             a. actors is an array of 30 char.
             b. betsie is an array of 100 short.
             c. chuck is an array of 13 float.
             d. dipsea is an array of 64 long double.

2. Does Chapter Review Question 1 use the array template class instead of built-in arrays.

3. Declare an array of five ints and initialize it to the first five odd positive integers.

4. Write a statement that assigns the sum of the first and last elements of the array in Question 3 to the variable even.

5. Write a statement that displays the value of the second element in the float array ideas.

6. Declare an array of char and initialize it to the string "cheeseburger".

7. Declare a string object and initialize it to the string "Waldorf Salad".

8. Devise a structure declaration that describes a fish.The structure should include the kind, the weight in whole ounces, and the length in fractional inches.

9. Declare a variable of the type defined in Question 8 and initialize it.

10. Use enum to define a type called Response with the possible values Yes, No, and Maybe. Yes should be 1, No should be 0, and Maybe should be 2.

11. Suppose ted is a double variable. Declare a pointer that points to ted and use the pointer to display ted’s  value.

12. Suppose treacle is an array of 10 floats. Declare a pointer that points to the first element of treacle and use the pointer to display the first and last elements of the array.

13. Write a code fragment that asks the user to enter a positive integer and then creates a dynamic array of that many ints. Do this by using new, then again using a vector object.

14. Is the following valid code? If so, what does it print?

             cout << (int *) “Home of the jolly bytes”;

15. Write a code fragment that dynamically allocates a structure of the type described in Question 8 and then reads a value for the kind member of the structure.

16. Listing 4.6 illustrates a problem created by following numeric input with line-oriented string input.How would replacing this:

             cin.getline(address,80);
with this:
             cin >> address;
affect the working of this program?

17. Declare a vector object of 10 string objects and an array object of 10 string objects. Show the necessary header files and don’t use using. Do use a const for the number of strings.

Programming Exercises

1. Write a C++ program that requests and displays information as shown in the following example of output:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
Note that the program should be able to accept first names that comprise more than one word. Also note that the program adjusts the grade downward—that is, up one letter. Assume that the user requests an A, a B, or a C so that you don’t have to worry about the gap between a D and an F.

2. Rewrite Listing 4.4, using the C++ string class instead of char arrays.

3. Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file. A sample run could look like this:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

4. Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string consisting of the user’s last name followed by a comma, a space, and first name. Use string objects and methods from the string header file. A sample run could look like this:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

5. The CandyBar structure contains three members.The first member holds the brand name of a candy bar.The second member holds the weight (which may have a fractional part) of the candy bar,
and the third member holds the number of calories (an integer value) in the candy bar.Write a program that declares such a structure and creates a CandyBar variable called snack, initializing its members to "Mocha Munch", 2.3, and 350, respectively. The initialization should be part of the declaration for snack. Finally, the program should display the contents of the snack variable.

6. The Candy Bar structure contains three members, as described in Programming Exercise 5.Write a program that creates an array of three CandyBar structures, initializes them to values
of your choice, and then displays the contents of each structure.

7. William Wingate runs a pizza-analysis service. For each pizza, he needs to record the following information:

The name of the pizza company, which can consist of more than one word
The diameter of the pizza
The weight of the pizza

Devise a structure that can hold this information and write a program that uses a structure variable of that type.The program should ask the user to enter each of the preceding items of information, and then the program should display that information. Use cin (or its methods) and cout.

8. Do Programming Exercise 7 but use new to allocate a structure instead of declaring a structure variable. Also have the program request the pizza diameter before it requests the pizza company name.

9. Do Programming Exercise 6, but instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically.

10. Write a program that requests the user to enter three times for the 40-yd dash (or 40-meter, if you prefer) and then displays the times and the average. Use an array object to hold the data. (Use a built-in array if array is not available.)

Read More

Monday, February 29, 2016

Dealing with Data (C++ Primer Plus Sixth Edition Solution Manual)

Leave a Comment

Chapter Review

1. Why does C++ have more than one integer type?

2. Declare variables matching the following descriptions:
          a. A short integer with the value 80
          b. An unsigned int integer with the value 42,110
          c. An integer with the value 3,000,000,000

3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?

4. What is the distinction between 33L and 33?

5. Consider the two C++ statements that follow:
          char grade = 65;
          char grade = 'A';
    Are they equivalent?

6. How could you use C++ to find out which character the code 88 represents?Come up with at least       two ways.

7. Assigning a long value to a float can result in a rounding error.What about assigning long to double? long long to double?

8. Evaluate the following expressions as C++ would:
          a. 8 * 9 + 2
          b. 6 * 3 / 4
          c. 3 / 4 * 6
          d. 6.0 * 3 / 4
          e. 15 % 4

9. Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so.What if you want to add them as type double and then convert to int?

10. What is the variable type for each of the following declarations?
          a. auto cars = 15;
          b. auto iou = 150.37f;
          c. auto level = 'B';
          d. auto crat = U'/U00002155';
          e. auto fract = 8.25f/2.5;

Programming Exercises

1. Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response. Also use a const symbolic constant to represent the conversion factor.

2. Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables to store the information.) Have the program report your body mass index (BMI). To calculate the BMI, first convert your height in feet and inches to your height in inches (1 foot = 12 inches).Then convert your height in inches to your height in meters by multiplying by 0.0254.Then convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute
your BMI by dividing your mass in kilograms by the square of your height in meters. Use symbolic constants to represent the various conversion factors.

3. Write a program that asks the user to enter a latitude in degrees, minutes, and seconds
and that then displays the latitude in decimal format.There are 60 seconds of arc to a minute and 60 minutes of arc to a degree; represent these values with symbolic constants.You should use a separate variable for each input value.

A sample
run should look like this:
Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

4. Write a program that asks the user to enter the number of seconds as an integer value (use type long, or, if available, long long) and that then displays the equivalent time in days, hours, minutes, and seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute.The output should look like this:

Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

5. Write a program that requests the user to enter the current world population and the current population of the U.S. (or of some other nation of your choice). Store the information in variables of type long long. Have the program display the percent that the U.S. (or other nation’s) population is of the world’s population.The output should look something like this:

Enter the world's population: 6898758899
Enter the population of the US: 310783781
The population of the US is 4.50492% of the world population.
Yo u  c a n  u s e  t h e  I n t e r n e t  t o  g e t  mo r e  r e c e n t  f i g u r e s .



Read More

Dealing with Data (C++ Primer Plus Sixth Edition Solution Manual) Question-5

Leave a Comment
Questions:

Dealing with Data (C++ Primer Plus Sixth Edition Solution Manual) Question-5

Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;
void main() {

       long float worldPopulation;
       long float usPopulation;
       long float result;
       cout << "Enter the world's population:";
       cin >> worldPopulation;
       cout << "Enter the population of the US:";
       cin >> usPopulation;
       result = (usPopulation / worldPopulation);
       result = result * 100;
       cout << "The population of the US is " << result<< "% of the world population." << endl;

}

Output:
Dealing with Data (C++ Primer Plus Sixth Edition Solution Manual) Question-5


Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution


Reference:

Answers to Chapter Reviews C++ Primer Plus

Read More