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 5 Exercise 6

c plus plusExercise 6 ask us to create a struct. We have already made structs that are very similar to this (see ch4 exercise 9). We employ the new keyword and create a point to a dynamic array. This way, we recycle the same struct, but change the data within it for different cars. See my solution below:

6. Design a structure called car that holds the following information about an automobile: its make, as a string in a character array or in a string object, and the year it was built, as an integer. Write a program that asks the user how many cars to catalog. The program should then use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care because it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following:

How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser

#include <iostream>
#include <string>

using namespace std;

// create car struct
struct car
{
string make;
int yearBuilt;
};

int main()
{
int cars;

cout << "How many cars do you wish to catalog? ";
cin >> cars;
cout << "\n";

// create dynmaic array with new
car * dynamicArray = new car[cars];

// iterate through our dynamic array
for(int i = 0; i < cars; i++)
{
cout << "For car #" << i+1 << endl;
cout << "Please enter the make: ";
cin >> dynamicArray[i].make;
cout << "Please enter the year made: ";
cin >> dynamicArray[i].yearBuilt;
}

// output our collection
cout << "Here is what is in your collection:" << endl;
for(int i = 0; i < cars; i++)
cout << dynamicArray[i].yearBuilt << " " << dynamicArray[i].make << endl;

// clean
delete [] dynamicArray;

return 0;
}