C++ Primer Plus Chapter 6 Exercise 6

c plus plusExercise 6 has us working with more structs. This time we create a dynamically allocated array of structures. This sounds interesting but is not too difficult after you start putting code to compiler. Essentially, you create your array, declare variables that were mentioned int the text, use the “new” keyword to allow numDonors and our struct, then loop through patrons and their amounts. See my source below for a more clear explanation:

6. Put together a program that keeps track of monetary contributions to the Society for the
Preservation of Rightful Influence. It should ask the user to enter the number of contributors
and then solicit the user to enter the name and contribution of each contributor.
The information should be stored in a dynamically allocated array of structures. Each
structure should have two members: a character array (or else a string object) to store
the name and a double member to hold the amount of the contribution. After reading
all the data, the program should display the names and amounts donated for all donors
who contributed $10,000 or more. This list should be headed by the label Grand
Patrons. After that, the program should list the remaining donors. That list should be
headed Patrons. If there are no donors in one of the categories, the program should print
the word “none.” Aside from displaying two categories, the program need do no sorting.

#include <iostream>
#include <string>

using namespace std;

struct contrib{
string name;
double amount;
};

int main()
{
int numDonors = 0;
int patrons = 0;
int grandPatrons = 0;

cout << "Society for the Preservation of Rightful Influence" << "\n\n";

cout << "Enter number of contributors: ";
cin >> numDonors;
cout << "\n";

contrib *society = new contrib[numDonors];

// Gather names and amounts
for(int i = 0; i < numDonors; i++)
{
cout << "Enter the name of the contributor: ";
cin >> society[i].name;
cout << "Enter the contribution anount: ";
cin >> society[i].amount;
}

cout << "\n";
// Display donors over 10000
cout << "Grand Patrons: \n";
for(int x = 0; x < numDonors; x++)
{
if(society[x].amount >= 10000)
{
cout << society[x].name << " Donated: "
<< "$ " << society[x].amount << "\n";
grandPatrons = 1;
}
}
if(grandPatrons == 0)
cout << "none\n";

cout << "\n";

// Display all other patrons
cout << "Patrons list: \n";
for(int y = 0; y < numDonors; y++)
{
if(society[y].amount < 10000)
{
cout << society[y].name << " Donated: "
<< "$ " << society[y].amount << "\n";
patrons = 1;
}
}
if(patrons == 0)
cout << "none\n";

// free memory
delete [] society;

return 0;
}
Advertisements

C++ Primer Plus Chapter 4 Exercise 9

c plus plusExercise 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.

#include <iostream>
#include <string>

using namespace std;

// Candy bar structure
struct candyBar
{
string brand;
double weight;
int calories;
};

int main()
{
// create three members, but use new to allocate
candyBar * bar = new candyBar[3];

bar[0].brand = "Crunch";
bar[0].weight = 1.7;
bar[0].calories = 275;

bar[1].brand = "Heath";
bar[1].weight = 2.3;
bar[1].calories = 400;

bar[2].brand = "Rolo";
bar[2].weight = 2.5;
bar[2].calories = 350;

// Ouput bars
cout << "The first bar variable holds: \n";
cout << bar[0].brand << "\n";
cout << bar[0].weight << " ounces \n";
cout << bar[0].calories << " calories \n";
cout << endl;

cout << "The second bar variable holds: \n";
cout << bar[1].brand << "\n";
cout << bar[1].weight << " ounces \n";
cout << bar[1].calories << " calories \n";
cout << endl;

cout << "The third bar variable holds: \n";
cout << bar[2].brand << "\n";
cout << bar[2].weight << " ounces \n";
cout << bar[2].calories << " calories \n";

delete [] bar; // free memory
cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 8

c plus plus

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 a
structure variable. Also, have the program request the pizza diameter before it requests
the pizza company name.

#include <iostream>
#include <string>

using namespace std;

// pizza struct
struct pizza
{
string pizzaCompanyName;
int pizzaDiameter;
int pizzaWeight;
};

int main()
{

// "New" keyword used to allocate memory for the structure
pizza * pie = new pizza;

// Gather info
cout << "Enter the diameter of the pizza in inches: ";
(cin >> pie->pizzaDiameter).get(); // manipulate cin
cout << "Enter the name of the Pizza Company: ";
getline(cin, pie->pizzaCompanyName);
cout << "Enter the weight of the pizza in ounces: ";
cin >> (*pie).pizzaWeight; // Another method of pointing to a structure
cout << "\n";

// Output info
cout << "The pizza company name is: " << pie->pizzaCompanyName << endl;
cout << "The Diameter inches is: " << pie->pizzaDiameter << endl;
cout << "The weight in ounces is: " << pie->pizzaWeight << endl;

// Free memory used by our structure, important.
delete pie;

cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 7

c plus plusExercise 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 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.

#include <iostream>
#include <string>

using namespace std;

// pizza struct
struct pizza
{
string pizzaCompanyName;
int pizzaDiameter;
int pizzaWeight;
};

int main()
{

pizza pie = {pie.pizzaCompanyName, pie.pizzaDiameter, pie.pizzaWeight};

// Gather info
cout << "Enter the name of the Pizza Company: ";
getline(cin, pie.pizzaCompanyName);
cout << "Enter the diameter of the pizza in inches: ";
cin >> pie.pizzaDiameter;
cout << "Enter the weight of the pizza in ounces: ";
cin >> pie.pizzaWeight;
cout << "\n\n";

// Output info
cout << "The pizza company name is: " << pie.pizzaCompanyName << endl;
cout << "The Diameter inches is: " << pie.pizzaDiameter << endl;
cout << "The weight in ounces is: " << pie.pizzaWeight << endl;

cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 6

c plus plusFor 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 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.

#include <iostream>
#include <string>

using namespace std;

// structure
struct candyBar
{
string brand;
double weight;
int calories;
};

int main()
{
// create three members to our struct
candyBar bar[3] =
{
{"Crunch",1.7,275},
{"Heath",2.3,400},
{"Rolo",2.5,350}
};

cout << "The first bar variable holds: \n";
cout << bar[0].brand << "\n";
cout << bar[0].weight << " ounces \n";
cout << bar[0].calories << " calories \n";
cout << endl;

cout << "The second bar variable holds: \n";
cout << bar[1].brand << "\n";
cout << bar[1].weight << " ounces \n";
cout << bar[1].calories << " calories \n";
cout << endl;

cout << "The third bar variable holds: \n";
cout << bar[2].brand << "\n";
cout << bar[2].weight << " ounces \n";
cout << bar[2].calories << " calories \n";

cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 5

c plus plusExercise 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 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.

#include <iostream>
#include <string>

using namespace std;

struct candyBar
{
string brand;
float weight;
int calories;
};

int main()
{
candyBar snack = {"Mocha Munch", 2.3, 350};
cout << "The snack variable holds: \n";
cout << snack.brand << "\n";
cout << snack.weight << " ounces \n";
cout << snack.calories << " calories \n";

cin.get();
return 0;
}