11.6: Focus on Software Engineering: Nested Structures: 10752

11.6: Focus on Software Engineering: Nested Structures: 10752
Question

Assume that  SREC has already been defined. Define a new type,  ROSTER , that is a structure consisting of an  int field,  size , and another field,  grades , that is an  SREC array with 500 elements.

Solution
struct ROSTER
{
    int size;
    SREC grades[500];
};

11.3: Accessing Structure Members: 11153

Question

Assume you are given a variable, payRate of type Money (a structured type with two int fields, dollars and cents). Write the necessary code to increase the effective value of payRate by $1.50. To do this you need to add 50 to cents and 1 to dollars but you ALSO must make sure that cents does not end up over 99: if it does, you need to "carry a one" to dollars. For example, if payRate were $12.80 then a naive addition of $1.50 yields 13 (dollars) and 130 (cents). This would have to be changed to 14 (dollars) and 30 (cents).

Solution
Money payRateIncrease;
payRateIncrease.dollars=1;
payRateIncrease.cents=50;

payRate.dollars += payRateIncrease.dollars + (payRate.cents + payRateIncrease.cents)/100;
payRate.cents = (payRate.cents + payRateIncrease.cents) % 100;

11.3: Accessing Structure Members: 11002

Question

The "origin" of the cartesian plane in math is the point where x and y are both zero. Given a variable, origin of type Point-- a structured type with two fields, x and y, both of type double, write one or two statements that make this variable's field's values consistent with the mathematical notion of "origin".

Solution
origin.x=0.0;
origin.y=0.0;

11.2: Focus on Software Engineering: Combining Data into Structures: 11055

Question

Declare a structure whose tag name is Server and that contains the following fields: manufacturer, a string, model and serialnum, both also strings, year, an int, clockSpeed, a double, cores, an int, ram, an int and finally storage an int.

Solution
struct Server
{
    string manufacturer, model, serialnum;
    int year;
    double clockSpeed;
    int cores, ram, storage;
};

11.2: Focus on Software Engineering: Combining Data into Structures: 10976

Question

Declare a structure whose tag name is Book and that contains exactly three fields (or members), each of type int. The first field is nCopies, the second field is nPages and the third field is nAuthors.

Solution
struct Book
{
    int nCopies, nPages, nAuthors;
};

11.2: Focus on Software Engineering: Combining Data into Structures: 11005

Question

Given the declaration of a structure whose tag is DATE write the declaration of the following variables enrolled_on, paid_on, and completed_on, each of which is of type DATE.

Solution
DATE enrolled_on, paid_on, completed_on;

CH10: Programming Challenge #18

Question

10.18: Phone Number List
Write a program that has an array of at least 50 string objects that hold people’s names and phone numbers. The program then reads lines of text from a file named phonebook  into the array.

The program should ask the user to enter a name or partial name to search for in the array. All entries in the array that match the string entered should be displayed-- thus there may be more than one entry displayed.

Prompts And Output Labels. The program prints the message "Enter a name or partial name to search for: " and then after the user enters a some input and hits return, the program skips a line, and prints the heading: "Here are the results of the search:", followed by each matched string in the array on a line by itself.

Input Validation. None.

Solution (Raezorane R.)


#include 
#include 
#include 
using namespace std;

int main()
{
const int SIZE = 50;
string phoneDirectory[SIZE];
int size=0;
string name; //name to look for

ifstream inFile;
inFile.open("phonebook");
while (!inFile.fail()) {
getline(inFile,phoneDirectory[size]);
size++;
}
inFile.close();

// Get a name or partial name to search for.
cout << "Enter a name or partial name to search for: ";
getline(cin, name);

cout << "\nHere are the results of the search: " << endl;
int numberEntriesFound = 0;

for (int k = 0; k < size; k++)
{
if (phoneDirectory[k].find(name.data(), 0) < phoneDirectory[k].length())
{
numberEntriesFound ++;
cout << phoneDirectory[k] << endl;
}
}
if (numberEntriesFound == 0)
cout << "\nNo Entries were found for " << name;
return 0;


}

CH10: Programming Challenge #15

Question

10.15: Character Analysis
Write a program that reads the contents of a file named text.txt and determines the following:
The number of uppercase letters in the file
The number of lowercase letters in the file
The number of digits in the file

Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following prompts (respectively): "Uppercase characters: ", "Lowercase characters: ", "Digits: ".

Input Validation. None.

Solution (By Raezorane R.)

// Chapter 10, Programming Challenge 15: Character Analysis
#include 
#include 
using namespace std;

int main()
{
char ch; // To hold a character from the file

// Counters for the number of uppercase characters,
// lowercase characters, and digits found in the file.
int uppercase = 0;
int lowercase = 0;
int digits = 0;

// Open the file.
ifstream inputFile;
inputFile.open("text.txt");

// Read each character in the file
// and analyze it.
while (inputFile >> ch)
{
if (isupper(ch))
uppercase++;

if (islower(ch))
lowercase++;

if (isdigit(ch))
digits++;
}

// Close the file.
inputFile.close();

// Display the results.
cout << "Uppercase characters: " << uppercase << endl;
cout << "Lowercase characters: " << lowercase << endl;
cout << "Digits: " << digits << endl;

return 0;