Scripting and Programming - Foundations - C173 (Unit 2)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the role of * in the expression x + z * 10?

((y+0.5)/4.0 ) (15.5 + 0.5) is evaluated first, yielding 16.0. 16.0 / 4.0 is 4.0.

Given x = 10 and y = 4. What value does the expression (x - y) * 2 evaluate to?

(12) (10 - 4) is evaluated first, yielding 6. 6 * 2 is 12.

What is the best declaration type for maintaining a person's weight for every month of the year?

(Array) 12 values must be held, one for each month. Thus, an array of size 12 should be used. Ex: float array(12) userWeights.

What is the best declaration type for a list of player numbers on a soccer team?

(Array) A scalar can only hold one value, while an array can hold a list of values, as needed here. Ex: integer array(20) playerNumbers (assuming up to 20 players on the team).

A program determines if the number of people admitted to a concert venue exceeds the maximum seats of 1500. How should the item to hold the maximum seats be declared?

(Constant integer maxSeats)The maximum number of seats for the venue is fixed and will not change, so a constant should be used. The number of seats is countable, so the type should be an integer.

The weekly sales for a sales person was 14 cars. Which operation should be used to compute the average daily sales, which is 2.0 cars per day?

(Division) The average daily sales is computed as the weekly sales divided by the number of days in a week, or 7.

Which data type should be used to hold the value 210.25?

(Float) A float variable can hold floating-numbers, such as 210.25.

A variable should hold the number of people attending an event. What data type should the variable be?

(Integer) The number of people attending the event is countable and can be represented as an integer.

What is the best declaration type for a person's height?

(Scalar) Only one value must be held, so a normal scalar variable can be used. Ex: float userHeight.

Which statement assigns itemSavings with the difference of normalPrice and salePrice.

(itemSavings = normalPrice - salePrice) The statement assigns the variable itemSavings with the result of the expression normalPrice - salePrice.

×

* In mathematics, the symbol for multiplication is usually × or a dot, as in a × y, or a · y. However, in programming, only keyboard characters may be used, so the symbol used is *.

-

- In mathematics, minus is often written using a wide segment known as an emdash: —. However, in programming, only the hyphen symbol - may be used, since that character exists on a keyboard.

unary -

- used for negation (unary minus) is next

Two common conventions for distinguishing words in an indentifier are:

-Camel case: Lower camel case abuts multiple words, capitalizing each word except the first, as in numApples or peopleOnBus. -Underscore separated: Words are lowercase and separated by an underscore, as in num_apples or people_on_bus.

Choosing a variable type (float vs. integer)

-Integer variables are typically used for values that are counted, like 42 cars, 10 pizzas, or -95 days. -Floating-point variables are typically used for values that are measured, like 98.6 degrees, 0.00001 meters, or -666.667 grams. -Floating-point variables are also used when dealing with fractions of countable items, such as the average number of cars per household.

Boolean

A Boolean refers to a quantity that has only two possible values, true or false.

Character

A character is a single letter, like 'A', 'p', or '%'.

Type conversion

A conversion of one data type to another, such as an integer to a float.

Arrays and loops

A key advantage of arrays becomes evident when used with loops. The program below uses a loop to allow a user to enter 5 integer values, storing those values in array userVals, and then outputting those 5 values. The loop makes use of the array's size attribute, accessed via userVals.size, which in this case has value 5.

Function

A list of statements executed by invoking the function's name, such invoking known as a function call.

Identifier

A name created by a programmer for an item like a variable or function. An identifier must: -Be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9) -Start with a letter or underscore

Variable

A named item, such as x or numPeople, used to hold a value.

Constant

A named value item that holds a value that cannot change. Constants are commonly used in programs to hold the value of mathematical or physical constants, such as Pi, the speed of light, or kilograms per pound. Constants can also be used for any value that should not change during the program's execution.

Floating-point literal

A number with a fractional part, even if that fraction is 0, as in 1.0, 0.0, or 99.573.

Floating-point number

A real number, like 98.6, 0.0001, or -666.667. The term "floating-point" refers to the decimal point being able to appear anywhere ("float") in the number.

Naming conventions

A set of style guidelines defined by a company, team, teacher, etc, for naming variables.

Literal

A specific value in code, like 2.

String

A string is a sequence of characters, like "Hello" or "The forecast for today is sunny with highs of 75F.".

Operator

A symbol that preforms a built-in calculation, like the operator + which preforms additions.

Reserved word (or keyword)

A word that is part of the language, like an integer, Get, or Put. A programmer cannot use a reserved word as an identifier.

Common errors

Accidentally perform integer division when floating-point division was intended.

Array

An array is an ordered list of items of a given data type, like an array of integers or an array of floats.

Size

An array stores a size attribute indicating the number of array elements

Unary minus

An exception is minus used as negative, as in: xCoord = -yCoord. Minus (-) used as negative.

Evaluates

An expression evaluates to a value, which replaces the expression. Ex: If x is 5, then x + 1 evaluates to 6, and y = x + 1 assigns y with 6.

Precedence rules

An expression is evaluated using the order of standard mathematics

+ -

Finally come + and - with equal precedence.

Divide-by-zero-error

For integer division, the second operand of / or % must never be 0, because division by 0 is mathematically undefined. A divide-by-zero error occurs at runtime if a divisor is 0, causing a program to terminate.

* /

Next to be evaluated are * and /, having equal precedence.

[ ]

Replaced by ( ) In mathematics, subexpressions may be enclosed in parentheses ( ), by brackets [ ] as above, or even by braces { }, to improve readability. In programming, typically only parentheses are allowed. Brackets and braces are typically used for other purposes in programs.

Vector

Similar to an array, each item in an array is known as an element.

Spaces in variable names

Single words Informally, quantities may be represented with multiple words, as in Heart Rate. In a program, a variable's name must be one word, as in heartRate. Above, the programmer used heartBPM to indicate beats-per-minute for the rate.

Float

Stores a floating-point number

Division: Integer rounding

When the operands of / are both integers, the operator performs integer division, which does not generate any fraction. The / operator performs floating-point division if at least one operand is a floating-point type.

Implicit conversion

zyFlowchart automatically performs several common conversions between integer and float types, and such automatic conversion

_

Underscore

Arguments

Any function input values

Assignment statement

Assigns a variable with a value, such as x = 5. That statement means x is assigned with 5, and x keeps that value during subsequent statements, until x is assigned again.

Integer

Can hold whole number values, like 1, 999, 0, -25 (not 3.5 or 0.001)

No commas allowed

Commas are not allowed in an integer literal. So 1,333,555 is written as 1333555.

Type cast

Converts a value of one type to another type. A programmer can type cast an integer to float by multiply the integer by the float literal 1.0. Ex: If myIntVar is 7, then myIntVar * 1.0 converts integer 7 to float 7.0.

Variable declaration

Declares a new variable, specifying the variable's name and type.

Infinity or -Infinity

Dividing a nonzero floating-point number by zero results in infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs Infinity or -Infinity.

left-to-right

If more than one operator of equal precedence could be evaluated, evaluation occurs left to right.

Index

In an array each element's location number is called the index.

= is not equals

In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5", and not as "x equals 5". When one sees x = 5, one might think of a box labeled x having the value 5 put in.

Not a number

Indicates an unrepresentable or undefined value.

Incremental development

Is the process of writing, compiling, and testing a small amount of code, then writing, compiling, and testing a small amount more (an incremental amount), and so on.

( )

Items within parentheses are evaluated first

Expression

May be a number like 80, a variable name like numApples, or a simple calculation like numApples + 1. Simple calculations can involve standard math operators like +, -, and *, and parentheses as in 2 * (numApples - 1).

Case sensitive

Meaning uppercase and lower case letters differ.So numCats and NumCats are different.

Scalar

To contrast with array variables, a single-item (non-array) variable is called a scalar variable.

Modulo operator (%)

The basic arithmetic operators include not just +, -, *, /, but also %. The modulo operator (%) evaluates to the remainder of the division of two integer operands. Ex: 23 % 10 is 3.

Six degrees of separation

The popular idea that any two people on earth are connected by just "six degrees of separation", accounting for overlapping of known-people.


Set pelajaran terkait

Econ Midterm #5 Norman Maynard College of Charleston (Chapters 15, 16, 17 & 18)

View Set

LFoB Ch 12 Quiz and Concept Checks

View Set

Lewis, Ch. 58 (Select All That Apply)

View Set

Micobiology Chapter 9: Biotechnology Recombinant DNA

View Set

Anticoagulants, Antiplatelets & Thrombolytics NCLEX

View Set

Ch.2 Gross Income and Exclusions Quiz

View Set

NOCTI- Healthcare Core Questions

View Set