Hi all, thanks for the help last time but I have another problem now. I need to input category ranges. Basically I want to ask the user to input a number between 1 - 5 and depending on what they choose they get x ranges - X being the inputted number. I.E: 1 = 1-100, 2 = 1-49 & 50-100, 3 = 1-33, 34-66 & 67-100 and so on.
This goes beyond what I have been taught and my professor has stated that we won't learn to do this until late into my second year and this is an optional extra but it guarantees a large amount of marks on the coursework.
The histogram at the bottom should also change (the parts with the "*") depending on the number of ranges chosen.
Here is my code so far: (I started using an array - the "ranges[5]" bit to "mark_sum" bit - but still having trouble understanding what I need to do.)
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
|
#include <iostream> //Start of code
using namespace std;
int MarkValueInput; //Mark entered by user
//Counter variables for ranges in While Loop
int counterlow; //Counter for low range
int countermidlow; //Counter for mid-low range
int countermidhigh; //Counter for mid-high range
int counterhigh; //Counter for high range
//Counter variables in For Loop to print stars
int starcounterlow; //Star Counter for low range
int starcountermidlow; //Star Counter for mid-low range
int starcountermidhigh; //Star Counter for mid-high range
int starcounterhigh; //Star Counter for high range
//Counter for each student starting at 1
int students = 1;
//total students
int totalstudents;
//Counters for extras part 1
double avgmark; //Average marks
int num_pass_students; //Number of students passed
int num_fail_students; //Number of students failed
int highmark; //Highest mark
int lowmark; //Lowest mark
double mark_sum; //Total marks
int main () {
int ranges[5];
int categoriesCount;
cout << "Please enter number from 1-5 to choose category ranges " << endl;
cout << "Enter 0 for default value" << endl;
cin >> categoriesCount;
if (categoriesCount == 0) {
cout << "Ranges are 1-29, 30-39, 40-69 and 70-100" << endl;
}
else if (categoriesCount == 1) {
cout << "Ranges are 1 - 100" << endl;
}
else if (categoriesCount == 2) {
cout << "Ranges are 1-50 and 51-100" << endl;
}
else if (categoriesCount == 3) {
cout << "Ranges are 1-33, 34-66 and 67-100" << endl;
}
else if (categoriesCount == 4) {
cout << "Ranges are 1-25, 26-50, 51-75 and 76-100" << endl;
}
else if (categoriesCount == 5) {
cout << "Ranges are 1-20, 21-40, 41-60, 61-80 and 81-100" << endl;
}
mark_sum = 0; //Total marks initialised as "0"
highmark = 0; //Highest mark initialised as "0" to make it easier to find the highest mark
lowmark = 100; //Lowest mark initialised as "100" to make it easier to find the lowest mark
cout << "Enter the student results below:" << endl;
cout << "Please enter mark between 0 - 100 for student " << students << endl;
cout << "To stop, enter value above the maximum of 100" << endl;
cin >> MarkValueInput; //Input mark for student
while (MarkValueInput <=100){ //While Loop begins
mark_sum = mark_sum + MarkValueInput; //Value inputted is added on Total marks and is done each time mark in inputted
if (MarkValueInput > highmark) {
highmark = MarkValueInput; //If mark inputted is greater than highest mark then it becomes the new highest mark and it is done with each input
}
if (MarkValueInput < lowmark) {
lowmark = MarkValueInput; //If mark inputted is smaller than lowest mark then it becomes the new lowest mark and it is done with each input
}
if ((MarkValueInput >=0) && (MarkValueInput <=29)) { //IF block 1 begins
students++; //Increment of students
counterlow++; //Increment of counter
cout << "Please enter mark for student " << students << endl;
cin >> MarkValueInput;
} //IF block 1 ends
else if ((MarkValueInput >=30) && (MarkValueInput <=39)) { //IF block 2 begins
students++;
countermidlow++;
cout << "Please enter mark for student " << students << endl;
cin >> MarkValueInput;
} //IF block 2 ends
else if ((MarkValueInput >=40) && (MarkValueInput <=69)) { //IF block 3 begins
students++;
countermidhigh++;
cout << "Please enter mark for student " << students << endl;
cin >> MarkValueInput;
} //IF block 3 ends
else if ((MarkValueInput >=70) && (MarkValueInput <=100)) { //IF block 4 begins
students++;
counterhigh++;
cout << "Please enter mark for student " << students << endl;
cin >> MarkValueInput;
} //IF block 4 ends
else { //If value is not greater than 100 and is smaller than 0, then the value will not be counted in any of the ranges
cout << "WARNING: NEGATIVE VALUE ENTERED!" << endl;
cout << "Input value between 0 - 100" << endl;
cout << "Please enter mark for student " << students << endl;
cin >> MarkValueInput;
} //ELSE block ends
} //End of While Loop
cout << endl << endl; //For Loop begins
cout << "0-29 " << endl;
for (starcounterlow = 1; starcounterlow <= counterlow; starcounterlow++) {cout << "* ";} //Number of asterisks needed for low range which is equal to counterlow
cout << endl << endl;
cout << "30-39 " << endl;
for (starcountermidlow = 1; starcountermidlow <= countermidlow; starcountermidlow++) {cout << "* ";} //Number of asterisks needed for mid-low range which is equal to countermidlow
cout << endl << endl;
cout << "40-69 " << endl;
for (starcountermidhigh = 1; starcountermidhigh <= countermidhigh; starcountermidhigh++) {cout << "* ";} //Number of asterisks needed for mid-high range which is equal to countermidhigh
cout << endl << endl;
cout << "70-100 " << endl;
for (starcounterhigh = 1; starcounterhigh <= counterhigh; starcounterhigh++) {cout << "* ";} //Number of asterisks needed for high range which is equal to counterhigh
cout << endl;
totalstudents = counterlow + countermidlow + countermidhigh + counterhigh; //Total students is equal to the sum of the range counters.
cout << endl << endl;
cout << "Total student(s) counted:" << totalstudents << endl; //End of For Loop
cout << endl;
cout << "Highest Mark = " << highmark << endl; //Output for highest mark
cout << endl;
cout << "Lowest Mark = " << lowmark << endl; //Output for lowest mark
cout << endl;
avgmark = mark_sum / totalstudents; //Average mark is found by total marks divided by total students
cout << "Average Mark = " << avgmark << endl << endl; //Output for average mark
num_pass_students = countermidhigh + counterhigh; //Number of students passed is found by summing mid-high and high counters
num_fail_students = counterlow + countermidlow; //Number of students failed is found by summing mid-low and low counters
cout << "Number of passed student(s) = " << num_pass_students << endl << endl; //Number of students passed is outputted here
cout << "Number of failed student(s) = " << num_fail_students << endl << endl; //Number of students failed is outputted here
int max = counterlow;
if (countermidlow > counterlow) max = countermidlow;
if (countermidhigh > counterlow) max = countermidhigh;
if (counterhigh > counterlow) max = counterhigh;
cout << " 0-29 30-39 40-69 70-100 " << endl;
for (int i = 1; i <= max; i++)
{
if (i <= counterlow)
cout << " * ";
else
cout << " ";
if (i <= countermidlow)
cout << " * ";
else
cout << " ";
if (i <= countermidhigh)
cout << " * ";
else
cout << " ";
if (i <= counterhigh)
cout << " * ";
else
cout << " ";
cout << endl;
}
} //End of code
|
What I essentially want is for the categories to declared depending on the input and the ranges on the histogram to change to those declared and display accordingly.
An explanation would be helpful as well, because I believe we have an exam on the code written and I want to understand it if they ask a question(s) on this part.
I know I used Global variables but I have to do this because the marker is an ass (excuse my language).
Any help or feedback would be much appreciated. Thanks in advance.