Quiz 3: Procedural Programming, Structures, and Classes

Computing

Program Plan: • Include header files • "cin" and "cout" statement • Declare the constant integer value "Number" • Score values and store the num real values • Array has store the "numval" and "Mean" values • Assign letter grade using grading on the curve • Inside the "main" function o Declare the score value and number o Declare the integer variables "cnt" o Declare the "gradeval" number in the character variables o Display the score values o Using for loop to get the array "ascoreval" to the store the letter grade o Get the score value o Using if condition score values is equal to -1 or not o "cnt" value store to score value variables o Increment the "cnt" values o Using if condition "cnt" value is equal to zero o Print the no score values o It has return the zero values o The Mean value store the "cnt" variables o The standard deviation value stored to the "score", "cnt" and mean values o Display the mean and standard deviation o Assign the "gradevals" function using the" score", "cnt", "StdDev" and "gradeval" values. o Using for loop to "cnt" the score values. o Display the score values. • Define the "mean" method and declare the variables "scoreval" and num values o Declare the double values "sumval" and initialize to zero values o Declare the integer variables "n" o Using for loop to print the "num" values o Increment the "sumval" values and store to the scorevalue variables o Display the "sumval" values • Define of "stdDeviation" method and declare the double variables "scoreval", "num" and the "Mean" values. o Declare the double variables "sumval" and initialize zero values o Using for loop to increment the num values o Standard deviation values to store the "sumval" values o Return the "sumval" and "num" values • Define the" assigngradevals" method and declare the variables "scoreval", "num", "Mean" and the "StdDev" method. o Standard deviation value initialize to zero values o All the score values same to the "gradeval" C process o Using for loop to print the" numval" values o Return "gradeval" values o Declare the double values "DF,CD,BC,AB" using mean and standard deviation condition o Using for and if loop to check the condition of the grade elements o If condition "scoreval" is less than DF o Print "F" grade o If condition "scoreval" is less than CD o Print "D" grade o If condition "scoreval" is less than BC o Print "C" grade o If condition "scoreval" is less than AB o Print "B" grade o Otherwise to print A "gradeval" values o Print "A" grade Program: / *********************************************************** Program to perform the letter grade assign to the * * numeric score * ********************************************************** / / / include header files #include #include / / cin and cout statements using namespace std; / / declare the constant integer value "Number" const int Number = 50; / / mean list of score values and store the num real values double mean(double scoreval[], int numval); / / list of standard deviation score values / / Array has store the numval reals and mean values double stdDeviation(double scoreval[], int numval, double Mean); / * It has gradeval letter to store the "scoreval" in a array list and curve process to declare the variables "numval","Mean","StdDev" and "gradevals" * / void assigngradevals(double scoreval[], int numval, double Mean, double StdDev, char gradevals[]); / / main function int main() { / / score value is an array that stores the number double ascoreval, scoreval[Number]; / / declare the integer variables "cnt" int cnt = 0; / / declare the gradeval number in character char gradeval[Number]; / / display the score values cout "please Enter scorevalues -1 to stop the process:"; / / using for loop to get the array element of "ascoreval" for (;;) { / / to get the score value cin ascoreval; / / using if condition score values is equal to -1 or not if (ascoreval == -1) break; / / cnt value store to score value variables scoreval[cnt] = ascoreval; / / increment the cnt values cnt++; } / / using if condition cnt value is equal to zero if (cnt == 0) { / / print the no score values cout "No score values.\n"; / / return zero elements return 0; } / / the Mean value stored to the score "cnt" variables double Mean = mean(scoreval, cnt); / * the standard deviation value stored to the score, cnt and mean values * / double StdDev = stdDeviation(scoreval, cnt, Mean); / / display the mean and standard deviation cout "The mean value is " Mean " and the standard deviation value is " StdDev endl; / * assign the "gradevals" function using the score, coun, stddev and gradeval values * / assigngradevals(scoreval, cnt, Mean, StdDev, gradeval); / / display the score letter gradevals cout " score values and letter gradevals values: \n"; / / using for loop to cnt the score values for (int i = 0; i cnt; i++) / / display the score values cout scoreval[i] " --- " gradeval[i] endl; } / * Define the mean method() and declare the variables "scoreval" and "num" values * / double mean(double scoreval[], int numval) { / * declare the double values "sumval" and initialize to zero values * / double sumval = 0.0; / / declare the integer variables "n" int n; / / using for loop to print the "numval" values for (n = 0; n numval; n++) / * to increment the "sumval" values and store to the "scoreval" variables * / sumval += scoreval[n]; / / display the "sumval" values return sumval / n; } / / Define of "stdDeviation" method and declare the double variables "scoreval","num" and the "Mean" values() double stdDeviation(double scoreval[], int numval, double Mean) { / * declare the double variables "sumval" and initialize zero values * / double sumval = 0.0; / / using for loop to increment the "numval" values for (int n = 0; n numval; n++) / * standard deviation values to store the "sumval" values * / sumval += (scoreval[n] - Mean)*(scoreval[n] - Mean); / / return the "sumval" and "numval" values return sqrt(sumval / numval); } / / Define the "assigngradevals" method and declare the variables "scoreval","num","Mean" and the "StdDev" method() void assigngradevals(double scoreval[], int numval, double Mean, double StdDev, char gradeval[]) { / / "StdDev" value initialize to zero values if (StdDev == 0) { / / using for loop to print the "numval" values for (int n = 0; n numval; n++) / * all the score values same to the gradeval "C" process * / gradeval[n] = 'C'; / / return "gradeval" values return; } / / else condition / * declare the double values "DF,CD,BC,AB" using mean and standard deviation condition * / double DF = Mean - 1.5 *StdDev, CD = Mean - 0.5 *StdDev, BC = Mean + 0.5 *StdDev, AB = Mean + 1.5 *StdDev; / / using for loop to display the "numval" values for (int n = 0; n numval; n++) / / using if condition scoreval is less than "DF" if (scoreval[n] DF) / / store the gradeval "F" variables gradeval[n] = 'F'; / / using if condition "scoreval" is less than "CD" else if (scoreval[n] CD) / / store the gradeval "D" variables gradeval[n] = 'D'; / / using if condition scoreval is less than "BC" else if (scoreval[n] BC) / / store the gradeval "C" variables gradeval[n] = 'C'; / / using if condition scoreval is less than "AB" else if (scoreval[n] AB) / / store the gradeval "B" variables gradeval[n] = 'B'; / / other wise to print "A" gradeval values else / / store the gradeval "A" variables gradeval[n] = 'A'; } Sample Output: Please Enter score values -1 to stop the process: 60 70 80 90 98 -1 The mean value is 79.6 and the standard deviation value is 13.5882 Score values and letter grade values: 60 --- D 70 --- D 80 --- C 90 --- B 98 --- B

Program Plan: • Include header files. • Declare the constant integer variables "cval". • Type definition variables "cval ". • Declare the constant integer variables "numb" it has 1 through 150 mailboxes. • Inside the main function, o Declare the Boolean array of open mailboxes initialize to false values o Using for loop to carried out the post office mailboxes o Check the condition mailboxes is opened or closed o Print the result mailboxes o Using for loop to open the even mailboxes o Display the open mailboxes o Open the door for all mailboxes using if condition "m % 60 == 0" o Print the number of closed mailboxes o using if loop check the condition not equal to open mailboxes are not o Finally display the open mailboxes. Program: / *********************************************************** Program to perform the results of Peter Postman problem * ********************************************************** / / / include header #include / / cin and cout statement using namespace std; / / declare the constant integer variables cval const int cval = 200; / / declare the type definition of boolean array cval typedef bool barray[cval]; / / declare the constant interger varibale numb 1 through 150 const int numb = 151; / / main function int main() { / * boolean array of open mailbox has declared to false values * / barray omail = {false}; / * using for loop to carried out the post office of mailboxes * / for (int m = 2; m = numb; m++) for (int n = m; n = numb; n += m) / / check the condition mailbox is opened or closed omail[n] = !omail[n]; / / display the result mailboxes cout "mailboxes (0 = open, 1 = closed):\n"; / / using for loop to open the even mailboxes for (int m = 1; m = numb; m++) { / / display the open mailboxes values cout omail[m]; / / open the door it has all even mailboxes if (m % 60 == 0) cout endl; } cout endl; / / display the number of closed mailboxes cout "\n closed mailboxes are:\n"; / / using for loop to close the mailboxes for (int m = 1; m = numb; m++) / * using if loop check the condition not equal to open mailboxes are not * / if (!omail[m]) / / display the open mailboxes cout m " "; cout endl; } Sample Output: mailboxes (0 = open, 1 = closed): 011011110111111011111111011111111110111111111111011111111111 111011111111111111110111111111111111111011111111111111111111 0111111111111111111111101111111 closed mailboxes are: 1 4 9 16 25 36 49 64 81 100 121 144

Program Plan: • Include header files • "cin" and "cout" statement • Declare the constant integer variables "CA" • Declare the type definition of Boolean values "CA" • Inside main function o Declare the integer variables "maximum" o Display the prime numbers o Get the prime number by using the "maximum" variables o Use the binary array sieve method o find all prime number by using the sieve method of Eratosthenes o sieve variables set to the true o declare the integer variables "primeval" • Check the condition an integer variable primeval is greater than or equal to 2. o find all prime numbers value is less than some given number. o sieve value set to be false o increment the prime values o check sieve is not equal to prime or not o maximum values in the sieve method when compared to the prime number o display the prime values Program: / *********************************************************** Program to perform the Sieve Method of Eratosthenes to * * find all prime numbers * ********************************************************** / / / include header files #include / / cin, cout statement using namespace std; / / declare the constant integer variables "CA" const int CA = 1000; / / declare the type definition boolean array "CA" typedef bool BArray[CA]; / / main function int main() { / / declare the integer variables "maximum" int maximum; / / display the prime number cout "\nplease generate primes number : "; / / get the prime number using "maximum" variables cin maximum; / / binary array use the sieve method BArray sieve; / * find all prime number by using the sieve method of Eratosthenes * / for (int i = 2; i = maximum; i++) / / sieve variables set to the true sieve[i] = true; / / declare the integer variables "primeval" int primeval = 2; / * using while loop an integer variable primeval is greater than or equal to 2 * / while (primeval * primeval maximum) { / * to find all prime numbers value is less than some given number * / for (int i = primeval * 2; i = maximum; i += primeval) / / sieve variables set to the false sieve[i] = false; / / loop to check for prime number do / / increment the prime value primeval++; / * using while check sieve is not equal to prime or not * / while (!sieve[primeval]); } / / print the prime values cout "\nPrimes:"; / / to find the prime values for (int i = 2; i = maximum; i++) / / if condition check the sieve of maximum values if (sieve[i]) / / display the prime values cout ' ' i; cout endl; } Sample Output: Please generate primes number: 30 Primes: 2 3 5 7 11 13 17 19 23 29