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
|
// -------------------------------------------------------------------------
// File name: intarrayfunctions.cpp
// Assign ID: PROG11
// Purpose: Functions supporting arrays.
// Author: bfields Byron Fields
// ------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// ------------------------------------------------------------------------
// Function: Read exactly N integer values into array from keyboard.
// ------------------------------------------------------------------------
void LoadArray(int A[], int N, string Prompt)
{
int k;
for(k=0;k<N;k++)
{
cout << Prompt;
cin >> A[k];
}
cout << endl;
}
// ------------------------------------------------------------------------
// Function: Read up to CAP integer values into array from keyboard,
// stopping when CAP values have been read or when the sentinel
// value is read.
// ------------------------------------------------------------------------
void LoadArray(int A[], int CAP, int & N, int Sentinel, string Prompt)
{
int k;
do
{
cout << Prompt;
cin >> A[k];
k++;
N = k;
} while(A[k-1] != Sentinel && k != CAP );
}
// ------------------------------------------------------------------------
// Function: Read a user-specified number of integer values into array from
// keyboard, where the number of integer values can not exceed CAP.
//
// NOTE: Input validation is performed to ensure that the specified
// number of values is in the range 0-CAP.
// ------------------------------------------------------------------------
void LoadArray(int A[], int CAP, int & N, string Prompt)
{
cout << "Enter number of values (between 0 and" << CAP << "): ";
cin >> N;
int k;
if(N>0 && N < CAP)
{
for(k=0;k<N;k++)
{
cout << Prompt;
cin >> A[k];
}
cout << endl;
}
else
{
cout << "Input does not fall within range 0 - CAP " << endl;
}
cout << endl;
}
// ------------------------------------------------------------------------
// Function: Read the M integer values in array from an input stream.
// ------------------------------------------------------------------------
void ReadArray(int A[], int M, istream & inF)
{
int k;
for(k=0;k<=M;k++)
{
inF >> A[k];
}
}
// ------------------------------------------------------------------------
// Function: Display the N integer values in array to the console/screen,
// ALL ON ONE LINE.
// ------------------------------------------------------------------------
void DisplayArray(int A[], int N, string OutputLabel)
{
cout << OutputLabel;
int k;
for(k=0;k<N;k++)
{
cout << A[k] << ' ';
}
cout << endl;
}
// ------------------------------------------------------------------------
// Function: Write the M integer values in array to an output stream
// ALL OM ONE LINE.
// ------------------------------------------------------------------------
void WriteArray(int A[], int M, ostream & outF)
{
int k;
for(k=0;k<M;k++)
{
outF << A[k] << ' ';
}
outF << endl;
}
// ------------------------------------------------------------------------
// Function: Return the sum of the M integer values in array.
// ------------------------------------------------------------------------
int ArraySum(int B[], int M)
{
int sum = 0;
int k;
for(k=0;k<M;k++)
{
sum += B[k];
}
return sum;
}
// ------------------------------------------------------------------------
// Function: Return the average of the M integer values in array.
// ------------------------------------------------------------------------
float ArrayAvg(int B[], int M)
{
int sum = 0;
int k;
for(k=0;k<M;k++)
{
sum += B[k];
}
float avg = 0;
avg = sum/M;
return avg;
}
// ------------------------------------------------------------------------
// Function: Return the max sum of the M integer values in array.
// ------------------------------------------------------------------------
int ArrayMax(int B[], int M)
{
int k = 0;
int max = 0;
do
{
if(B[k] > max)
max = B[k];
k++;
} while(B[k-1]< M);
return max;
}
|