In: Computer Science
I know this qu is posted but I have not got the answer BY unsorted list using STLl!!
1.(70) An organization
that 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.
Input (Note: if you use keyboard for the input, it is ok for this
assignment) Here is a sample of the data. (The classes are numbered
from 1 through 10.)
Id. Number Boxes Sold
3 23
4 1
2 13
2 7
4 5
1 6
10 16
Output The following information written on file "boxes", all
properly labeled. The total number of boxes sold by each class. The
identification number of the winning class. If there is a tie, list
all winners.
Data Structures: using class UnsortedType defined in the textbook
(chapter 3). The interface is provided as follows or you can
include from the STL library. You can use either array or
LinkedList as the fundamental data structure. (you need to justify
your decision.)
Deliverables
Part I - Your design (objected-oriented design). (use diagrams, or
pseudo-code, or CRC card to show your logical level design)
Part II - A listing of your program (implementation of the program
in C++) - A listing of your test plan as input to the program - A
listing of the output file
Interface of UnsortedType class:
bool IsFull() const; // Function: Determines whether list is full.
// Pre: List has been initialized.
// Post: Function value = (list is full)
int LengthIs() const; // Function: Determines the number of
elements in list. // Pre: List has been initialized. // Post:
Function value = number of elements in list
void RetrieveItem(ItemType& item, bool& found); //
Function: Retrieves list element whose key matches item's // key
(if present). // Pre: List has been initialized. // Key member of
item is initialized. // Post: If there is an element someItem whose
key matches // item's key, then found = true and item is a copy of
// someItem; otherwise found = false and item is unchanged. // List
is unchanged.
void InsertItem(ItemType item); // Function: Adds item to list. //
Pre: List has been initialized. // List is not full. // item is not
in list. // Post: item is in list.
void DeleteItem(ItemType item); // Function: Deletes the element
whose key matches item's key. // Pre: List has been initialized. //
Key member of item is initialized. // One and only one element in
list has a key matching // item's key. // Post: No element in list
has a key matching item's key.
void ResetList(); // Function: Initializes current position for an
iteration // through the list. // Pre: List has been initialized.
// Post: Current position is prior to list.
void GetNextItem(ItemType& item); // Function: Gets the next
element in list. // Pre: List has been initialized and has not been
changed since // last call. // Current position is defined. //
Element at current position is not last in list. // Post: Current
position is updated to next position. // item is a copy of element
at current position.
I will use Array as my data structure as number of classes is fix that is 10 so need to use Linked List.
I am typing the code as well attaching the screenshots. Please refer to the comments to understand the code.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Enter id and boxes:" << endl;
int id;
int boxes;
int classes[10] = {0}; // 10 classes initially all have sold 0
boxes;
// take id and number of boxes sold as input
// Type any alphabet or character other than integer to stop taking
input
// count boxes sold by each class and store in the array
while (cin >> id)
{
if(id <= 10){
cin >> boxes;
classes[id-1] += boxes; // id -1 as index starts from 0 while class
id starts from 1
}else{
cout << "Invalid" << endl; // if id > 10
}
}
// print class id and total boxes sold by them
cout << "id boxes"<< endl;
for(int i = 0; i < 10; i++){
cout << i+1 << " ";
cout << classes[i]<<endl;
}
// finding the maximum boxes sold by any class
int max = 0;
for(int i = 0; i < 10; i++){
if(classes[i] > max){
max = classes[i];
}
}
// printing the name of winners
cout << "winners:" << endl;
for(int i = 0; i < 10; i++){
if(classes[i] == max){
cout << i+1 << endl;
}
}
return 0;
}
Input:
3 23
4 1
2 13
2 7
4 5
1 6
10 16
STOP
Output:
Enter id and boxes: id boxes 1 6 2 20 3 23 4 6 5 0 6 0 7 0 8 0 9 0 10 16 winners 3