C++ Primer Plus Chapter 7 Exercise 8

c plus plus Exercise 8 has us re-writing a skeleton program to fill in the missing functions. A combination of structs, pointers, and functions will complete this exercise. Comments describing where and what additions have been made are embedded in the code. See my solution below:

8. This exercise provides practice in writing functions dealing with arrays and structures. The following is a program skeleton. Complete it by providing the described functions:


#include <iostream>

using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};

int getinfo(student pa[], int n);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int n);

int main(){
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student * ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);

for (int i = 0; i < entered; i++) {
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "Done\n";

return 0;
}

// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n)
{
int i;
for(i = 0; i < n; i++)
{
cout << "Student Number: " << (i+1) << "\n";
cout << "Student's Name: ";
cin.getline(pa[i].fullname, SLEN);

cout << "Enter students hobby: ";
cin.getline(pa[i].hobby, SLEN);

cout << "Enter students OOP level: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}

// display1() takes a student structure as an argument
// and displays its contents
void display1(student st)
{
cout << "\n=============Display 1================\n";
cout << "Student name: " << st.fullname << "\n";
cout << "Hobby: " <<  st.hobby << "\n";
cout << "OOP Level: " << st.ooplevel << "\n";
cout << "=============End Display 1==============\n";
}

// display2() takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student * ps)
{
cout << "\n============Display 2================\n";
cout << "Students Name: " << ps->fullname << "\n";
cout << "Hobby: " << ps->hobby << "\n";
cout << "OOP Level" << ps->ooplevel << "\n";
cout << "==========End Display 2================\n";
}

// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student pa[], int n)
{
for(int i = 0; i < n; i++)
{
cout << "\n============Display 3================\n";
cout << "Student: " << (i+1) << "\n";
cout << "Fullname: " << pa[i].fullname << "\n";
cout << "OOP Level: " << pa[i].ooplevel << "\n";
cout << "=============End Display 3============\n";
}
}

C++ Primer Plus Chapter 7 Exercise 6

c plus plusExercise 6 of chapter 7 is not so bad because the first two functions are given to us in the text. However, you do need to do some thinking as to how to reverse an array. There are a few ways to do this. One would be to implement an XOR type algorithm. Another would be to use reverse() from the std library, and then you can do it the way I have done it which is work through the array by starting on the ends. The way I have done it is one of the most common ways to solve this problem and is good to know as it is sometimes used as a skill assessment on programming interviews. See source below.

Write a program that uses the following functions:
Fill_array() takes as arguments the name of an array of double values and an array
size. It prompts the user to enter double values to be entered in the array. It ceases taking
input when the array is full or when the user enters non-numeric input, and it
returns the actual number of entries.
Show_array() takes as arguments the name of an array of double values and an array
size and displays the contents of the array.
Reverse_array() takes as arguments the name of an array of double values and an
array size and reverses the order of the values stored in the array.
The program should use these functions to fill an array, show the array, reverse the array,
show the array, reverse all but the first and last elements of the array, and then show the
array.

#include <iostream>

using namespace std;

const int Max = 5;

int fill_array(double ar[], int limit);
 void show_array(const double ar[], int n);
 void reverse_array(double ar[], int n);

int main()
 {
 double properties[Max];
 int size = fill_array(properties, Max);
 cout << endl;
 show_array(properties, size);
 cout << endl;
 reverse_array(properties, size);
 show_array(properties, size);
 cout << endl;
 reverse_array(properties + 1, size -2);
 show_array(properties, size);
 return 0;
 }

int fill_array(double ar[], int limit)
 {
 double temp;
 int i;
 for(i = 0; i < limit; i++)
 {
 cout << "Enter value #" << (i + 1) << ": ";
 cin >> temp;
 if(!cin)
 {
 cin.clear();
 while(cin.get() != '\n')
 continue;
 cout << "Bad input; input process terminated" << endl;
 break;
 }
 else if(temp < 0)
 break;
 ar[i] = temp;
 }
 return i;
 }

void show_array(const double ar[], int n)
 {
 for (int i = 0; i < n; i++)
 {
 cout << "Property #" << (i + 1) << ": ";
 cout << ar[i] << endl;
 }
 }

void reverse_array(double ar[], int n)
 {
 double temp;
 for(int i = 0; i < n/2; i++)
 {
 temp = ar[i];
 ar[i] = ar[n - i - 1];
 ar[n - i - 1] = temp;
 }
 }

Note: for the sake of the solution. fill_array() could be stripped of the error handling code to make this easier to read.

C++ Primer Plus Chapter 6 Exercise 2

c plus plusThe trick to exercise 2 is not just getting it to average numbers, but getting it to produce how many where over the average. See my solution to this below:

2. Write a program that reads up to 10 donation values into an array of double. The program
should terminate input on non-numeric input. It should report the average of the
numbers and also report how many numbers in the array are larger than the average.

#include <iostream>

const int max = 10;

int main()
{
double donations[max];

std::cout << "Enter your donation amounts\n\n";
std::cout << "Donation #1: ";

int i = 0;
while(i < max && std::cin >> donations[i])
{
if(++i < max)
std::cout << "Donation #" << i+1 << ": ";
}

double total = 0.0;
int over = 0;
for(int j = 0; j < i; j++)
{
total+=donations[j];
}

int avg = total / i;

for(int j = 0; j < i; j++)
{
if(donations[j] > avg)
over++;
}

std::cout << "\n";
std::cout <<  avg << " Average of donations\n";
std::cout << "Number of donations over the average amount " << over << "\n";

return 0;
}