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

c plus plusTo understand recursion, one must understand recursion.

Define a recursive function that takes an integer argument and returns the factorial of that argument. Recall that 3 factorial, written 3!, equals 3 × 2!, and so on, with 0! defined as 1. In general, if n is greater than zero, n! = n * (n – 1)!. Test your function in a program that uses a loop to allow the user to enter various values for which the program reports the factorial.

#include <iostream>

using namespace std;

int factorial(int number);

int main()
{
    int number;
    while(number != -1)
    {
        cout << "Enter a number to find it's factorial (q to quit): ";
        cin >> number;
        if(!cin.good())
            break;
        else
        {
            int factor;
            factor = factorial(number);
            cout << "Factorial " << number << " is " << factor << endl;
        }
    }
    return 0;
}

int factorial(int number)
{
    if(number == 0)
        return 1;
    return number * factorial(number - 1);
}

Oracle SQL Employee Database Creation Script

This is a Oracle SQL Table creation script I made awhile back in college. After running this, you will have a functional normalized employee database up and running. The script works to setup a clean database instance from scratch and could be used as a good base example for you guys and gals in a relational database class or if you are just wanting to see the syntax. It begins executing commands to create an administrator user. Then, it drops all tables and sequences it might find so it can have a clean slate. Tables are then created, sequencing added, data inserted, a view is created, etc. Take a look at the source to see complete function list. Better yet, run it, and see what you end up with.

/* Employee Database Table Creation Script, Platform - Oracle SQL Server/Developer

-- Begin db connection and clear workspace
-- Also suppresses error messages if tables do not exist

-- Create user cam
CONNECT system/Oracle11g;
prompt>Dropping users
DROP USER cam CASCADE;

prompt>Creating users
CREATE USER cam IDENTIFIED BY cam DEFAULT TABLESPACE users;

prompt>Granting privileges
GRANT ALL PRIVILEGES TO cam;

--Connect as user cam
CONNECT cam/cam;

BEGIN
EXECUTE IMMEDIATE 'DROP VIEW Employee_Labs_vw';

EXECUTE IMMEDIATE 'DROP SEQUENCE EmployeeID_seq';
EXECUTE IMMEDIATE 'DROP SEQUENCE LabID_seq';
EXECUTE IMMEDIATE 'DROP SEQUENCE WorkID_seq';

EXECUTE IMMEDIATE 'DROP TABLE tblEmployeeSchedule';
EXECUTE IMMEDIATE 'DROP TABLE tblLab';
EXECUTE IMMEDIATE 'DROP TABLE tblShift';
EXECUTE IMMEDIATE 'DROP TABLE tblEmployee';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('');
END;
/
-- Begin db create tables

CREATE TABLE tblEmployee
(
EmployeeID                INTEGER             NOT NULL,
EmpFName                 VARCHAR2 (25)    NOT NULL,
EmpLName                 VARCHAR2 (25)   NOT NULL,
EmpSSN                     VARCHAR2 (12)    NOT NULL,
JobTitle                 VARCHAR2 (25),
CONSTRAINT tblEmployee_pk
PRIMARY KEY (EmployeeID)
);

CREATE TABLE tblLab
(
LabID                     INTEGER            NOT NULL,
LabDescr                 VARCHAR2(50),
CONSTRAINT tblLab_pk
PRIMARY KEY (LabID)
);

CREATE TABLE tblShift
(
ShiftID                 INTEGER            NOT NULL,
DayofWeek                 CHARACTER(3),
StartTime                 VARCHAR2 (5),
EndTime                 VARCHAR2 (5),
CONSTRAINT tblShift_pk
PRIMARY KEY (ShiftID)
);

CREATE TABLE tblWorksOn
(
WorkID                    INTEGER        NOT NULL,
EmployeeID                INTEGER        NOT NULL,
LabID                    INTEGER     NOT NULL,
CONSTRAINT tblWorksOn_pk
PRIMARY KEY (WorkID),
CONSTRAINT tblWorksOnEmpID_fk
FOREIGN KEY (EmployeeID) REFERENCES tblEmployee(EmployeeID),
CONSTRAINT tblWorksOnLab_fk
FOREIGN KEY (LabID)    REFERENCES tblLab(LabID)
);

CREATE TABLE tblEmployeeSchedule
(
AssignmentID             INTEGER        NOT NULL,
EmployeeID                 INTEGER        NOT NULL,
LabID                     INTEGER        NOT NULL,
ShiftID                    INTEGER        NOT NULL,
WorkID                    INTEGER        NOT NULL,
CONSTRAINT tblEmployeeSchedule_pk
PRIMARY KEY (AssignmentID),
CONSTRAINT tblEmployeeScheduleEmp_fk
FOREIGN KEY (EmployeeID) REFERENCES tblEmployee(EmployeeID),
CONSTRAINT tblEmployeeScheduleLab_fk
FOREIGN KEY (LabID) REFERENCES tblLab(LabID),
CONSTRAINT tblEmployeeScheduleShift_fk
FOREIGN KEY (ShiftID) REFERENCES tblShift(ShiftID),
CONSTRAINTS tblWorksOn_fk
FOREIGN KEY (WorkID) REFERENCES tblWorksOn(WorkID)
);

-- Sequence
CREATE SEQUENCE EmployeeID_seq
START WITH 125
INCREMENT BY 5;
CREATE SEQUENCE LabID_seq
START WITH 225
INCREMENT BY 5;
CREATE SEQUENCE WorkID_seq
START WITH 335
INCREMENT BY 5;

-- Insert employees into db

INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL,'Cameron', 'Anglin', '123-45-6789', 'Chief Executive Officer');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL,'Jimi', 'Hendrix', '124-78-9923', 'Scientist');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'Linus', 'Torvalds', '332-05-6512', 'Software Engineer');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'Bill', 'Gates', '909-76-3467', 'Software Engineer');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'Clarke', 'Kent', '250-77-9843', 'Experimental Genetics');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'Tony','Stark', '221-32-3234', 'Electrical Engineer');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'Doc', 'Brown', '676-56-8892','Scientist');
INSERT INTO tblEmployee
(EmployeeID, EmpFName, EmpLName, EmpSSN, JobTitle)
VALUES (EmployeeID_seq.NEXTVAL, 'John','Grishsam', '120-45-2156', 'Accountant');

-- Insert employee Labs into db

INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Engineering');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Particle Physics');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Experimental Genetics');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Extraterrestrial Relations ');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Cryptology');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Human Resources');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Finance');
INSERT INTO tblLab
(LabID, LabDescr)
VALUES (LabID_seq.NEXTVAL, 'Board');

-- Insert shift types into db

INSERT INTO tblShift
(ShiftID, DayofWeek, StartTime, EndTime)
VALUES (1,'Mon', '07:00', '3:30');
INSERT INTO tblShift
(ShiftID, DayofWeek, StartTime, EndTime)
VALUES (2,'Tue', '07:30', '3:30');
INSERT INTO tblShift
(ShiftID, DayofWeek, StartTime, EndTime)
VALUES (3,'Wed', '07:30', '3:30');
INSERT INTO tblShift
(ShiftID, DayofWeek, StartTime, EndTime)
VALUES (4,'Thu', '07:30', '3:30');
INSERT INTO tblShift
(ShiftID, DayofWeek, StartTime, EndTime)
VALUES (5,'Fri', '07:30', '3:30');

-- Insert into tblWorksOn

INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,125, 260);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,130, 235);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,135, 225);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,140, 225);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,145, 235);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,150, 225);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,155, 230);
INSERT INTO tblWorksOn
(WorkID, EmployeeID, LabID)
VALUES (WorkID_seq.NEXTVAL,160, 255);

-- Create view

CREATE OR REPLACE FORCE VIEW "EMPLOYEE_LABS_VW" ("EMPFNAME", "EMPLNAME", "LABORATORY") AS
SELECT
e.EmpFName,
e.EmpLName,
l.LabDescr AS Laboratory
FROM
tblEmployee e,
tblWorksOn w,
tblLab l
WHERE
e.EmployeeID = w.EmployeeID AND
l.LabID = w.LabID
ORDER BY e.EmpLName;

COMMIT;

C++ Primer Plus Chapter 7 Exercise 4

c plus plusThe trick to this program is understanding what is it asking and getting it to work as it wants. In my solution I created a second function that holds all the numbers plus the probability function within it. I think this is one of the quickest solution to this exercise. The output is truncated to that of a long int, as I think the book format is quite ugly. Another way I had though of doing this originally was to calculate the odds of 1 in 47, store it. Calculate the 1 in 27 odds, store it, then find the product of those all in their own functions. That idea seemed clumsy when coding it and all roads were pointing to the solution provided for me.

Many state lotteries use a variation of the simple lottery portrayed by Listing 7.4. In
these variations you choose several numbers from one set and call them the field numbers.
For example, you might select 5 numbers from the field of 1–47). You also pick a
single number (called a mega number or a power ball, etc.) from a second range, such as
1–27. To win the grand prize, you have to guess all the picks correctly. The chance of
winning is the product of the probability of picking all the field numbers times the probability
of picking the mega number. For instance, the probability of winning the example
described here is the product of the probability of picking 5 out of 47 correctly times the
probability of picking 1 out of 27 correctly. Modify Listing 7.4 to calculate the probability
of winning this kind of lottery.

#include <iostream>

using namespace std;

// Note: some implementations require double instead of long double
long double probability(unsigned numbers, unsigned picks);
long int TotalOdds(int, int, int, int, long double(*p)(unsigned, unsigned));

int main()
{
cout << "Welcome to the Powerball!\n";
cout << "Odds of winning are one in " << TotalOdds(47, 5, 27, 1, probability);
cout << "\nThanks for playing!" << endl;

return 0;
}
// the following function calculates the probability of picking picks
// numbers correctly from numbers choices
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0; // here come some local variables
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
result = result * n / p;
return result;
}

long int TotalOdds(int FirstSet, int x, int PowerBall, int y, long double(*p)(unsigned, unsigned))
{
long double odds;
odds = p(FirstSet, x)*p(PowerBall, y);
return odds;
}