must used code blocks C++ This assignment demonstrates array of STRUCT (structur
ID: 3704467 • Letter: M
Question
must used code blocks C++
This assignment demonstrates array of STRUCT (structures), two dimensional arrays and the use of index sorting routine.
The problem:
A trucking company charges the customers a fixed amount per pound, based on the following (Zone-Area) table.
Area code Area code Area code
Zone 1 2 3
code
1 .40 .55 .68
2 .60 .70 .85
3 .75 .79 .96
4 .90 1.05 1.10
5 .99 1.08 1.15
Write a program which will read in the above table into a two dimensional array. The array will be used to calculate the shipping cost. Your program will read in the name of the customer, the zone-area code, and the weight of the item shipped. The zone-area code consists of two digits, the first one indicates the zone, and the second indicates the area.
Assume there are less than 20 records for one truckload.
Print all customers’ information and their shipping charges in a report form. The end of the report must contain the total of all of the customers’ weight and a total of all the shipping charges.
Reproduce the above report in alphabetical order by customers name
Reproduce the above report sorted by weight.
Produce a report of all the companies in area code number two.
Your test data will be similar to the following:
3
Chevrolet 23 750
Zapata Co. 53 385
Schlumberger 51 1104
The first line of the data file will contain a header record indicating how many customer records are to follow.
The actual test data will be stored under DATA7.txt
The Zone-Area table will be stored under TABLE7.txt
Submit your source code in blackboard and demonstrate your program in lab.
Documentation will be 20% of your grade.
Your source code must contain the following documentation.
Header information: (Source Code and Output)
Your Name, course & section number, assignment number and due date.
A brief description of your assignment.
Variable dictionary: A brief description of every variable used in your program.
Comments related to input and output of data and all formulas used in your code.
data7.txt
table7.txt
Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
typedef struct test_data
{
string name;
int zone_area_code;
double weight;
}test_data;
bool customer_sorter(test_data const& lhs, test_data const& rhs)
{
return lhs.name < rhs.name;
}
bool customer_sorter1(test_data const& lhs, test_data const& rhs)
{
return lhs.weight < rhs.weight;
}
int main()
{
//stream for files
std::ifstream infile1("table7.txt");
std::ifstream infile2("data7.txt");
int i,j,row,area_code,zone,area;
string customer_name;
double weight,total_weight,total_charges;
//create array for zone area
double zone_area_arr[5][3];
test_data *arr;
//if table7.txt file present then
if(infile1)
{
i = 0;
double a1,a2,a3;
//reading file line by line and storing in 2D array
while(infile1 >> a1 >> a2 >> a3)
{
j = 0;
zone_area_arr[i][j++] = a1;
zone_area_arr[i][j++] = a2;
zone_area_arr[i][j++] = a3;
i++;
}
}
else
{
cout<<"File table7.txt Not present"<<endl;
return 0;
}
//if data7.txt is present
if(infile2)
{
infile2 >> row; //reading first value from file
arr = new test_data[row];
i = 0;
//reading file line by line and store it in array of struct
while(infile2 >> customer_name >> area_code >> weight)
{
arr[i].name = customer_name;
arr[i].weight = weight;
arr[i].zone_area_code = area_code;
i++;
}
}
else
{
cout<<"File data7.txt Not present"<<endl;
return 0;
}
total_weight = 0.0;
total_charges = 0.0;
double charges;
cout<<" Customer Information"<<endl<<endl;
cout<<"======================================================================"<<endl;
for(int k = 0; k < row; k++) //printing data with charges
{
area = arr[k].zone_area_code % 10;
zone = arr[k].zone_area_code / 10;
charges = zone_area_arr[zone-1][area-1] * arr[k].weight;
cout<<arr[k].name<<" "<<arr[k].zone_area_code<<" "<<arr[k].weight<<" "<<charges<<endl;
total_weight += arr[k].weight; //calculating total weight and total charges
total_charges += charges;
}
cout<<"======================================================================"<<endl;
cout<<"total of all of the customer's weight = "<<total_weight<<endl;
cout<<"total of all the shipping charges = "<<total_charges<<endl;
cout<<" Report in alphabetical order by customers name"<<endl<<endl;
cout<<"======================================================================"<<endl;
//sorting array depending on name
std::sort(arr, arr+row, &customer_sorter);
for(int k = 0; k < row; k++)
{
cout<<arr[k].name<<" "<<arr[k].zone_area_code<<" "<<arr[k].weight<<" "<<endl;
}
cout<<"======================================================================"<<endl;
cout<<" report sorted by weight"<<endl<<endl;
cout<<"======================================================================"<<endl;
//sorting array of struct on weight
std::sort(arr, arr+row, &customer_sorter1);
for(int k = 0; k < row; k++)
{
cout<<arr[k].name<<" "<<arr[k].zone_area_code<<" "<<arr[k].weight<<" "<<endl;
}
cout<<"======================================================================"<<endl;
//closing file
infile1.close();
infile2.close();
return 0;
}
PLEASE REFER BELOW OUTPUT
Customer Information
======================================================================
Chevrolet.... 23 750 1387.5
Zapata.Co.... 53 375 1571.25
Schlumberger. 51 1204 2636.76
FMC.......... 13 675 1181.25
Buick........ 22 475 831.25
Action.Inc... 41 1712 3595.2
IBM.......... 32 979 1811.15
Apple.Comp... 42 790 1698.5
Zapata.Co.... 52 443 1413.17
Univ..Mich... 12 1960 3234
======================================================================
total of all of the customer's weight = 9363
total of all the shipping charges = 19360
Report in alphabetical order by customers name
======================================================================
Action.Inc... 41 1712
Apple.Comp... 42 790
Buick........ 22 475
Chevrolet.... 23 750
FMC.......... 13 675
IBM.......... 32 979
Schlumberger. 51 1204
Univ..Mich... 12 1960
Zapata.Co.... 53 375
Zapata.Co.... 52 443
======================================================================
report sorted by weight
======================================================================
Zapata.Co.... 53 375
Zapata.Co.... 52 443
Buick........ 22 475
FMC.......... 13 675
Chevrolet.... 23 750
Apple.Comp... 42 790
IBM.......... 32 979
Schlumberger. 51 1204
Action.Inc... 41 1712
Univ..Mich... 12 1960
======================================================================
Process returned 0 (0x0) execution time : 0.115 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.