1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_ARRAY_SIZE = 30;
const int NO_DATA = -1;
void ReadData(int idNumbers[], int amountSold[], int& numIds);
void ReadAndUpdateAmountSold(int& SoldAmount);
int FindMaxIndex(const int ary[], int size);
// DO_4: Complete the prototype for function IdIndex()
int main()
{
int idNumbers[MAX_ARRAY_SIZE];
int amountSold[MAX_ARRAY_SIZE];
int numOfIds, maxIndex;
// DO_2: Write a for loop to initialize each element of
// idNumbers to NO_DATA and amountSold to 0.
ReadData(idNumbers, amountSold, numOfIds);
// DO_7: Call function FindMaxIndex to find and return
// the index of the maximum value in amountSold
// storing the value in maxIndex
// DO_8: Complete this output statement to write the
// class room number
cout << "Classroom with most boxes sold was class room #"
<<
cout << '.' << endl;
// DO_9: Complete this output statement to write the
// number of boxes sold in the class room.
cout << "That classroom sold " <<
<< " boxes." << endl;
return 0;
}
// ReadData: Read in the data to update amountSold for each id #
// also updates numIds when a new id is encountered.
// params: (inout, inout, out)
void ReadData(int idNumbers[], int amountSold[], int& numIds)
{
int id; // used to read in id #
numIds = 0;
cin >> id;
while (!cin.eof())
{
int index = IdIndex(idNumbers, numIds, id);
if (index == NO_DATA)
{
idNumbers[numIds] = id;
index = numIds;
numIds++;
}
// DO: 6. Call ReadAndUpdateAmountSold to update the
// array element in amountSold with subscript: index
cin >> id;
}
}
// Function IdIndex() has three parameters:
// ary[]: array of int
// size : int, number of elements in ary[]
// key : int
// Function IdIndex() searchs ary[] for key and returns the index of
// where key appears in ary; returns NO_DATA if not found
// params: (in, in, in)
// Do_3: Complete the header and the body of this linear search
// function.
// DO_5: Complete the body of the function by
// writing a for loop to find the index of
// the largest value in the array.
// DONT FORGET TO RETURN THIS INDEX!
// FindMaxIndex: Returns the index of largest value in the array
// Assumes that there is at least one value in array.
// If two or more elements have the same max value,
// return the first index.
// params: (in, in)
int FindMaxIndex(const int ary[], int size)
{
}
// ReadAndUpdateAmountSold : Reads in an amount and adds this value
// to the amount-sold parameter.
// params: (inout)
void ReadAndUpdateAmountSold(int& SoldAmount)
{
int amount;
cin >> amount; // read in the amount to increment by
SoldAmount += amount; // increment the inout parameter by amount
}
|