Pleas don\'t copy from another website I sew all of them thank you The school yo
ID: 3546994 • Letter: P
Question
Pleas don't copy from another website I sew all of them thank you
The school your little cousin belongs to is selling low-fat cookies. If your cousin's class sells more cookies than any other class, the teacher has promised to take the whole class on a picnic. Of course, your cousin volunteered you to keep track of all the sales and determine the winner.
Each class has an identification number. Each sales slip has the class identification number and the number of boxes sold. Sales slips are turned in when cookies are sold, and continue til the end of the season. The information is recorded in a data file with one sales record per line. The class identification numbers are a four digit number in the range from 0001 through 9999. Here is a sample of the data:
Class ID Boxes Sold
-------------------------------------------
3013 8 2045 13 2045 7 1600 25 2045 6 3013 18 ...
this is the first line of the data file
Create two 1-D arrays: one to hold the identification numbers and one to record the number of boxes sold. Since the number of classes in the school is 40, the size of both arrays should be set to 40.
The first time an ID number is read, store it in the next free slot in the array of identification numbers and initialize the corresponding position in the array of boxes sold to the number sold on the sales slip.1 Each subsequent time the same ID number is read, add the number of boxes sold to the corresponding position in the array of boxes sold. When there are no more sales slips, scan the array of boxes sold for the largest value. The identification number in the corresponding position in the array of identification numbers is the class that wins.
Copy the data file cookie.dat using the following command: cp ~spp2k/data/cookie.dat cookie.dat
Your program should prompt for data file name, and read from that data file. It should output in a table format the number of boxes sold by each class, as well as a message indicating which class is the winner. Here is an example output of the program:
1 If you do not write your program according to this scheme, 20 points will be deducted.
Final Results for Cookie Sale 2013
=========================
Class Number of Boxes Sold 3013 340
2045 1255
1600 534
2438 154
3030 298
...
The winning class is class 2045 with a sale of 1255 boxes of cookies. Congratulations!
Your program should have at least the following user defined functions. Make sure to write a brief description above each user-defined function for program documentation.
? ReadData (reads the sales information from cookie.dat, and performs the appropriate accumulations of number of cookie boxes sold)
? FindWinner (scans the total number of boxes sold by each class, and finds the class that sold the most boxes)
? DisplayResults (prints the number of boxes sold by each class in table format and displays the winning class)
? LinearSearch (if an item is found in the array, returns the subscript of the item in the array; otherwise, return -1)
Explanation / Answer
//you might want to format the output, but:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//prototype
int ReadData(int id[],int boxes[],int size);
int FindWinner(int boxes[],int size);
void DisplayResults(int id[],int boxes[],int size);
int LinearSearch(int arr[],int size,int target);
int main(){
const int size=40;//number of classes in the school is 40
//Create two 1-D arrays: one to hold the identification numbers
int idNumbers[size];
//and one to record the number of boxes sold.
int BoxesSold[size]={0};
int idCount=ReadData(idNumbers,BoxesSold,size);
DisplayResults(idNumbers,BoxesSold,idCount);
cin.ignore();
cin.get();
return 0;
}
int ReadData(int id[],int boxes[],int size){
//(reads the sales information from cookie.dat, and performs the appropriate accumulations of number of cookie boxes sold)
int classCount=0;
int idNum;
int index;
int cookie;
string fileName;
ifstream infile;
cout<<"Enter file name: ";
cin>>fileName;
infile.open(fileName.c_str());
while (!infile.eof()){
infile>>idNum>>cookie;
index=LinearSearch(id,classCount,idNum);
if(index<0){//not found
id[classCount]=idNum;
boxes[classCount]=cookie;
classCount++;
}
else{
infile>>cookie;
boxes[index]+=cookie;
}
}
return classCount;
}
int FindWinner(int boxes[],int size){
//(scans the total number of boxes sold by each class, and finds the class that sold the most boxes)
int winner=0;//index to winner
for(int i=1;i<size;i++){
if(boxes[i]>boxes[winner]){
winner=i;
}
}
return winner;
}
void DisplayResults(int id[],int boxes[],int size){
int winner;
//(prints the number of boxes sold by each class in table format and displays the winning class)
cout<<" Final Results for Cookie Sale 2013"<<endl
<<"========================="<<endl
<<"Class Number of Boxes Sold "<<endl;
for(int i=0;i<size;i++){
cout<<id[i]<<" "<<boxes[i]<<endl;
}
winner=FindWinner(boxes,size);//get winner index
cout<<"The winning class is class "<<id[winner]<<" with a sale of "<<boxes[winner]<<" boxes of cookies. Congratulations!"<<endl;
}
int LinearSearch(int arr[],int size,int target){
//(if an item is found in the array, returns the subscript of the item in the array; otherwise, return -1)
for(int i=0;i<size;i++){
if(arr[i]==target)
return i;
}
return -1;
}