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
|
the largest, smallest and the range of the values.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 50;
void readthearrays (int score1[], int score2[], int score3[], int k);
void printarray (int nums[], int k);
void makenewarray (int score1[], int score2[], int score3[], int sumscores[] , int k);
void findlargesmallrange (int numb[], int n, int &, int &, int &);
void sort1array (int numb[], int n);
void printlargestandsmallest (int numb[], int n);
ifstream infile ("input.txt");
ofstream outfile ("output.txt");
int main()
{
int score1[SIZE], score2[SIZE], score3[SIZE], sumscores[SIZE];
int largest, smallest, howfarapart, large, small;
for (int n = 17; n < 18; n++) {
outfile << "n is " << n << endl;
outfile << "here is the original data:" << endl;
outfile << setw(12) <<"array1" << setw(12) << "array2" << setw(12) << "array3" << endl;
readthearrays(score1, score2, score3, n);
outfile << endl;
outfile << "here is the score1 array:" << endl;
printarray(score1, n);
outfile << endl << endl;
outfile << "here is the score2 array:" << endl;
printarray(score2, n);
outfile << endl << endl;
outfile << "here is the score3 array:" << endl;
printarray(score3, n);
outfile << endl << endl;
outfile << "here is the new array:" << endl;
makenewarray(score1, score2, score3, sumscores, n);
printarray(sumscores, n);
outfile << endl << endl;
findlargesmallrange(score1, n, largest, smallest, howfarapart);
outfile<< "in the score1 array:" << endl;
outfile << "largest is " << largest << endl;
outfile << "smallest is " << smallest << endl;
outfile << "they are " << howfarapart;
outfile << " apart"<< endl << endl;
findlargesmallrange(score2, n, largest, smallest, howfarapart);
outfile << "in the score2 array:" << endl;
outfile << "largest is " << largest << endl;
outfile << "smallest is " << smallest << endl;
outfile << "they are " << howfarapart << " apart" << endl << endl;
findlargesmallrange(score3, n, largest, smallest, howfarapart);
outfile << "in the score3 array:" << endl;
outfile << "largest is " << largest<< endl;
outfile << "smallest is " << smallest << endl;
outfile << "they are " << howfarapart << " apart" << endl << endl;
findlargesmallrange(sumscores, n, largest, smallest, howfarapart);
outfile << "in the new array:" << endl;
outfile << "largest is " << largest<< endl;
outfile << "smallest is " << smallest << endl;
outfile << "they are " << howfarapart << " apart" << endl << endl;
sort1array (score1, n);
outfile << "here is the score1 array in sorted array:" << endl;
printarray (score1, n);
outfile << endl << endl;
sort1array (score2, n);
outfile << "here is the score2 array in sorted array:" << endl;
printarray (score2, n);
outfile << endl << endl;
sort1array (score3, n);
outfile << "here is the score3 array in sorted array:" << endl;
printarray (score3, n);
outfile << endl << endl;
sort1array (sumscores, n);
outfile << "here is the sumscores array in sorted array:" << endl;
printarray (sumscores, n);
outfile << endl << endl;
printlargestandsmallest (score1, n);
outfile << "the smallest value in score1 sorted array is: " << small << endl;
outfile << "the largest value in score1 sorted array is: " << large << endl << endl;
printlargestandsmallest (score2, n);
outfile << "the smallest value in score2 sorted array is: " << small << endl;
outfile << "the largest value in score2 sorted array is: " << large << endl << endl;
printlargestandsmallest (score3, n);
outfile << "the smallest value in score3 sorted array is: " << small << endl;
outfile << "the largest value in score3 sorted array is: " << large << endl << endl;
printlargestandsmallest (sumscores, n);
outfile << "the smallest value in sumscores sorted array is: " << small << endl;
outfile << "the largest value in sumscores sorted array is: " << large << endl;
}
outfile.close();
infile.close();
return 0;
}
void readthearrays (int score1[], int score2[], int score3[], int k) {
int j;
for (j = 0; j < k; j++) {
infile >> score1[j];
outfile << setw(10) << score1[j];
infile >> score2[j];
outfile << setw(12) << right << score2[j];
infile >> score3[j];
outfile << setw(12) << right << score3[j] << endl;
}
}
void printarray (int nums[], int k) {
int j;
for (j = 0; j < k; j++)
{
outfile << setw(5) << nums[j];
if (j % 5 == 4)
outfile << endl;
}
}
void makenewarray (int score1[], int score2[], int score3[], int sumscores[], int k) {
for (int j = 0; j < k; j++)
sumscores[j] = score1[j] + score2[j] + score3[j];
}
void findlargesmallrange (int numb[], int n, int &large, int &small, int &apart) {
large = numb[0];
small = numb[0];
for (int j = 0; j < n; j++) {
if (large < numb[j]){
large = numb[j];
}
if (small > numb[j]){
small = numb[j];
}
}
apart = large - small;
}
void sort1array (int numb[], int n) {
int score;
for (int j = 0; j < (n - 1); j++)
for (int test = j + 1; test < n; test++)
if (numb[j] > numb[test]) {
score = numb[j];
numb[j] = numb[test];
numb[test] = score;
}
return;
}
void printlargestandsmallest (int numb[], int n) {
int large, small;
small = numb [0];
large = numb [16];
outfile << small << endl;
outfile << large << endl;
return;
}
|