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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
void GetData(int ages[], int capacity, int& n)
// Given: access to an array and its capacity
// Action: get input from keyaboard for each cell and put the final number of cells in n
{
// cout << "@" << __FUNCTION__ << "@" << endl;
int i;
for(i=0; cin && i<capacity; i++) {
cout << "Enter an age or CTRL-Z when done: ";
cin >> ages[i];
} // for
cin.clear();
n=i-1;
cout << n << " records were read. " << endl;
return;
} // GetData
void Initialize(int x[],int n,int initValue)
// Given: access to an array of n values
// Action: initialize each of the n cells in the array to 0
{
// cout << "@" << __FUNCTION__ << "@" << endl;
int i;
for(i=0; i<n; i++) {
x[i]=initValue;
} // for
return;
} // Initialize
void Random(int x[], int low, int high, int n)
// Given: access to an array of n values
// Action: initialize each of the n cells in the array to a random value between low and high
{
// cout << "@" << __FUNCTION__ << "@" << endl;
int i;
for(i=0; i<n; i++) {
x[i]=rand(low,high);
} // for
return;
} // Random
void Display(const int x[], int n)
// Given: access to an array of n values
// Action: display each cell with its index
{
// cout << "@" << __FUNCTION__ << "@" << endl;
int i;
for(i=0; i<n; i++) {
cout << "Cell " << setw(3) << i << ":";
cout << setw(5) << x[i] << endl;
} // for
return;
} // Display
int Sum(const int x[], int n)
// Given: access to an array of n values
// Return: the sum of the first n values of x
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=0;
return result;
} // Sum
double Average(const int x[], int n)
// Given: access to an array of n values
// Return: the average of the first n values of x or 0.0 if n is 0
{
cout << "@" << __FUNCTION__ << "@" << endl;
double result=0.0;
return result;
} // Average
int Min(const int x[], int n)
// Given: access to an array of n values
// Return: the smallest value of x
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=0;
return result;
} // Min
int Max(const int x[], int n)
// Given: access to an array of n values
// Return: the largest value of x
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=0;
return result;
} // Max
int IndexOfMin(const int x[], int n)
// Given: access to an array of n values
// Return: the index of the smallest value of x
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=0;
return result;
} // IndexOfMin
int IndexOfMax(const int x[], int n)
// Written by: <your name>
// Given: access to an array of n values
// Return: the index of the largest value of x
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=0;
return result;
} // IndexOfMax
int LinearSearch(int key, const int x[], int n)
// Given: access to an array of n values
// Return: the index of the cell that matches key
{
cout << "@" << __FUNCTION__ << "@" << endl;
int result=-1;
return result;
} // LinearSearch
int BinarySearch(int key, const int x[], int first, int last)
// Given: a value to search for (the key)
// the array to search (x)
// the index of the first cell to consider
// the index of the last cell to consider
// Returns: the index of the cell whose value matches key
// or -1 if there is no match
{
// cout << “@” << __FUNCTION__ << “@” << endl;
int result=-1;
int middle=(first+last)/2;
if(first>last) {
result=-1;
} else if(key<x[middle]) { // if key is in x it must be in first part
result=BinarySearch(key,x,first,middle-1);
} else if(key==x[middle]) { // found it! return its index!
result=middle;
} else { // key must be > x[middle] so it must be in the last part
result=BinarySearch(key,x,middle+1,last);
} // if
return result;
} // BinarySearch
void Exchange(int& x, int& y)
// Given: access to two arguments, x and y
// Action: exchanges the contents of x and y
// Example: if x is 5 and y is 3, Exchange(x,y) will make x 3 and y 5
{
// cout << “@” << __FUNCTION__ << “@” << endl;
int temp=x;
x=y;
y=temp;
return;
} // Exchange
void BubbleSort(int key[], int n)
// Given access to the array and the number of values to sort
// Action: the values in the array are arranged in ascending order
{
// cout << “@” << __FUNCTION__ << “@” << endl;
int i, pass;
bool needsSorting=true;
for(pass=1; pass<n; pass++) {
for(i=0; i<n-1; i++) {
if(key[i]>key[i+1]) {
Exchange(key[i],key[i+1]);
} // if
} // for
} // for
return;
} // BubbleSort
#endif // last line
|