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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// function declarations
double getAverage(int* grades, int size);
double getMedian(int* grades, int size);
double getSd(int* grades, int size);
void sortGrades(int* grades, int size);
int main()
{
// Declaring variables
int size, val;
// setting the precision to two decimal places
std::cout << std::setprecision(2) << std::fixed;
// Getting the input entered by the user
cout << "How many Number of Grades For Child Process?? :";
cin >> size;
// Creating array dynamically
int* grades = new int[size];
/* Getting the inputs entered by the user
* and populate those values into array
*/
for (int i = 0; i < size;)
{
while (true)
{
cout << "Enter the grade for each #" << i + 1 << ":";
cin >> val;
if (val < 0 || val>100)
{
cout << "** Invalid.Must be between 0-100. **" << endl;
continue;
}
else
{
grades[i] = val;
i++;
break;
}
}
}
// calling the functions
double avg = getAverage(grades, size);
double median = getMedian(grades, size);
double sd = getSd(grades, size);
sortGrades(grades,size);
//displaying the average , median ,Standard Deviation
cout<<"Child process has completed the average computation"<<endl;
cout <<"Child process can now display the Average! :"<<avg<<endl;
cout<<"Child process has completed the median computation"<<endl;
cout <<"Child process can now display the Median!:" << median << endl;
cout<<"Child process has completed the Standard-deviation computation"<<endl;
cout << "Child process can now display the Standard Deviation :" << sd << endl;
//Displaying the array elements after sorting
cout<<"Child process will now display the sorted Grades After Sorting the Grades :"<<endl;
for(int i=0;i<size;i++)
{
cout<<grades[i]<<" ";
}
cout<<endl;
//Displaying the graph
cout<<"Child process will now Graph"<<endl;
cout<<"\nGraph is displayed below:"<<endl;
for(int n=0;n<100;n+=20)
{
cout<<n<<"-"<<n+20<<"\t";
for(int m=0;m<size;m++)
{
if(grades[m]>=n && grades[m]<n+20)
cout<<"*";
}
cout<<endl;
}
cout<<"Child process has completed graphing"<<endl;
cout<<"Child process has completed all task"<<endl;
return 0;
}
// This function calculates the average of grades
double getAverage(int* grades, int size)
{
double sum = 0.0;
// calculating the sum of res[] array elements
for (int i = 0; i < size; i++)
{
// calculating the sum
sum += grades[i];
}
// calculating the average
double avg = sum / size;
return avg;
}
// This function calculates the median of grades
double getMedian(int* grades, int size)
{
// This Logic will Sort the Array of elements in Ascending order
int temp;
int middle;
double median;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (grades[i] > grades[j])
{
temp = grades[i];
grades[i] = grades[j];
grades[j] = temp;
}
}
}
if (size % 2 == 0)
{
middle = size / 2;
median = (float)(grades[middle - 1] + grades[middle]) / 2.0;
}
else
{
middle = (size + 1) / 2;
median = grades[middle];
}
return median;
}
// This function calculates the standard deviation of grades
double getSd(int* grades, int size)
{
int sum_of_squares = 0, standard_deviation;
double variance;
double avg = getAverage(grades, size);
/* This loop Calculating the sum of
* square of eeach element in the array
*/
for (int i = 0; i < size; i++)
{
/* Calculating the sum of square of
* each element in the array
*/
sum_of_squares += pow((grades[i] - avg), 2);
}
// calculating the variance of an array
variance = ((double)sum_of_squares / (size));
// calculating the standard deviation of an array
standard_deviation = sqrt(variance);
return standard_deviation;
}
void sortGrades(int* grades, int size)
{
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (grades[i] > grades[j])
{
temp = grades[i];
grades[i] = grades[j];
grades[j] = temp;
}
}
}
}
{
int size = 0, min = 0, max = 0, countMin = 0, countMax = 0;
;
cout << "Enter number of Grades : ";
cin >> size;
int grade[size];
// loop through size time and read user input
for (int i = 0; i < size; i++)
{
cout << "Enter #" << (i + 1) << " grade : ";
cin >> grade[i];
if (i == 0) // Initally the user entered inputs are max and min
{
min = grade[i];
max = grade[i];
}
// get the max grade
if (max < grade[i])
max = grade[i];
// get the min grade
if (min > grade[i])
min = grade[i];
}
// loop through each grade in array and no of times the max and min value repeated
for (int i = 0; i < size; i++)
{
if (max == grade[i])
countMax++;
if (min == grade[i])
countMin++;
}
cout<<"Parent process has determinded the largest grade"<<endl;
cout <<"Parent Process can now display Max grade which is: " << max << endl;
cout<<"Parent process has determined the smallest grade"<<endl;
cout << "Parent Process can now display Min grade which is : " << min << endl;
cout << "Parent Process can now display count of time Max grade " << max << " repeated is : " << countMax << endl;
cout << "Parent Process can now display count of time Min grade " << min << " repeated is : " << countMin << endl;
int countGraph[max + 1] = {0};
// create a array and display the graph by count
for (int i = 0; i < size; i++)
{
countGraph[grade[i]] = countGraph[grade[i]] + 1;
}
cout << "\nGraph report : " << endl;
// loop through min to max value of array
for (int i = min; i <= max; i++)
{
// if count value is greater then 0 display graph
if (countGraph[i] > 0)
{
cout << i << " ";
for (int j = 0; j < countGraph[i]; j++)
{
cout << "*";
}
cout << endl;
}
}
}
|