0% found this document useful (0 votes)
30 views

Week 4

The document discusses using structs to store sales data for multiple salespeople. It describes analyzing the problem, designing an algorithm to read input files of salesperson IDs and sales data, calculate totals for each salesperson and quarter, and output a report. The algorithm includes initializing a struct array, processing the sales data file, and calling functions to calculate totals, print the report, and find maximum sales.

Uploaded by

aneeq ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Week 4

The document discusses using structs to store sales data for multiple salespeople. It describes analyzing the problem, designing an algorithm to read input files of salesperson IDs and sales data, calculate totals for each salesperson and quarter, and output a report. The algorithm includes initializing a struct array, processing the sales data file, and calling functions to calculate totals, print the report, and find maximum sales.

Uploaded by

aneeq ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

C++ Programming: From Problem Analysis to

Program Design, Fifth Edition

Chapter 11: Records (structs)


structs within a struct

versus

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2


Programming Example: Sales Data
Analysis
• A company has six salespeople
• Every month they go on road trips to sell
the company’s product
• At the end of each month, the total sales
for each salesperson, salesperson’s ID,
and the month, are recorded in a file
• At the end of each year, the manager of
the company asks for a report

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3


Programming Example: Output
Format
----------- Annual Sales Report -------------

ID QT1 QT2 QT3 QT4 Total


______________________________________________________________
12345 1892.00 0.00 494.00 322.00 2708.00
32214 343.00 892.00 9023.00 0.00 10258.00
23422 1395.00 1901.00 0.00 0.00 3296.00
57373 893.00 892.00 8834.00 0.00 10619.00
35864 2882.00 1221.00 0.00 1223.00 5326.00
54654 893.00 0.00 392.00 3420.00 4705.00
Total 8298.00 4906.00 18743.00 4965.00

Max Sale by SalesPerson: ID = 57373, Amount = $10619.00


Max Sale by Quarter: Quarter = 3, Amount = $18743.00

QT1 stands for quarter 1 (months 1 to 3), QT2 for quarter 2


(months 4 to 6), QT3 for quarter 3 (months 7 to 9), and QT4 for
quarter 4 (months 10 to 12)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
Programming Example: Output
Format (cont'd.)
• The salespeople IDs are stored in one file;
sales data are stored in another file
• The sales data is in the following form:
salesPersonID month saleAmount
.
.
.
• Sales data are not ordered

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5


Programming Example:
Input/Output
• Input: file containing each salesperson’s
ID and a second file containing the sales
data
• Output: file containing annual sales report
in the above format

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6


Programming Example: Problem
Analysis
• Main components for each salesperson:
– ID
– Quarterly sales amount
– Total annual sales amount
• Use a struct to group the components
• Six people: array of size six
• Program requires total sales for each
quarter
– Use array of size four to store the data
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7
Programming Example: Problem
Analysis (cont'd.)

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8


Programming Example: Problem
Analysis (cont'd.)
• Read the salespeople IDs into the array
salesPersonList
• Initialize the quarterly sales and total sales
for each salesperson to 0

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9


Programming Example: Problem
Analysis (cont'd.)
• For each entry in the file with the sales
data:
– Read ID, month, sale amount for the month
– Search salesPersonList to locate the
component corresponding to this salesperson
– Determine the quarter corresponding to the
month
– Update the sales for the quarter by adding the
sale amount for the month

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10


Programming Example: Problem
Analysis (cont'd.)
• Once the sales data file is processed:
– Calculate the total sale by salesperson
– Calculate the total sale by quarter
– Print the report

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11


Programming Example: Algorithm
Design
• Translates into the following algorithm:
– Initialize the array salesPersonList
– Process the sales data
– Calculate the total sale by salesperson
– Calculate the total sale by quarter
– Print the report
– Calculate and print maximum sale by
salesperson
– Calculate and print maximum sale by quarter
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
Programming Example: Main
Algorithm
• Declare the variables
• Prompt user to enter name of file
containing the salesperson’s ID data
• Read the name of the input file
• Open the input file
• If input file does not exist, exit
• Initialize the array salesPersonList by
calling the function initialize
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
Programming Example: Main
Algorithm (cont'd.)
• Close input file containing salesperson’s ID
• Prompt user to enter name of file containing
sales data
• Read the name of the input file
• Open the input file
• If input file does not exist, exit
• Prompt user to enter name of output file
• Read the name of the output file

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14


Programming Example: Main
Algorithm (cont'd.)
• Open the output file
• Output data to two decimal places
• Process sales data
– Call the function getData
• Calculate the total sale by quarter by
calling the function saleByQuarter
• Calculate the total sale by salesperson by
calling the function totalSaleByPerson
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15
Programming Example: Main
Algorithm (cont'd.)
• Print the report in the tabular form; call the
function printReport
• Find and print the salesperson who
produces the maximum sales for the year
by calling maxSaleByPerson
• Find and print the quarter producing the
maximum sale for the year by calling
maxSaleByQuarter
• Close files
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16
Summary

• struct: collection of a fixed number of


components
• Components can be of different types
– Called members
– Accessed by name
• struct is a reserved word
• No memory is allocated for a struct
– Memory when variables are declared

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17


Summary (cont'd.)
• Dot (.) operator: member access operator
– Used to access members of a struct
• The only built-in operations on a struct are
the assignment and member access
• Neither arithmetic nor relational operations
are allowed on structs
• struct can be passed by value or reference
• A function can return a value of type struct
• structs can be members of other structs

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18

You might also like