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
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void getGrades(vector<int>&);
void copyVector(vector<int>, vector<int>&);
void deleteDuplicates(vector<int>&);
void getCount(vector<int>, vector<int>, vector<int>&);
void vectorToDArray(int [], int, vector<int>);
void printArray(int [], int [], int);
typedef int* IntPtr;
int main()
{
vector<int> grades, gradesCopy, uniqueCount;
getGrades(grades);
sort(grades.begin(), grades.end()); //#include<algorithm>
copyVector(grades, gradesCopy);
deleteDuplicates(grades);
getCount(grades, gradesCopy, uniqueCount);
int size = uniqueCount.size();
IntPtr gradeArray, countArray;
gradeArray = new int [size];
countArray = new int [size];
vectorToDArray(gradeArray, size, grades);
vectorToDArray(countArray, size, uniqueCount);
printArray(gradeArray, countArray, size);
delete [] gradeArray;
delete [] countArray;
return 0;
}
//will prompt the user in a do-while loop to get the grades and will end when -1 is entered
//push_back will be used to store the grades into a created vector
void getGrades(vector<int>& grades) {
int grade;
cout << "**************************************************************************************" << endl;
cout << endl;
cout << " WELCOME TO THE HISTOGRAM OF STUDENT GRADES MAKER THINGY" << endl;
cout << endl;
cout << "**************************************************************************************" << endl;
cout << endl;
cout << "Please enter the student's grades. When you have entered a single grade, press enter. " << endl;
cout << "When finished entering all grades, type in '-1' and then press enter. " << endl;
cout << endl;
do {
cin >> grade;
if (grade != -1) {
grades.push_back(grade);
}
} while (grade != -1);
}
//gets the grades with the original vectors and will use push_back
//to be put and copied into another vector
void copyVector(vector<int> grades, vector<int>& gradesCopy) {
for (int i = 0; i < grades.size(); i++) {
gradesCopy.push_back(grades[i]);
}
}
//deletes the duplicates in the first vector
//needed to compare to second vector
void deleteDuplicates(vector<int>& grades) {
for (int i = 0; i < grades.size(); i++) {
for (int j = i + 1; j < grades.size(); j++) {
if (grades[i] == grades[j]) {
grades.erase(grades.begin() + j);
}
}
}
}
//compares the two vectors and when one equals the other the count goes up
//count is then pushed back to another vector to hold the count for each number
void getCount(vector<int> g1, vector<int> g2, vector<int>& c) {
int count = 0;
for (int i = 0; i < g1.size(); i++) {
for (int j = 0; j < g2.size(); j++) {
if (g1[i] == g2[j]) {
count++;
}
}
c.push_back(count);
count -= count;
}
}
void vectorToDArray(int a[], int size, vector<int> v) {
for (int i = 0; i < size; i++) {
a[i] = v[i];
}
}
void printArray(int a[], int b[], int size) {
for (int i = 0; i < (size - 1); i++) {
cout << "Number of " << a[i] << "'s : " << b[i] << endl;
}
}
|