# Variations in Programs

Since every program that is created is different from every other program, what are the variations that we look for and how do we control them?

© BrainMass Inc. brainmass.com October 16, 2018, 8:04 pm ad1c9bdddfhttps://brainmass.com/computer-science/cpp/variations-in-programs-136673

#### Solution Preview

Even though every program that is created is different from every other program, there are known programming guidelines we can look for in every program.

General Programming Guidelines:

- Writing Clearly

- Writing Modular Code

- Breaking Complex Equations into Smaller Pieces

- Overloading Functions

- Making it Work, THEN Making it Fast

- Using Parenthesis

- Using Memory de-allocation

- Exception Handling

- Error Catching - assert and verify

- Using Goto Statements

- Using Templates

- Using Standard Template Libraries (STL)

- Using Variables

- Using Public Member Class Variables

- Using Functions

- Using Constant

* Writing Clearly

Just because you can do something in a single line of code doesn't mean you should. Do not write code just for the compiler; write the code for you and your fellow programmers. It may be you who returns to this code days, months, or years later and can't remember what that complex statement does. Never sacrifice clear code for "efficient" code.

Clearly written code is 'self' commenting, there should be no need for blocks of comments to describe what is going on. If that isn't the case, consider using more descriptive variable names, and breaking the code up into more distinct modules.

* Writing Modular Code

Code should be broken down into smaller pieces in order to make testing easier and to facilitate re-use of code. Functions that span several pages of printed text are hard to follow, harder to debug and are less likely to be reusable in alternative applications. As a general rule, a function should not span more than 2 pages (or 100 lines). Furthermore, two or more functions can be adapted by others for a wider range of uses than one single function.

* Breaking Complex Equations into Smaller Pieces

Complex equations containing many operators should be broken down into smaller pieces, with suitably name local variables to describe the individual portions of the code. This not only makes the code more understandable (self documented), but it is also easier to analyse the values of specific parts of the equation during debugging.

If only locally defined variables are used then writing code in this way is NOT less efficient - the code will run just as fast. However, it is important to constrain the scope of these local variables, which is achieved when you code in a modular fashion. To further aid the compiler you can encapsulate the locally used variables with { }.

// poor

double num=(A * 2 * cos(w * t)) * sin(k * x) * cosh(k * d) + 2 * B * sin(k * x - w * t);

// better

...

double num;

{

double Amp_A = A * 2 * cos(w * t);

double Wave_A = Amp_A * sin(k * x) * cosh(k * d);

doule Amp_B = B * 2;

Wave_B = Amp_B * sin(k * x - w * t);

num = Wave_A + Wave_B

}

...

* Overloading Functions

Overloading functions can be a powerful tool for creating a family of related functions that only differ in the type of data provided as arguments. If not used properly (such as using functions with the same name for different purposes) they can, however, cause considerable confusion. When overloading functions all variations should have the same semantics (be used for the same purpose).

* Making it Work, THEN Making it Fast

Often during development, developers want to get the most "bang" for their money. If you write code that works, and the interface is clear and correct, you can always go back later and make it faster. Strive to make it correct and readable first and fast second. Fast wrong code is still wrong code.

* Using Parenthesis

It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others - you should not assume that other programmers know precedence as well as you do.

if(a == b && c == d) // avoid

if((a == b) && (c == d)) // good

x >= 0 ? x : -x; // avoid

(x >= 0) ? x : -x; // good

* Using Memory de-allocation

If you know that a pointer variable is ...

#### Solution Summary

The variations among programs and how to control those variations are explained.

Problems on Confidence Intervals, Statistical Test of Hypothesis, ANOVA, Regression and Forecasting

Calculate a 99 percent confidence interval for the mean debt-to-equity ratio. Using the Excel descriptive statistics output given below, find a 95 percent confidence interval for the mean of all possible yields obtained using catalyst XA-100. For each of the following situations, indicate whether an error has occurred and, if so, indicate what kind of error (Type I or Type II) has occurred. For each of the following sample results, determine whether the power plant should be shut down and the cooling system repaired. Define the null and alternate hypotheses using the treatment means M1, M2, and M3 to represent each group. Then test for statistically significant differences between these treatment means. Write the regression equation for the LaborCost (y) and BatchSize (x). What type of seasonal variation do you see in the sales data?

Q1 The bad debt ratio for a financial institution is defined to be the dollar value of loans defaulted divided by the total dollar value of all loans made. Suppose a random sample of seven Ohio banks is selected and that the bad debt ratios (written as percentages) for these banks are 7 percent, 4 percent, 6 percent, 7 percent, 5 percent, 4 percent, and 9 percent. Assuming the bad debt ratios are approximately normally distributed, the MINITAB output of a 95 percent confidence interval for the mean bad debt ratio of all Ohio banks is as follows:

Variable N Mean StDev SE Mean 95.0% CI

d-ratio 7 6.000 1.826 0.690 ( 4.311, 7.689)

a Using the sample mean and standard deviation on the MINITAB output, verify the calculation of the 95 percent confidence interval.

b. Calculate a 99 percent confidence interval for the mean debt-to-equity ratio.

c Banking officials claim the mean bad debt ratio for all banks in the Midwest region is 3.5 percent and that the mean bad debt ratio for Ohio banks is higher. Using the 95 percent confidence interval, can we be 95 percent confident that this claim is true? Using the 99 percent confidence interval, can we be 99 percent confident that this claim is true? Explain.

Q2 A production supervisor at a major chemical company wishes to determine whether a new catalyst, catalyst XA-100, increases the mean hourly yield of a chemical process beyond the current mean hourly yield, which is known to be roughly equal to, but no more than, 750 pounds per hour. To test the new catalyst, five trial runs using catalyst XA-100 are made. The resulting yields for the trial runs (in pounds per hour) are 801, 814, 784, 836, and 820. Assuming that all factors affecting yields of the process have been held as constant as possible during the test runs, it is reasonable to

regard the five yields obtained using the new catalyst as a random sample from the population of all possible yields that would be obtained by using the new catalyst. Furthermore, we will assume that this population is approximately normally distributed.

a Using the Excel descriptive statistics output given below, find a 95 percent confidence interval for the mean of all possible yields obtained using catalyst XA-100.

b Based on the confidence interval, can we be 95 percent confident that the mean yield using catalyst XA-100 exceeds 750 pounds per hour? Explain.

Mean 811

Standard Error 8.786353

Median 814

Mode N/A

Standard Deviation 19.64688

Sample Variance 386

Kurtosis -0.12472

Skewness -0.23636

Range 52

Minimum 784

Maximum 836

Sum 4055

Count 5

Confidence Level(95.0%) 24.39488

Q3

Part X: For each of the following situations, indicate whether an error has occurred and, if so, indicate what kind of error (Type I or Type II) has occurred.

a We do not reject H0 and H0 is true.

b We reject H0 and H0 is true.

c We do not reject H0 and H0 is false.

d We reject H0 and H0 is false.

Part Y: What is the level of significance alpha? Specifically, state what you understand by an alpha value of 0.05 and how it is related to Type 1 error?

Q4 Consolidated Power, a large electric power utility, has just built a modern nuclear power plant. This plant discharges waste water that is allowed to flow into the Atlantic Ocean. The Environmental Protection Agency (EPA) has ordered that the waste water may not be excessively warm so that thermal pollution of the marine environment near the plant can be avoided. Because of this order, the waste water is allowed to cool in specially constructed ponds and is then released into the ocean. This cooling system works properly if the mean temperature of waste water discharged is 60°F or cooler. Consolidated Power is required to monitor the temperature of the waste water. A sample of 100 temperature readings will be obtained each day, and if the sample results cast a substantial amount of doubt on the hypothesis that the cooling system is working properly (the mean temperature of waste water discharged is 60°F or cooler), then the plant must be shut down and appropriate actions must be taken to correct the problem.

a Consolidated Power wishes to set up a hypothesis test so that the power plant will be shut down when the null hypothesis is rejected. Set up the null and alternative hypotheses that should be used.

b In the context of this situation, interpret making a Type I error; interpret making a Type II error.

c Suppose Consolidated Power decides to use a level of significance alpha = 0.05, and suppose a random sample of 100 temperature readings is obtained. For each of the following sample results, determine whether the power plant should be shut down and the cooling system repaired:

1. Sample Mean = 60.482 and Sample Standard Deviation = 2

2. Sample Mean = 60.262 and Sample Standard Deviation = 2

3. Sample Mean = 60.618 and Sample Standard Deviation = 2

You should show the 5 step STOH for each sample result.

Q5. Advertising research indicates that when a television program is involving (such as the 2002 Super Bowl between the St. Louis Rams and New England Patriots, which was very exciting), individuals exposed to commercials tend to have difficulty recalling the names of the products advertised. Therefore, in order for companies to make the best use of their advertising dollars, it is important to show their most original and memorable commercials during involving programs. In an article in the Journal of Advertising Research, Soldow and Principe (1981) studied the effect of program content on the response to commercials. Program content, the factor studied, has three levels-more involving programs, less involving programs, and no program (that is, commercials only)-which are the treatments. To compare these treatments, Soldow and Principe employed a completely randomized experimental design. For each program content level, 29 subjects were randomly selected and exposed to commercials in that program content

level as follows:

(1) 29 randomly selected subjects were exposed to commercials shown in more involving programs,

(2) 29 randomly selected subjects were exposed to commercials shown in less involving programs, and,

(3) 29 randomly selected subjects watched commercials only (note: this is called the control group).

Then a brand recall score (measured on a continuous scale) was obtained for each subject. The 29 brand recall scores for each program content level are assumed to be a sample randomly selected from the population of all brand recall scores for that program content level. The mean brand recall scores for these three groups were as follows:

(1) 1.21

(2) 2.24

(3) 2.28

Furthermore, a one-way ANOVA of the data shows that SST = 21.40 and SSE = 85.56.

a. Identify the value of n, the total number of observations, and k, the number of treatments.

b. Calculate MST using MST = SST/(k-1)

c. Calculate MSE using MSE = SSE/(n-k+1)

d. Calculate F = MST/MSE.

e. Define the null and alternate hypotheses using the treatment means M1, M2, and M3 to represent each group. Then test for statistically significant differences between these treatment means. Set alpha =.05. Use the F-table to obtain the critical value of F. You should show the 5 steps in the STOH.

f. If you found a difference due to the treatments, between which groups do you think this treatment is most likely? Note you do have to perform tests to provide this answer.

Q6 An accountant wishes to predict direct labor cost (y) on the basis of the batch size (x) of a product produced in a job shop. Using labor cost and batch size data for 12 production runs, the following Excel Output of a Simple Linear Regression Analysis of the Direct Labor Cost Data was obtained. The scatter plot of this data is also shown.

Regression Statistics

Multiple R 0.99963578

R Square 0.999271693

Adjusted R Square 0.999198862

Standard Error 8.641541

Observations 12

ANOVA

df SS MS F Significance F

Regression 1 1024593f 1024593 13720.47k 5.04436E-17m

Residual 10 746.7624g 74.67624

Total 11 1025340h

Coefficients Standard Error t Stat P-value

Intercept 18 a 4.67658 3.953211c 0.00271e

BatchSize(X) 10b 0.08662 117.13d 5.04436E-17e

For your aid, the different values in the ANOVA table are explained below using the superscript notation:

a: b0, b: b1, c: t for testing H0: b0 = 0, d: t for testing H0: b1 = 0,

e: p-values for t statistics, f: Explained variation, g: SSE = Unexplained variation,

h: Total variation, k: F(model) statistic, m: p-value for F(model)

Answer the following questions based on the information provided above:

a. Write the regression equation for the LaborCost (y) and BatchSize (x). Note that your equation has to identify the point estimates for b0 and b1 in the equation:

y = b0 + b1x

b Identify the t statistic and the p-value for this t statistic for testing the significance of the slope of the regression line. Using this, determine whether the null hypothesis

H0: b1 = 0 can be rejected?

c What do you conclude about the relationship between LaborCost (y) and BatchSize (x)? Use the different test statistics provided in the data to support your case.

d. Interpret the meanings of b0 and b1. Does the interpretation of b0 make practical sense for this case? Think carefully about what the value of x will be when y = b0 .

e Estimate the value of LaborCost for a batch size of 10. Use your regression equation and show all your steps.

Q7 Use the following data for the given situation: International Machinery, Inc., produces a tractor and wishes to use quarterly tractor sales data observed in the last four years to predict quarterly tractor sales next year.

All the data for answering the problems (a) through (c) has been provided to you. You do not have to compute any data for parts (a) through (c).

a. What type of seasonal variation do you see in the sales data? Is there no seasonal variation, constant seasonal variation, increasing seasonal variation, or decreasing seasonal variation? State your reasons. Find and identify the four seasonal factors for quarters 1, 2, 3, and 4.

b. What type of trend is indicated by the plot of the deseasonalized data?

c. What is the equation of the estimated trend that has been calculated using the deseasonalized data?

d. Compute a point forecast of tractor sales (based on trend and seasonal factors) for each of the quarters next year. You should show all your steps for each quarter forecast. (Hint: Note that you will use the equation from ( c ). This will provide you with the deseasonalized data. You then have to adjust it for the seasonal factor applicable for the quarter.)