# CS2211a Assignment 4 Solved

35.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

5/5 - (1 vote)

# Program 1 (35 marks)

The area of a circle of radius r is given by

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Area of a circle = Ï€ Ã— r2.

Imagine that you divided this circle exactly into 4 quadrants. The area of one quadrant is then 0.25 Ã— Ï€ Ã— r2.

Let us set the radius of this circle to be r = 1. The equation hence becomes

Area of one quadrant of a circle of radius 1 = 0.25 Ã— Ï€.

We can simply say:

Ï€ = 4 Ã— the area of one quadrant of a circle of radius 1.

To calculate the area of the shaded quadrant of the circle, use a random number generator x and guess effectively the correct value of the constant Ï€. The technique you should use is to:

• Generate a random number between 0.00 and 1.00 and assign it to x.
• Generate a random number between 0.00 and 1.00 and assign it to y.
• If (x2+y2 <= 1), it means that this (x,y) coordinate lies inside the shaded quadrant.
• Repeat these steps N times, where N is sufficiently large number and then calculate the ratio of the points located inside the circle to an the total number of generated points, i.e., N. This ratio should approximate the area of one quadrant of this circle.
• Multiply the calculated ratio by 4 to find an approximation of the mathematical constant Ï€.

Using the above procedure, draw a flowchart and write a C program to approximate the mathematical constant Ï€.

Your program must read and validate the value of N (a positive integer) from the user at the beginning of execution.

Add as many inline comments as you can to make your program well documented and easy to be understood by anyone who reads it.Â  o Test your program using

Â Â Â Â Â Â Â Â Â  N = 10, N = 100, N = 1`000, N = 100`000, N = 1`000`000, N = 10`000`000, and N = 100`000`000. o Loop inside your program to recalculate the value of Ï€ 10 times using each of the above values of N. Calculate the mean (i.e., the average value) and standard deviation for results generated from the same value of N. Report all results (i.e., 70 values, 7 means and 7 standard deviations).

âˆ‘

# Program 2 (30 marks)

Draw a flowchart and write a C program that prints an nÃ—n magic square (a square arrangement of the numbers 1, 2, â€¦, n2 in which the sum of the elements in any row, column, or diagonal is the same). This program should generate a magic square of a specified size n, where the user will specify this size at run time. The size must be an odd positive integer number between 1 and 13 (to be validated by your program). You should store the magic square in a two-dimensional array.

Start by placing the number 1 in the middle of row 0. Place each of the remaining numbers 2, 3, â€¦, n2 by moving up one row and left one column. Any attempt to go outside the bounds of the array should wrap around to the opposite side of the array. For example, if row = 0, instead of storing the next number in row -1, we would store it in row n-1 (the last row). If column = n-1, instead of storing the next number in column n, we would store it in column 0. In addition, if a particular array element is already occupied (filled), put the number directly below the previously stored number. Here it is a sample output:

 Enter size of magic square: 6 Invalid size, try again… Enter size of magic square: 3 8Â Â Â  1Â Â Â  6 3Â Â Â Â Â Â  5Â Â Â  7 4Â Â Â Â Â Â  9Â Â Â  2

 Enter size of magic square: 5 17Â Â Â Â Â Â  24Â Â Â Â Â Â  1Â Â Â Â Â Â Â  8Â Â Â Â Â Â  15 23Â Â Â Â Â Â Â  5Â Â Â Â Â Â  7Â Â Â Â Â Â  14Â Â Â Â Â Â  16 4Â Â Â Â Â Â Â  6Â Â Â Â Â  13Â Â Â Â Â Â  20Â Â Â Â Â Â  22 10Â Â Â Â Â Â Â Â Â Â Â Â  12Â Â Â Â Â  19Â Â Â Â Â Â  21Â Â Â Â Â Â Â  3 11Â Â Â Â Â Â Â Â Â Â Â Â  18Â Â Â Â Â  25Â Â Â Â Â Â Â  2Â Â Â Â Â Â Â  9

In your program, you are NOT allowed to use the array notation, other than declaring the array. I.e., you can have int magic_square[12][12];

Yet, you are not allowed to use magic_square[i][j] to access the array elements in your program. Instead, you have to use the pointers notation to access the array elements.

Add as many inline comments in your program as you can to make it well documented and easy to be understood by anyone who reads it.

You should include enough actual test cases in your submission to demonstrate the functionality of your program, e.g., run your program with n = 1, 3, 4, 5, 11, and 13.

Â

# Program 3 (35 marks)

Draw a flowchart and write a C function that determines the smallest number of \$20, \$10, \$5, \$2, and \$1 bills/coins necessary to pay a dollar amount. The function prototype must be as follow:

void pay_amount(int dollars, int *twenties, int *tens, int *fives, int

*toonies, int *lonnie);

where the dollar amount is represented by the dollars parameter. The twenties parameter points to a variable in which the function will store the number of \$20 bills required. The tens, fives, toonies and lonnie parameters are similar.

You should add as many inline comments as you can to make your code well documented and easy to be understood by anyone who reads it.

To test your function, write a program that asks the user to enter an integer value (a dollar amount), calls pay_amount to get the smallest number of bills/coins necessary to pay the amount, and then display the values returned by the function.

You should include enough actual test cases in your submission (i.e., a cut and paste from actual run to your program) to demonstrate the functionality of your program, to test various cases (at least 8 different cases).

• cs-2211-assignment-4-rjxjny.zip