Design a Program That Uses the Following Parallel Arrays
Parallel Arrays and Array Look-Up
- Parallel Arrays
- Algorithm using Parallel Arrays
- The Array Look-up Routine
- Code
- The problem
- Defining Diagram
- The Arrays
- Hierarchy Charts
- Pseudo-code Algorithms
- Flowchart Algorithms
Sample Problem
Parallel Arrays
In the previous material on arrays, we used the indexes to directly access the elements of arrays. The rate of tax_code 1 is stored in rate[1]. The count of rating 1 is accumulated in rating[1]. What if we need to find out the discount rate of the pricing code 'H' (50%) and the discount rate of 'F' (40%)? 'H' and 'F' cannot be easily converted to indexes 1 or 2. The solution is using parallel arrays or paired arrays. They are two arrays having the same number of elements. They are paired because the elements in the first array correspond to the elements in the same position in the second array. For example, the pricing codes 'H', 'F', 'T', 'Q', and 'Z' can be stored in a char array of 5 elements. The corresponding discount rates can be stored in a float array of 5 elements. The first pricing code 'H' in the pricing code array corresponds to the first discount rate 50% of the discount array, and so on.
Declaring these arrays:
char price_code[5] = {'H', 'F', 'T', 'Q', 'Z' };
float discount_rate[5] = {.5, .4, .33, .25, 0. };
Algorithm using Parallel Arrays
The algorithm using the above parallel arrays for this problem is shown below:
Determining_discount_price prompt for price and input_price_code get price and input_price_code set index to 0 // These lines set size to 5 // replace While index < size AND input_price_code != price_code(index) // the original index = index + 1 // nested if EndWhile // statements If index < size If index = 4 Print " No discount " else discount = price * discount_rate[index] new_price = price - discount print price, discount, new_price Endif Else print " Invalid pricing code" Endif End
The Array Look-up Routine
The code with comments in the above pseudocode: set index to 0 // These lines set size to 5 // replace While index < size AND input_price_code != price_code(index) // the original index = index + 1 // nested if EndWhile // statementsThe above is called array look-up routine. It replaces the 5-level deep nested if statement which would be required without the use of an array.
The following is the flowchart algorithm of the array look-up routine.
Code
The code with the array look-up routine as a module is given below. Notice that the parallel arrays are not declared in the same module, nevertheless, they are still paired.
char price_code[5] = {'H', 'F', 'T', 'Q', 'Z' }; float discount_rate[5] = {.5, .4, .33, .25, 0. }; /************************************************************************* * price.cpp Determining the discounted price give the discount code. * * DAT2219D demonstration program * * M Mark, 2000.10.22 * **************************************************************************/ #include <iostream.h> // cin, cout, endl #include <conio.h> // clrscr() int price_code_lookup( char keyValue, int size); // function protype void main( void ) { float discount_rate[5] = {.5, .4, .33, .25, 0. }; // define the array float input_price, discount, new_price; char input_price_code; int index; cout << "Enter a price for the item: "; // prompt for price cin >> input_price; // get price cout << "Enter a price code: "; // prompt for code cin >> input_price_code; // get code // lookup the code array index = price_code_lookup( input_price_code, 5 ); if (index == 4) // if code is 'Z' { cout << "No discount. "; } else { if ( index < 5 ) // if a valid discount code { discount = input_price * discount_rate[index];// calculate discount new_price = input_price - discount; // calculate new discounted // price cout << "Original price: " << input_price // display results << " discount: " << discount << " new price: " << new_price << endl; } else { cout << "invalid pricing code. "; // display invalid // code message } } } /*************************************************************************** * Function: price_code_lookup * Return value: the integer index of the array. * Parameters: The char value to search and size of the array ***************************************************************************/ int price_code_lookup( char keyValue, int size) { char price_code[5] = {'H', 'F', 'T', 'Q', 'Z' }; //declare the array int index = 0; while ( index < size AND keyValue != price_code[index] ) { index = index + 1; } return index; } Question:
Design an algorithm and code it into a C++ program that will read a file of student records containing the student's number, name, subject number and letter grade. Your program is to use the letter grade on the student record to retrieve the corresonding grade points for that letter grade from the paired arrays:
char grade[5] = { 'A', 'B', 'C', 'D'. 'F' }; int point[5] = { 12, 9, 6, 3, 0 }; Print a report showing each student's number, name, grade point, plus the total number of students and the grade point average for all students. The Problem
Senior management at the Company wants to know the labor costs of each of its departments.
Our job is to design a program that will produce a report from the company's existing employee file. The report will list the departments by name and code and show the total weekly payroll (based on the employee's weekly base pay) for the department and the percentage of the company payroll that each department represents. At the end there should be a summary line showing the total company payroll.
The employee file contains the following fields:
| Field name | Type | Field Size | Notes |
| number | String | 7 | 6 + 1 |
| last name | String | 16 | 15 + 1 |
| first name | String | 16 | 15 + 1 |
| address | String | 21 | 20 + 1 |
| next of kin | String | 26 | 25 + 1, (Maybe it's dangerous here) |
| weekly base pay | Number | float | |
| position code | Numeric | int | 1 through 227, not all values used |
| department code | Numeric | int | various three digit codes |
| start date | String | 9 | yyyymmdd, 8 + 1 |
| birth date | String | 9 | yyyymmdd, 8 + 1 |
Currently there are seven departments with numeric codes assigned to each. Here is the list
| Department | Code |
| Accounts | 167 |
| Design | 111 |
| Marketing | 129 |
| Production | 431 |
| Safety | 555 |
| Procurement | 132 |
| Human Resources | 384 |
The Arrays
There are three parallel arrays needed here, each one containing different information about the departments. They all contain the same number of elements.
- An integer array of Departmental Codes.
- A string array of Departmental Names.
- A float array of Departmental Pay Totals.
The index of any one will give access to the corresponding elements in the other two.
Since we have the Departmental Code in our file we will search for a match to it and use that index to accumulate totals and display our report.
ITO Feasibility Chart
| Input | Transforms | Output |
Employee File Records
Department List
| For each Department List entry: DepartmentPayroll[Code] = sum of Weekly Base Pay for Employee records with this Department Code TotalCompanyPayroll = DepartmentPercentage = | Report:
|
Hierarchy Charts
Design Phase Hierarchy Chart
Which will simplify when we implement it to:
Pseudocode Algorithms:
Prepare_Departmental_Report Initialize DepartmentTotalPay Array to zeros Open EmployeeFile If opened Read EmployeeFile While Not EOF [Process_Employee] Read EmployeeFile EndWhile Close EmployeeFile [Display_Report] Else Display "Could not open Employee file !" EndIf End
Process_Employee index = 0 While Employee.DepartmentCode Not = DepartmentCode[index] AND index <= MAX_INDEX increment index EndWhile If index <= MAX_INDEX Add Employee.WeeklyBasePay to DepartmentTotalPay[index] Add Employee.WeeklyBasePay to CompanyTotalPay Else Display "Bad Department Code for " Employee.Number EndIf Return
Display_Report Display Headings [Process_Departments] Display CompanyTotalPay Return
Process_Departments index = 0 While index <= MAX_INDEX DepartmentalPercentage = (DepartmentTotalPay[index] / CompanyTotalPay) X 100 Display DepartmentName[index] Display DepartmentCode[index] Display DepartmentTotalPay[index] Display DepartmentalPercentage Increment index EndWhile Return
Flowchart Algorithms
Design a Program That Uses the Following Parallel Arrays
Source: https://elearning.algonquincollege.com/coursemat/pincka/dat2219d.f03/lectures/39-Parallel-Arrays.htm
0 Response to "Design a Program That Uses the Following Parallel Arrays"
Post a Comment