CPSC111 – Introduction to Computing
计算概论代写 An algorithm is a set of step-by-step procedure for accomplishing a task.The word “algorithm” is derived from the ninth-century Muslim mathematician,
An algorithm is a set of step-by-step procedure for accomplishing a task.
The word “algorithm” is derived from the ninth-century Muslim mathematician, Al-Khwarizmi who worked on “written processes to achieve some goal” [Abu Ja’far Muhammad ibn Musa Al-Khwarizmi]
The term “algebra” also derives from the term “al-jabr (means “restoring”, referring to the process of moving a subtracted quantity to the other side of an equation),” which he introduced.
Computer algorithms are central to computer science. They provide step-by step methods of computations that a machine can carry out.
Programmer’s job is to turn the algorithm into a program by adding details, testing procedures, and debugging or correcting errors.
We use algorithms every day. Recipes, instructions, and directions are all examples of algorithms that are not programs.
When you start your car, you follow a step-by-step procedure. The algorithm might look something like this:
An Algorithm is a series of steps required to solve a problem.
Example of your everyday life simple task algorithm; an algorithm for baking a cake might be:
Step1: Heat oven to 180 degrees Centigrade
Step2: Gather the ingredients
Step3: Mix the ingredients in a bowl
Step4: Pour the ingredients into a cake tin
Step5: Bake in the oven 20 minutes
Step7: Bake in oven 5 minutes
Step8: Until cake cooked
Step9: Take cake out of oven and place on cake rack
We use algorithms in this way to help design the steps of a computer program.
Check your algorithm against the four criteria:
a. Un-ambiguous: When you read an algorithm, there should be no question about what should be done. If you understand cooking terms like “Gather the ingredients”, and “Baking a cake”, then you can probably follow most of this recipe.
b. Solves a problem: an algorithm should always present a solution to a particular problem. In our case, the problem must have been something like “Using these ingredients, make a cake.”
c. Legitimate input: In this example, “legitimate” ingredients include sugar, eggs, flour and butter.
d. Finite amount of time: once we start the algorithm, eventually it will finish at step 9
Pseudo-code is an outline of a program written in a way that it can be easily converted into a computer programming language.
Examples of Algorithms
Adding two numbers algorithm
Step 1 Start Step 2 Prompt the user to enter the first integer Step 3 Prompt the user to enter a second integer Step 4 Compute the sum of the two user inputs Step 5 Display the result Step 6 Stop
Others examples of Algorithms
Finding the largest of two numbers
Step 1 Start Step 2 Read A, B Step 3 If A > B, then BIG = A, otherwise BIG = B Step 4 Write BIG Step 5 Stop
Finding the square and the cube of a number
Step 1 Start Step 2 Read value of N Step 3 S = N * N Step 4 C = S * N Step 5 Write values of S, C Step 6 Stop
Finding average of any three numbers
Step 1 Start Step 2 Read values of X, Y, Z Step 3 S = X + Y + Z Step 4 A = S / 3 Step 5 Write value of A Step 6 Stop
The algorithms below have been written using pseudo-code. Can you say what each does?
1.start 2.set yes_votes to 79 3.set no_votes to to 17 4.total_votes = yes_votes + no_votes 5.display total_votes 6.stop
1.start 2.ask user for juice_cost_price 3.ask user for juice_sale_price 4.ask user for cans_sold 5.unit_profit = juice_sale_price - juice_cost_price 6.total_profit = unit_profit x cans_sold 7.display total_profit 8.stop
1.start 2.ask user for traffic light color 3.if color is red then 4. display "STOP" 5.else if color is yellow then 6. display "WAIT" 7.else 8. display "PASS" 9.stop
1.start 2.repeat 12 times 3.display "Happy Computing!" 4.end repeat 5.stop
Variables are reserved memory locations to store values; when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables
Python has some conventions for variable names. You can use any letter (lowercase or uppercase), the special character underscore “_” and every digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) provided you do not start with a digit.
Python variable names are case-sensitive, so counter and Counter are not the same variable.
The following list of words cannot be used as variable names. These are some reserved words (keywords) for Python. These keywords define the language rules and have special meanings in Python. Here is the list of all of them:
and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, yield
Python Function Template 计算概论代写
# Defining Functions # def starts a function definition # names of functions follow variable naming conventions #A docstring is a string literal that occurs as the first statement in a function, module, class, or method definition. def greeting(): """ A function for greeting people""" #docstring print("Hello, World!") print("From CPSC-111 Class") print("At Alexander College in Burnaby, Canada") greeting()