# C++ Primer Plus Chapter 7 Exercise 9 Chapter 7 ends with a short exercise on function pointers. The simplest way to complete this, which I provided in my source, is to create the two functions add() and calculate(), then call them in main() with a loop. The text mentions if you are feeling adventurous to include other functions in addition to add. For simplicities sake, I did the minimum requirement here. Finally,  we can put this chapter to rest. See my source below:

```#include <iostream>

using namespace std;

double calculate(double x, double y, double (*pf)(double a, double b));

int main()
{
double x, y;
cout << "Enter two numbers (q to exit): ";
while(cin >> x >> y)
{
if(cin.fail())
break;
cout << "The sum is: " << calculate(x, y, add) << "\n\n";
}
return 0;
}

double calculate(double x, double y, double (*pf)(double a, double b))
{
return pf(x, y);
}

{
return x + y;
}
```

# C++ Primer Plus Chapter 7 Exercise 7 This exercise makes use heavier use of pointers then what we have been doing. However, much of this program can be pulled from listing 7.7 and around that part of the chapter. See source below.

Redo Listing 7.7, modifying the three array-handling functions to each use two pointer parameters to represent a range. The fill_array() function, instead of returning the actual number of items read, should return a pointer to the location after the last location filled; the other functions can use this pointer as the second argument to identify the end of the data.

```#include <iostream>

using namespace std;

const int Max = 5;

// function prototypes
double * fill_array(double *first, double *last);
void show_array(const double *first, const double *last);
void revalue(double *first, double *last, double factor);

int main()
{
double properties[Max];
double * last;

last = fill_array(properties, properties+Max);
show_array(properties, last);

cout << "Enter revaluation factor: ";
double factor;
cin >> factor;

revalue(properties, last, factor);
show_array(properties, last);
cout << "Done.\n";

return 0;
}

double *fill_array(double *first, double *last)
{
double temp;
double *pt;
int i=0;
for (pt = first; pt != last; pt++, i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
*pt = temp;
}
return pt;
}

void show_array(const double *first, const double *last)
{
const double *pt;
int i=0;
for (pt = first; pt != last; pt++, i++)
{
cout << "Property #" << (i + 1) << ": \$ \n";
cout << *pt;
}
}

void revalue(double *first, double *last, double factor)
{
double *pt;
for (pt = first; pt != last; pt++)
*pt *= factor;
}

```

# C++ Primer Plus Chapter 4 Exercise 8

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 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;
}

```