A commentary on my technical exploits

Monthly Archives: October 2012

Exercise 9 is a remake of exercise 6, a common theme in this chapter. Only a few things need to be swapped in order to meet the requirements. We initially declare a single structure candyBar, then we create a dynamic array candyBar[3]. *bar points to the first element of candyBar[3], and we can access candyBar by calling bar. Here is my solution:

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

Exercise 8 took a little finagling to make it ask for diameter first while outputting it second in order as the last program, without skipping over the name input. If you use some other methods for pointing to structs at the diameter input, you will see what I mean. I provided a few different methods of pointing to structs in this exercise. The “new” keyword was used as per directions to allocate memory for our structure. Alas, here is my solution:

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

Exercise 7 is fairly straightforward. We are constructing another data structure modeled after a pizza. The only difference here is that we are making the structs member variables instead of hard coding them. Here is my solution:

William Wingate runs a pizza-analysis service. For each pizza, he needs to record the followinginformation:• The name of the pizza company, which can consist of more than one word• The diameter of the pizza• The weight of the pizzaDevise a structure that can hold this information and write a program that uses a structurevariable of that type. The program should ask the user to enter each of the precedingitems of information, and then the program should display that information. Use cin(or its methods) and cout.

For exercise six we can copy and paste our previous code and simply make some minor adjustments to the source to complete exercise six. This time we declare our snack variable as an array of three, and initialize the member within braces. I went with my three favorite candy bars fir this one 🙂 I think the goal of this exercise was to show that we can have either one thing with relevant variables or we can create structs with many relevant member search their own variables. Here is my solution:

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

Exercise five requires us to make a basic struct. Structs are useful for when you want to hold relevant information about an object. With structs we are getting closer to OOP programming. Here is my solution to the problem:

5. The CandyBar structure contains three members. The first member holds the brandname of a candy bar. The second member holds the weight (which may have a fractionalpart) of the candy bar, and the third member holds the number of calories (an integervalue) in the candy bar. Write a program that declares such a structure and creates aCandyBar variable called snack, initializing its members to “Mocha Munch”, 2.3, and350, respectively. The initialization should be part of the declaration for snack. Finally,the program should display the contents of the snack variable.

With some minor adjustments, we can use the C++ string class to accomplish our goal. We will use the C++ member function ‘append’ from the string class. My solution if fairly clean and simple. Here is the source:

4. Write a program that asks the user to enter his or her first name and then last name, andthat then constructs, stores, and displays a third string consisting of the user’s last name174 C++ PRIMER PLUS, FIFTH EDITIONfollowed by a comma, a space, and first name. Use string objects and methods from thestring header file. A sample run could look like this:Enter your first name: FlipEnter your last name: FlemingHere’s the information in a single string: Fleming, Flip

Exercise 3 of chapter 4 wants us to do some specific things with cstrings. We can use strcpy and strcat member functions from the cstring class to make our third string adhere to the requirements. Here is my solution :

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