Inventory Counts - C++ Note: Cannot use classes or arrays, only fucntions. You a
ID: 672787 • Letter: I
Question
Inventory Counts - C++
Note: Cannot use classes or arrays, only fucntions.
You are to write a program that will sum up inventory counts and report the total. The inventory is represented by a three digit inventory number and a count of the items. Sample / Test data is given below to test your program, but the real data may be more or less items than shown.
There exists a problem with the data. Two different inventories have been mixed up in the list. The inventory number and the count still match up though. You need to separate out the two inventories and get a count for each one (ie, Inventory One, and Inventory Two). The R&D department has discovered that the two inventories can be distinguished by digits in the inventory number. If the digits in the inventory sum up to be less than or equal to 13, then those items belong to inventory One. Everything else belongs in Inventory Two.
Separate and compute sums for each of the two inventories and generate a total inventory count for each one. Print out each item in the inventory, the total number of items found in each inventory, total counts and the average for each of the two inventories. You have to write to two output files. You are given sample ata below.
The input files for testing your program are in two different files. The first file has the item number. The second file has the counts.
Item File: inventoryItemsNbr.txt // The data for both fies can be found here: http://pastebin.com/4i9JT4sz
Count File: inventoryCount.txt
Restrictions: must use functions where you see fit.
Belows is a sample of each file:
item 382, 123, 965, 432, 823
count 20, 30, 109, 45
Example output for Inventory One
332 20
123 40
235 109
etc....
Example output for Inventory Two
965 109
872 51
etc....
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int segregator(int);
int main()
{
ifstream inItemNbr, inCount;
ofstream outOne, outTwo;
int one, two, flag,totalInventoryOne = 0, totalInventoryTwo = 0,count1 = 0, count2 = 0;
char comma;
inItemNbr.open("inventoryItemsNbr.txt");
inCount.open("inventoryCount.txt");
outOne.open("inventoryOne.txt");
outTwo.open("inventoryTwo.txt");
while(!inItemNbr.eof()) //As long as there are elements in the inventory.
{
inItemNbr >> one; //Read ItemNbr into one.
inItemNbr >> comma; //Remove ,.
inCount >> two; //Read Count into two.
inCount >> comma; //Remove ,.
cout<< one <<" "<< two<<endl; //Print the ItemNbr and Count.
flag = segregator(one); //Identify whether the ItemNbr belongs to inventory 1 or inventory 2.
if(flag) //If it ItemNbr belongs to Inventory 1, at it to that file.
{
outOne << one;
outOne <<" ";
outOne <<two;
outOne <<endl;
totalInventoryOne += two;
count1++;
}
else //Else add it to the other file.
{
outTwo << one;
outTwo << " ";
outTwo <<two;
outTwo <<endl;
totalInventoryTwo += two;
count2++;
}
}
cout<<"Total count in Inventory 1: "<<totalInventoryOne<<endl;
cout<<"Total count in Inventory 2: "<<totalInventoryTwo<<endl;
cout<<"Average count of Inventory 1: "<<totalInventoryOne/(float)count1<<endl;
cout<<"Average count of Inventory 2: "<<totalInventoryTwo/(float)count2<<endl;
}
int segregator(int x)
{
int sum = 0;
while(x != 0)
{
sum += x % 10;
x /= 10;
}
if(sum <= 13)
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.