CS Parallel Arrays 1. Use the StoreData.txt file inside this project folder...ea
ID: 3763035 • Letter: C
Question
CS Parallel Arrays
1. Use the StoreData.txt file inside this project folder...each line (called a data RECORD) contains 3 items.
product id store number quantity.
2. Write three C++ functions as described below
Here are a list of variables...
filename - the Windows external name of the text file
newFileName - the Windows external name of the file of extracted records
requestId - the product id number used to extract data
ORDER_VALUE - the quantity value used to extract data, make this a global constant with a value of 500
idArray - the array of product id numbers
storeArray - the array of store numbers
qtyArray - the array of quantities
count - the actual number of cells filled in the arrays
newcount - the number of extracted records written to newFileName
Your program will be inputting records of data from a disk file where each record has 3 data items.
Assume the following data records with id, storeNo and quantity:
1111 55 50
2222 75 100
3333 95 150
After loading these records
idArray will contain 1111 2222 3333 in positions 0,1 and 2
storeArray will contain 55 75 95 in positions 0,1 and 2
qtyArray will contain 50 100 150 in positions 0,1 and 2
These arrays are in parallel which means that the data in the three positions 0 are related, the data in the three positions 1 are related and
the data in the three positions 2 are related.
Thus i, the common index for these three arrays, tells us which data belongs together. If you know for example that
idArray[2] is an item you want, then you know the data which is related to it is in the 2 position of the other arrays.
HERE ARE THE FUNCTION PROTOTYPES...
bool loadArrays(const char fileName[],long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells)
This boolean function will load the three arrays with data from the text file.
Be certain to include logic/code to ensure that you do not load more cells than you have dimensioned via maxCells.
Return true if you were able to load all of the data, return false otherwise.
The variable count will hold the number of cells your function has loaded.
void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtyArray[], int count)
This function will print to the ostream the data stored in the arrays.
Count is the number of cells/positions in each parallel array
bool extractData(const char newFileName[],int requestId, int baseQty, const long idArray[], const int storeArray[],
const int qtyArray[], int count, int & newcount)
This function will write to the new file only the records whose id number matches requestID BUT ALSO
having a quantity less than baseQty. The variable newcount will hold the number of records actually written.
The return value will be false if the file could not be opened, otherwise the return value will be true.
Inside this project folder is a extractData.txt file sample where the function extracted two records
whose quantity was under 43 and whose request id was 10555...ie:
10555 25 40
10555 20 30
3. Write a main program to test the three functions. Always check to see that a file was successfully opened.
If the file was NOT successfully opened, write an error message and exit the program.
include the two text files in your submission.
________________________________________________________
Text files:
storedata.txt
extractdata.txt
10155 25 40
10155 20 30
Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
#include <string>
using namespace std;
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[],int & count, int maxCells);
void printArrays(ostream & w, const long idArray[], const int storeArray[],const int qtyArray[], int count);
bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[],
const int storeArray[], const int qtyArray[], int count, int & newCount);
void main()
{
const int MAX_CELLS = 100;
char newFileName[100] = "products" ;
char fileName[100] = "output";
long requestId;
const int ORDER_VALUE= 500;
long idArray[MAX_CELLS];
int storeArray[MAX_CELLS];
int qtyArray[MAX_CELLS];
int count=0;
int newCount=0;
bool result;
result = loadArrays(fileName, idArray, storeArray, qtyArray, count, MAX_CELLS) ;
if (result == false)
{
if( count == 0)
{
cerr << "Could not open input file " << fileName << endl;
exit(1);
}
else //Print error message about could not open file, abort
{
cerr << "Could not load all data from the file: " << fileName;
exit(2);
}
}
printArrays(cout, idArray, storeArray, qtyArray, count);
cout << "Please enter a product Id" << endl;
cin >> requestId;
bool check_return;
result = extractData(newFileName, requestId, ORDER_VALUE, idArray,storeArray, qtyArray, count, newCount);
if (result == false)
{
cerr << "Could not open output file: " << newFileName;
exit(3);
}
cout << "There were " << newCount <<" lines extracted." << endl;
}
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[],int & count, int maxCells)
{
bool okay = true;
ifstream in(fileName, ios::out);
if(!in)
okay = false;
else
{
int i = 0;
while (i < maxCells && in >> idArray[i] >> storeArray[i] >> qtyArray [i])
{
i++;
}
count = i;
if ( !in.eof() )
okay = false;
in.close();
}
return okay;
}
void printArrays(ostream & w, const long idArray[], const int storeArray[],const int qtyArray[], int count)
{
for(int i = 0; i <= count; i++)
{
w << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;
}
}
bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[],const int storeArray[], const int qtyArray[], int count, int & newCount)
{
bool okay = true;
ofstream out(newFileName, ios::out);
if(!out)
okay = false;
else
{
int i = 0;
newCount = i;
out.close();
}
return okay;
}