Sort problem
May 23, 2014 at 6:53pm UTC
I am writing this program to sort the data and select a median from the information. It is giving me the error of sort is undefined. What would be the correct way to identify that? Also if the information could be put better from an experienced C++ user I am listening. Thanks for you help.
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
#include <iostream>
#include <vector>
#include <cstdio>
#include <iomanip>
using namespace std;
//Declaring Constants
const int ZERO = 0;
const int ONE_HUNDRED = 100;
int main()
{
int firstExam, secondExam, thirdExam;
int error;
double median;
vector<int > scores;
size_t size = scores.size();
cout << "Dr. DoLittle's Grading Program ..... (by Kirk Kelley)" << endl;
do
{
error = 0;
cout << "Please enter in the score for the first exam: " ;
cin >> firstExam;
if (firstExam < 0 || firstExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> firstExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the second exam: " ;
cin >> secondExam;
if (secondExam < 0 || secondExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> secondExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the third exam: " ;
cin >> thirdExam;
if (thirdExam < 0 || thirdExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> thirdExam;
cin.clear();
}
} while (error);
//declaring variable
int homework;
//initiating loop
while (!cin.eof())
{
homework = EOF;
cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): " ;
cin >> homework;
if (!cin.good())
if (homework == EOF)
break ;
else
{
cout << "\nInvalid Input. Entry must be an integer." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n' );
}
else
{
if (homework >= ZERO && homework <= ONE_HUNDRED)
{
scores.push_back(homework);
}
else
{
cout << "\nInvalid Input. Grade must be between 0-100." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n' );
}
}
}
for (unsigned int i = 0; i < scores.size(); i++)
{
cout << "Scores: " << scores[i] << '\n' ;
sort(scores.begin(), scores.end());
if (scores.size() % 2 == 0)
{
median = (scores[size / 2 - 1] + scores[size / 2]) / 2.0;
}
else
{
median = scores[size / 2 - 1];
}
}
cout << fixed << setprecision(2);
cout << "Median: " << median << endl;
cout << "First exam score: " << firstExam << endl;
cout << "First exam score: " << secondExam << endl;
cout << "First exam score: " << thirdExam << endl;
system("Pause" );
return 0;
}
May 23, 2014 at 7:02pm UTC
#include <algorithm>
May 23, 2014 at 7:05pm UTC
That did work to declare the sort, but now I have a break. at the if else loop from 98 to 105. Wondering how to code it more effectively?
May 23, 2014 at 7:17pm UTC
First of all, move sort and median calculation out of your loop.
Second. If you need to find median, sort is not nesesarry. nth_element is enough.
May 23, 2014 at 7:25pm UTC
MiiNiPaa I am not sure what to put in the middle argument for nth_element, what should be put after scores.begin()? and also is the format outside the loop in the right spot?
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
#include <iostream>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <algorithm>
using namespace std;
//Declaring Constants
const int ZERO = 0;
const int ONE_HUNDRED = 100;
int main()
{
int firstExam, secondExam, thirdExam;
int error;
double median;
vector<int > scores;
size_t size = scores.size();
cout << "Dr. DoLittle's Grading Program ..... (by Kirk Kelley)" << endl;
do
{
error = 0;
cout << "Please enter in the score for the first exam: " ;
cin >> firstExam;
if (firstExam < 0 || firstExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> firstExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the second exam: " ;
cin >> secondExam;
if (secondExam < 0 || secondExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> secondExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the third exam: " ;
cin >> thirdExam;
if (thirdExam < 0 || thirdExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> thirdExam;
cin.clear();
}
} while (error);
//declaring variable
int homework;
//initiating loop
while (!cin.eof())
{
homework = EOF;
cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): " ;
cin >> homework;
if (!cin.good())
if (homework == EOF)
break ;
else
{
cout << "\nInvalid Input. Entry must be an integer." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n' );
}
else
{
if (homework >= ZERO && homework <= ONE_HUNDRED)
{
scores.push_back(homework);
}
else
{
cout << "\nInvalid Input. Grade must be between 0-100." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n' );
}
}
}
for (unsigned int i = 0; i < scores.size(); i++)
{
cout << "Scores: " << scores[i] << '\n' ;
}
nth_element(scores.begin(), scores.begin()+5, scores.end());
if (scores.size() % 2 == 0)
{
median = (scores[size / 2 - 1] + scores[size / 2]) / 2.0;
}
else
{
median = scores[size / 2 - 1];
}
cout << fixed << setprecision(2);
cout << "Median: " << median << endl;
cout << "First exam score: " << firstExam << endl;
cout << "First exam score: " << secondExam << endl;
cout << "First exam score: " << thirdExam << endl;
system("Pause" );
return 0;
}
May 23, 2014 at 7:32pm UTC
on line 18 size_t size = scores.size();
you calculate the size of the vector there is nothing in the vector at this point so size = 0;
on lines 102 and 106 you use size to calculate a vector subscript.
size / 2 - 1
size is 0 so the result will always be -1 which is not a valid subscript
Topic archived. No new replies allowed.