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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
void printArray (double x[], int n); // function prototype
void readArray (double x [], int &n); // function prototype
void showValues (double x [], int n); // function prototype
void sumValues (double x [], int n); // function prototype
void average (double x [], int n); // function prototype
void valueCount (double x [], int n); // function prototype
void between (double x [], int n); // function prototype
void displayMenu (); // function prototype
int main ()
{
bool processAnotherFile = true;
char response;
bool menuActive;
char menuChoice;
double x[100]; //array for up to 100 numbers
int n; // counts array elts x[0],x[1],...,x[n-1]
while (processAnotherFile)
{
readArray (x, n); // call the readArray function
menuActive = true;
while (menuActive)
{
displayMenu (); // call displayMenu function
cin >> menuChoice; cin.ignore(80,'\n');
cout << endl;
switch (menuChoice)
{
case '1': case 'P': case 'p':
printArray (x, n);
break;
case '2': case 'M': case 'm':
showValues (x, n);
break;
case '3': case 'S': case 's':
sumValues (x , n);
break;
case '4': case 'A': case 'a':
average (x, n);
break;
case '5': case 'C': case 'c':
valueCount (x, n);
break;
case '6': case 'N': case 'n':
between (x, n);
break;
case '7': case 'Q': case 'q':
menuActive = false; // Finish this series of menu choices
break;
default:
cout << " Invalid menu choice: try again!" << endl << endl;
} // end switch statement
}
cout << "Process another file? y/n : ";
cin >> response; cin.ignore(80,'\n');
if (response == 'n' || response == 'N') processAnotherFile = false;
cout << endl << endl;
} // end while (goAgain) loop
return 0;
} // end main function
void readArray (double x [], int &n)
{
int i;
char filename[51];
ifstream inputFile;
cout << "Enter filename (and path, if needed):" << endl;
cin.getline (filename, 51);
cout << endl << endl;
inputFile.open(filename);
i = 0;
while (inputFile.eof() == false)
{
inputFile >> x[i];
if (!inputFile.eof()) d
i++;
}
inputFile.clear();
inputFile.close();
n = i; // n = number of elements in array
}
void printArray (double x[], int n)
{
int i;
int columnCount;
cout << endl << endl;
cout << "Echo of Array Values from Input File:" << endl << endl;
cout << setprecision(2) << fixed << right;
columnCount = 0;
for (i=0; i < n; i++)
{
cout << setw(12) << x[i];
columnCount++;
if (columnCount == 6)
{
columnCount = 0;
cout << endl << endl;
}
}
cout << endl << endl;
cout << "Number of elements in array: " << n << endl << endl;
} // end printarray function
void showValues (double x [], int n)
{
int i;
double maxValue;
double minValue;
int numbers [n];
maxValue = numbers [n];
for (i = 1; i < n; i ++)
{
if ( numbers [i] > maxValue)
maxValue = numbers [n];
cout<< "Max value: "<< maxValue;
cout<< setprecision (2)<< fixed<< right;
cout<< endl;
}
minValue = numbers [0];
for (i = 1; i < n -1; i ++)
{
if ( numbers [i] < minValue)
minValue = numbers [n];
cout<< "MIn value: "<< minValue;
cout<< setprecision (2)<< fixed<< right;
cout<< endl<< endl<< endl;
}
}
void sumValues (double x [], int n)
{
int i;
double sumAbs;
double sum;
sum = 0;
for ( i = 0; i <= n-1; i++)
{
sum = sum + x[i];
cout<<"Sum of values = "<< sum <<endl;
cout<<setprecision(2)<<fixed<<right;
}
sumAbs = 0;
for (i = 0; i <= n-1; i++)
{
if ( i < 0)
{
i = i * -1;
}
sumAbs = sumAbs + x [i];
cout<<"Sum of absolute values = "<< sumAbs <<endl<<endl<<endl;
cout<<setprecision(2)<<fixed<<right;
}
}
void average (double x[], int n)
{
double average;
double deviation;
double total;
int i;
for (i = 0; i <= n - 1; i ++)
{
total += x[i];
average = total / (n -1);
cout<<"Average of values = "<< average << endl;
cout<<setprecision(2)<<fixed<<right;
}
for (i =0; i <= n-1; i ++)
{
deviation = sqrt (((i + (n-1)) * pow (x[i] - average, 2))/ (n - 1));
cout<<"Standard deviation = "<< deviation << endl<<endl<<endl;
cout<<setprecision(2)<<fixed<<right;
}
}
void valueCount (double x[], int n)
{
int i;
int posCount = 0;
int negCount = 0;
int zeroCount = 0;
int tenCount = 0;
for (i = 0; i <= n - 1; i++)
{
if ( n > 0) posCount ++;
if ( n < 0) negCount ++;
if ( n = 0) zeroCount ++;
if ( n <= 10 || n >= -10) tenCount ++;
cout<<"Count of positive elements = "<<posCount<<endl;
cout<<"Count of negative elements = "<<negCount<<endl;
cout<<"Count of zero elements = "<<zeroCount<<endl;
cout<<"Count of abs. values < = 10 = "<<tenCount<<endl<<endl<<endl;
}
}
void between (double x[], int n)
{
double highNumber;
double lowNumber;
int i;
cout<<"Enter a high number: ";
cin>> highNumber;
cout<<endl;
cout<<"Enter a low number: ";
cin>> lowNumber;
cout<<endl<<endl<<endl;
for ( i = 0; i <= n-1; i ++)
{
if ( i <= highNumber || i >= lowNumber)
cout<<" "<< i <<endl<<endl;
}
}
void displayMenu ()
{
cout << " *************************************************" << endl;
cout << " * *" << endl;
cout << " * M A I N M E N U *" << endl;
cout << " * *" << endl;
cout << " * 1) (P)rint Array *" << endl;
cout << " * 2) (M)ax and min values *" << endl;
cout << " * 3) (S)um of values and absolute values *" << endl;
cout << " * 4) (A)verage and standard deviation *" << endl;
cout << " * 5) (C)ount of number of positive, negative, *" << endl;
cout << " * zero, and values < = |10| *" << endl;
cout << " * 6) (N)umbers within user's threshold *" << endl;
cout << " * 7) (Q)uit: Finish processing this array *" << endl;
cout << " * *" << endl;
cout << " *************************************************" << endl;
cout << " Enter choice: ";
}
|