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
|
#include <iostream>
#include <iomanip>
#include <cctype>
#include <math.h>
#include <fstream>
#include <cstdlib>
using std::setiosflags;
using std::resetiosflags;
using std::setw;
using std::setprecision;
using std::ofstream;
using std::cin;
using std::cout;
using namespace std;
using std::fstream;
using std::ios;
int read (char filename[], char names[][30], int scores[][3], double averages[]);
void write (char filename[], ios::openmode mode, char description[],char names[][30],
int scores[][3], double averages[], int numberOfStudents);
void bubbleSortByName (char names[][30], int scores[][3], double averages[],int numberOfStudents);
void bubbleSortByAverage (char names[][30], int scores[][3], double averages[],int numberOfStudents);
int main()
{
char names [100][30];
int scores [100][ 3];
double averages [100];
cout << ':' << setiosflags(ios::left) << setw(30) << "hello, world!"<< resetiosflags (ios::left) << "\n";
cout << setiosflags (ios::fixed|ios::showpoint)<< setw(7) << setprecision(2) << ':' << 3.1415927 << endl;
int numberOfStudents = read ("dataHw.txt", names, scores, averages);
write ("sortHw.txt", ios::out, "Pre-Sorting",names, scores, averages, numberOfStudents);
write ("sortHw.txt", ios::app, "Sorted by Name",names, scores, averages, numberOfStudents);
bubbleSortByName (names, scores, averages, numberOfStudents);
write ("sortHw.txt", ios::app, "Sorted by Average",names, scores, averages, numberOfStudents);
bubbleSortByAverage (names, scores, averages, numberOfStudents);
return 0;
}
int read(char filename[], char names[][30], int scores[][3], double averages[])
{
// initialize scores & averages to 0
for (int i = 0; i < 10; i++)
{
averages[i] = 0;
for (int j = 0; j < 3; j++)
{
scores[i][j] = 0;
}
}
int sum;
int numberOfStudents = 0; // how many students we will have
int col = 0; // column for names
// read text document
ifstream infile(filename); // define & open
for (int i = 0; i < 10; i++)
{
infile >> names[i]; // get the name
sum = 0; // base finding of the average
if (strcmp(names[i], "zzz") != 0)
{
for (int j = 0; j < 3; j++)
{
infile >> scores[i][j]; // start plugging in scores
sum += scores[i][j]; // while we're at it, get the sum of the scores
}
averages[i] = sum / 3.0; // get the average for this name
numberOfStudents = i;
}
}
infile.close(); // close
return numberOfStudents;
}
void write(char filename[], ios::openmode, char description[], char names[][30], int scores[][3], double averages[], int numberOfStudents)
{
// write to text file
ofstream outfile (filename, ios::out); // define
outfile << description << endl;
cout << description << endl;
for (int i = 0; i < numberOfStudents; i++)
{
outfile << names[i] << ' ' << "\n"; // print the name
outfile << fixed << setw(7) << setprecision(2) << averages[i] << ' '<< "\n"; // then the averages
for (int j = 0; j < 4; j++)
{
outfile << scores[i][j] << ' ' << "\n"; // then the three exam scores
}
outfile << endl; // then go to the next line
}
outfile << endl;
outfile.close();
return;
}
void bubbleSortByName (char names[][30], int scores[][3], double averages[],int numberOfStudents)
{
int i = 0;
int compares = 0;
bool swapOccurred;
char temp[80];
do
{
swapOccurred = false;
for (int j=0;j<=numberOfStudents-2-i;j++)// pair = 0 to numberOfstudents-2
{
compares++;
if (strcmp (names[j],names[j+1]) > 0)
{
strcpy (temp,names[j]);
strcpy (names[j], names[j+1]);
strcpy (names[j+1], temp); // swap
swapOccurred = true;
scores[j] and scores[j+1];
}
} // end for
i++;
}
while (swapOccurred);
return;
}
void bubbleSortByAverage (char names[][30], int scores[][3], double averages[],int numberOfStudents)
{
int compares = 0;
bool swapOccurred;
do
{
swapOccurred = false;
for (int i=0; i <= numberOfStudents-2; i++) // sink = 0 to numberOfExams-2
{
for (int j=0;j<=numberOfStudents-2-i;j++)// pair = 0 to numberOfStudents-2
{
compares++;
if (scores[i][j] < scores[i][j+1])
{
int temp = scores[i][j];
scores[i][j] = scores[i][j+1];
scores[i][j+1] = temp; // swap
swapOccurred = true;
scores[i][j] and scores[i][j+1];
}
} // end for
i++;
}
}
while (swapOccurred);
return;
}
|