How can I get these two programs below to run as one single program in C++ Sort
ID: 3731696 • Letter: H
Question
How can I get these two programs below to run as one single program in C++ Sort of a tricky question but the code is below
Child Process
#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<<" Graph is displayed below:"<<endl;
for(int n=0;n<100;n+=20)
{
cout<<n<<"-"<<n+20<<" ";
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;
}
}
}
}
Parent Process
#include <iostream>
// #include <algorithm>
using namespace std;
int main()
{
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 << " Graph 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;
}
}
}
Explanation / Answer
For the Parent and child process to be written as a single code, you have to call these functions from the parent process. It will work right because in the function signature grades is a pointer to an integer and you are calling the functions with the address of the grades array as an argument.
SO for the parent and child process to work as a single process just eliminate the child process and call the above mentioned functions from the parent process. Now its just a single code.
second method: Using pthreads
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.