Friday, November 4, 2022

Pr11-8.cpp

 // This program demonstrates a function that uses a

// pointer to a structure variable as a parameter.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Student
{
   string name;			  // Student's name
   int idNum;             // Student ID number
   int creditHours;       // Credit hours enrolled
   double gpa;			  // Current GPA
};

void getData(Student *);  // Function prototype

int main()
{
   Student freshman;

   // Get the student data.
   cout << "Enter the following student data:\n";
   getData(&freshman);    // Pass the address of freshman.
   cout << "\nHere is the student data you entered:\n";

   // Now display the data stored in freshman
   cout << setprecision(3);
   cout << "Name: " << freshman.name << endl;
   cout << "ID Number: " << freshman.idNum << endl;
   cout << "Credit Hours: " << freshman.creditHours << endl;
   cout << "GPA: " << freshman.gpa << endl;
   return 0;
}

//*******************************************************
// Definition of function getData. Uses a pointer to a  *
// Student structure variable. The user enters student  *
// information, which is stored in the variable.        *
//*******************************************************

void getData(Student *s)
{
   // Get the student name.
   cout << "Student name: ";
   getline(cin, s->name);

   // Get the student ID number.
   cout << "Student ID Number: ";
   cin >> s->idNum;

   // Get the credit hours enrolled.
   cout << "Credit Hours Enrolled: ";
   cin >> s->creditHours;

   // Get the GPA.
   cout << "Current GPA: ";
   cin >> s->gpa;
}

Pr11-7.cpp

 // This program uses a function to return a structure. This

// is a modification of Program 11-2.
#include <iostream>
#include <iomanip>
#include <cmath>  // For the pow function
using namespace std;

// Constant for Pi.
const double PI = 3.14159;

// Structure declaration
struct Circle
{
   double radius;      // A circle's radius
   double diameter;    // A circle's diameter
   double area;        // A circle's area
};

// Function prototype
Circle getInfo();

int main()
{
   Circle c;      // Define a structure variable

   // Get data about the circle.
   c = getInfo();
   
   // Calculate the circle's area.
   c.area = PI * pow(c.radius, 2.0);
   
   // Display the circle data.
   cout << "The radius and area of the circle are:\n";
   cout << fixed << setprecision(2);
   cout << "Radius: " << c.radius << endl;
   cout << "Area: " << c.area << endl;
   return 0;
}

//***************************************************************
// Definition of function getInfo. This function uses a local   *
// variable, tempCircle, which is a circle structure. The user  *
// enters the diameter of the circle, which is stored in        *
// tempCircle.diameter. The function then calculates the radius *
// which is stored in tempCircle.radius. tempCircle is then     *
// returned from the function.                                  *
//***************************************************************

Circle getInfo()
{
   Circle tempCircle;  // Temporary structure variable

   // Store circle data in the temporary variable.
   cout << "Enter the diameter of a circle: ";
   cin >> tempCircle.diameter;
   tempCircle.radius = tempCircle.diameter / 2.0;
   
   // Return the temporary variable.
   return tempCircle;
}

Pr11-6.cpp

 // This program has functions that accept structure variables

// as arguments.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct InventoryItem
{
   int partNum;                  // Part number
   string description;			 // Item description
   int onHand;                   // Units on hand
   double price;                 // Unit price
};

// Function Prototypes
void getItem(InventoryItem&);    // Argument passed by reference
void showItem(InventoryItem);    // Argument passed by value

int main()
{
   InventoryItem part;

   getItem(part);
   showItem(part);
   return 0;
}

//***********************************************************
// Definition of function getItem. This function uses       *
// a structure reference variable as its parameter. It asks *
// the user for information to store in the structure.      *
//***********************************************************

void getItem(InventoryItem &p)   // Uses a reference parameter
{
   // Get the part number.
   cout << "Enter the part number: ";
   cin >> p.partNum;

   // Get the part description.
   cout << "Enter the part description: ";
   cin.ignore();  // Ignore the remaining newline character
   getline(cin, p.description);

   // Get the quantity on hand.
   cout << "Enter the quantity on hand: ";
   cin >> p.onHand;

   // Get the unit price.
   cout << "Enter the unit price: ";
   cin >> p.price;
}

//***********************************************************
// Definition of function showItem. This function accepts   *
// an argument of the InventoryItem structure type. The     *
// contents of the structure is displayed.                  *
//***********************************************************

void showItem(InventoryItem p)
{
   cout << fixed << showpoint << setprecision(2);
   cout << "Part Number: " << p.partNum << endl;
   cout << "Description: " << p.description << endl;
   cout << "Units On Hand: " << p.onHand << endl;
   cout << "Price: $" << p.price << endl;
} 

Pr11-5.cpp

 // This program uses nested structures.

#include <iostream>
#include <string>
using namespace std;

// The Date structure holds data about a date.
struct Date
{
   int month;
   int day;
   int year;
};

// The Place structure holds a physical address.
struct Place
{
   string address;
   string city;
   string state;
   string zip;
};

// The EmployeeInfo structure holds an employee's data.
struct EmployeeInfo
{
   string name;
   int employeeNumber;
   Date birthDate;           // Nested structure
   Place residence;          // Nested structure
};

int main()
{
   // Define a structure variable to hold info about the manager.
   EmployeeInfo manager;

   // Get the manager's name and employee number
   cout << "Enter the manager's name: ";
   getline(cin, manager.name);
   cout << "Enter the manager's employee number: ";
   cin >> manager.employeeNumber;
   
   // Get the manager's birth date
   cout << "Now enter the manager's date of birth.\n";
   cout << "Month (up to 2 digits): ";
   cin >> manager.birthDate.month;
   cout << "Day (up to 2 digits): ";
   cin >> manager.birthDate.day;
   cout << "Year: ";
   cin >> manager.birthDate.year;
   cin.ignore();  // Skip the remaining newline character
   
   // Get the manager's residence information
   cout << "Enter the manager's street address: ";
   getline(cin, manager.residence.address);
   cout << "City: ";
   getline(cin, manager.residence.city);
   cout << "State: ";
   getline(cin, manager.residence.state);
   cout << "ZIP Code: ";
   getline(cin, manager.residence.zip);
   
   // Display the information just entered
   cout << "\nHere is the manager's information:\n";
   cout << manager.name << endl;
   cout << "Employee number " << manager.employeeNumber << endl;
   cout << "Date of birth: ";
   cout << manager.birthDate.month << "-";
   cout << manager.birthDate.day << "-";
   cout << manager.birthDate.year << endl;
   cout << "Place of residence:\n";
   cout << manager.residence.address << endl;
   cout << manager.residence.city << ", ";
   cout << manager.residence.state << "  ";
   cout << manager.residence.zip << endl;
   return 0;
}

Pr11-4

 // This program uses an array of structures.

#include <iostream>
#include <iomanip>
using namespace std;

struct PayInfo
{
   int hours;        // Hours Worked
   double payRate;   // Hourly Pay Rate
};

int main()
{
   const int NUM_WORKERS = 3;    // Number of workers
   PayInfo workers[NUM_WORKERS]; // Array of structures
   int index;                    // Loop counter

   // Get employee pay data.
   cout << "Enter the hours worked by " << NUM_WORKERS 
        << " employees and their hourly rates.\n";

   for (index = 0; index < NUM_WORKERS; index++)
   {
      // Get the hours worked by an employee.
      cout << "Hours worked by employee #" << (index + 1);
      cout << ": ";
      cin >> workers[index].hours;
      
      // Get the employee's hourly pay rate.
      cout << "Hourly pay rate for employee #";
      cout << (index + 1) << ": ";
      cin >> workers[index].payRate;
      cout << endl;
   }

   // Display each employee's gross pay.
   cout << "Here is the gross pay for each employee:\n";
   cout << fixed << showpoint << setprecision(2);
   for (index = 0; index < NUM_WORKERS; index++)
   {
      double gross;
      gross = workers[index].hours * workers[index].payRate;
      cout << "Employee #" << (index + 1);
      cout << ": $" << gross << endl;
   }
   return 0;
}

Pr11-3

 // This program demonstrates partially initialized

// structure variables.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct EmployeePay
{
   string name;			// Employee name
   int empNum;          // Employee number
   double payRate;      // Hourly pay rate
   double hours;        // Hours worked
   double grossPay;     // Gross pay
};

int main()
{
   EmployeePay employee1 = {"Betty Ross", 141, 18.75};
   EmployeePay employee2 = {"Jill Sandburg", 142, 17.50};

   cout << fixed << showpoint << setprecision(2);

   // Calculate pay for employee1
   cout << "Name: " << employee1.name << endl;
   cout << "Employee Number: " << employee1.empNum << endl;
   cout << "Enter the hours worked by this employee: ";
   cin >> employee1.hours;
   employee1.grossPay = employee1.hours * employee1.payRate;
   cout << "Gross Pay: " << employee1.grossPay << endl << endl;

   // Calculate pay for employee2
   cout << "Name: " << employee2.name << endl;
   cout << "Employee Number: " << employee2.empNum << endl;
   cout << "Enter the hours worked by this employee: ";
   cin >> employee2.hours;
   employee2.grossPay = employee2.hours * employee2.payRate;
   cout << "Gross Pay: " << employee2.grossPay << endl;
   return 0;
}

Pr11-2

 // This program stores data about a circle in a structure.

#include <iostream>
#include <cmath>  // For the pow function
#include <iomanip>
using namespace std;

// Constant for Pi.
const double PI = 3.14159;

// Structure declaration
struct Circle
{
   double radius;      // A circle's radius
   double diameter;    // A circle's diameter
   double area;        // A circle's area
};

int main()
{
   Circle c;    // Define a structure variable

   // Get the circle's diameter.
   cout << "Enter the diameter of a circle: ";
   cin >> c.diameter;

   // Calculate the circle's radius.
   c.radius = c.diameter / 2;

   // Calculate the circle's area.
   c.area = PI * pow(c.radius, 2.0);

   // Display the circle data.
   cout << fixed << showpoint << setprecision(2);
   cout << "The radius and area of the circle are:\n";
   cout << "Radius: " << c.radius << endl;
   cout << "Area: " << c.area << endl;
   return 0;
}

Chapter 11 Structures: Source Code. Pr11-1

 // This program demonstrates the use of structures.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct PayRoll
{
   int empNumber;    // Employee number
   string name;		 // Employee's name
   double hours;     // Hours worked
   double payRate;   // Hourly payRate
   double grossPay;  // Gross Pay
};

int main()
{
   PayRoll employee; // employee is a PayRoll structure.

   // Get the employee's number.
   cout << "Enter the employee's number: ";
   cin >> employee.empNumber;

   // Get the employee's name.
   cout << "Enter the employee's name: ";
   cin.ignore();	// To skip the remaining '\n' character
   getline(cin, employee.name);

   // Get the hours worked by the employee.
   cout << "How many hours did the employee work? ";
   cin >> employee.hours;

   // Get the employee's hourly pay rate.
   cout << "What is the employee's hourly payRate? ";
   cin >> employee.payRate;

   // Calculate the employee's gross pay.
   employee.grossPay = employee.hours * employee.payRate;

   // Display the employee data.
   cout << "Here is the employee's payroll data:\n";
   cout << "name: " << employee.name << endl;
   cout << "Number: " << employee.empNumber << endl;
   cout << "hours worked: " << employee.hours << endl;
   cout << "Hourly payRate: " << employee.payRate << endl;
   cout << fixed << showpoint << setprecision(2);
   cout << "Gross Pay: $" << employee.grossPay << endl;
   return 0;
}

NaCl.xyz Generator with Interactive 3D Visualization

NaCl.xyz Generator with Interactive 3D Visualization NaCl.xyz Generator with Interactive 3D Visualization ...