Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

--------------------------------------------------------------------------------

ID: 3867331 • Letter: #

Question

------------------------------------------------------------------------------------

Educational Goals: The educational goals of this program are that the student should use the concepts of

design with pseudocode

output of prompts and labels to match specified output

input of values from the keyboard

translating algebraic equations into Python expressions

using math library functions

calculations in assignment statements

priority of arithmetic operations

documentation

testing with valid inputs and invalid inputs

Imagine that three observers at three points on the plane are all looking at a lighthouse. They would like to know where the lighthouse is in terms of the coordinates of the plane. The lighthouse would be at the center of the circle determined by their positions.

The problem is also described as finding the equation of the circle determined by three points (as long as the three points are not all on the same straight line).

Assuming that the location of the 3 observers are given by (a, b), (c, d) and (e, f), the center (x, y) of the circle is

The radius of the determined circle is r. It is found using the distance formula, with one of the given points and the coordinates of the center.

Run Examples
When you have the program running correctly, Verify that your program produces the same output as in the following test cases.


--- Test case 1 ---

--- Test case 2 ---

--- Test case 3 ---

Design


First, read the assignment carefully. Look for how the program is supposed to behave. You do not know what the code looks like - that is fine. The assignment gives some examples of normal runs. Just as in the lab exercises, decide on what steps you will need to perform to solve this problem.

Requirments:

1- Use the Math fucntion from the built-in Math package

2- Your output should match the run examples above.

3- Make sure you document your program. Use good, multi-character, meaningful identifiers (variables).

4- At the top of your program, you should have a prolog (name, date, etc.) as shown a bove, and add comments explaining different operations in your code.

5- Save the program file as circle.py.

.

+ 15 (3.5) (0,4) (7.3) -5 5 10

Explanation / Answer

An Introduction to Computer Programming and Mathematics

Stage: 5

Article by David Saxton

Published February 2011.

A computer program is a series of instructions (also called code) given to the computer to perform some task, which could be anything from summing the numbers from 1 to 10 to modelling the climate. When the computer follows the instructions given in the program, we say that the computer is running the program. There are many different ways of writing these instructions for the computer (we speak of programming in different languages) - in this article, we will use a language called C++. By the end of it you will be able to write your own programs to perform basic mathematical and scientific tasks.

Our first C++ program

Our first C++ program will tell the computer to print out the text "Hello world!". Here it is (don't worry - it will be explained line by line).

For a computer to run this program, it must first be compiled by a compiler (this means translating it from the language of C++ to the computer's native machine code language). There's a useful online resource, codepad.org, which does the steps of compiling and running the program for us. Navigate to this website, select the option "C++" for the language, and copy and paste (or better yet, type out) the program above in the text box, before clicking the submit button. If you entered the text correctly, then you should see the following displayed for the output:

If this doesn't appear, then you may have entered the program incorrectly - try again.

To get a feel for what is going on, let us examine the structure of the program.

These first two lines tell the compiler about a range of functions that are available - a function is a block of code, which in this case, already exists in the computer memory ready for us to use. For now you don't need to understand exactly these lines mean; only that these should be placed at the top of most C++ programs that you will write. In this program, we want to use a function called "cout", which prints out text.

This type of structure denotes a function in the program, called "main". This is a special function; we can (and later will) define functions with other names, but the computer will look for this function for the initial instructions to start following, which we place inside the brackets {, } (these show where the function starts and stops). We will describe more on the function syntax later.

This is a comment line. When the compiler sees "//", it will ignore anything that comes after this until the end of the line. Adding this text has no effect on the behaviour of the program, but it can be useful for when a person wishes to read and understand the code at a later date.

This tells the computer to print out text. You don't need to worry about how cout works for now - only how to use it. A series of text or numbers can be printed out by combining them with <<. For example we could have written the above line instead as:

The endl means "end line" and prints out a newline character.

The semicolons ; tell the compiler where one instruction stops and another begins - their role is analogous to the role played by full-stops in sentences.

Exercise: We must be precise in programming. If we type out a name of a keyword incorrectly, miss out a bracket, or forget a semicolon, for example, then the compiler will not be able to understand the program. Identify the 5 mistakes in the following program.

Solution: include and using were spelt incorrectly. There is a semicolon missing at the end of the 2nd line. A space has been inserted into the "//" comment part. There is a quote " missing for the string in the 7th line.

Variables and arithmetic

A variable in a computer program is a piece of information such as a number in the computer's memory. Once we have a variable, we can make use of and modify its value as we please. This is analogous to how a human brain stores, for example, a friend's address - it is there ready to be recalled, and may change from time to time.

Variables are essential for computer programs to work. If we sum the numbers from 1 to 10, we need a variable to store the sum at each stage of the calculation. If a program receives two numbers from the user and calculates their sum, it must use variables to store the inputed values before the sum can be calculated.

Integer variables

In C++, there are many different types of variables, representing the different types of information that they refer to. To make use of a variable, we must first tell the computer what type we want. The first one we look at in this section are integer variables, for storing a whole number.

To "declare" an integer variable (which means setting aside computer memory for an integer), we include a line of code such as the following. The variable is then ready for use.

The "int" is the special codeword for integer. The "x" is the name of the variable, and can be replaced with anything made up of letters, numbers, and underscores (the first character cannot be a number however). The case of the letters are also important (so "hello" and "heLLo" would be seen as different by the computer). The " = 0" sets the variable's initial value to zero. We can also write "int x;" if we do not care about setting a specific value.

Exercise: Which of these are valid variable names?

Solution: 1, 3, 4 are valid variable names. Name 2 starts with a number. Name 5 contains an illegal character #.

We can later change the value of a variable by writing a piece of code such as:

This assigns the value 7 to x. To print out the value of a variable, we can again use the "cout" function - to display the value of x for example, we include the line of code:

Here is a complete program that declares a variable called x, with initial value 0, prints out its value, assigns the value of 7 to x and prints out its value again. This shows how variables can be read from and changed. Try it.

For integers, we can perform all the basic arithmetic operations such as addition, subtraction, multiplication and division. We use the symbols +, -, *, and / for these respectively. For example, this code creates two integer variables x and y with initial values, creates a third integer variable z, and sets the value of z to the value of x + y.

In the above code, z would now have the value of 9. We can also write the last two lines more quickly as "int z = x + y + 3". We can also write code such as:

The line "x = x * x" may look a bit strange - and if it was a mathematical equation then it would mean "x has a value satisfying x = x * x". In a computer program however, it means "take x, multipy it by itself, and assign the resulting value back to x". Now x would have the value 4.

There are another two operators, ++ and --, which are very useful (there are different types of operators - things like + and - will take two integer values and produce a third. ++ and -- take an integer variable and change its value). The expression "x = x + 1;" (increasing the value of x by 1) tends to occur a lot in programming, so C++ has a shorter way of writing this: the piece of code "x++;" has exactly the same effect. Similarly "x = x - 1;" and "x--;" are equivalent. For example, in the following lines of code, x has initial value 3, but has final value 5.

What about division? If x is an integer variable has value 4, and we write "int y = x / 2;", then y will have the value 2. However, what is the value of y in the following program?