computer science chapter 7 : math functions in c#

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What's the difference between sequential algorithms, branching algorithms, and iterative algorithms?

-sequential algorithms do not have to make any decisions or repeat sections of code in a loop -branching algorithms will execute some steps based on decision-making logic ( if(), else if (), else () ) -iterative algorithms will use for(), while(), or do while() loops to repeat sections of code until a solution is found

What method would you use to find the absolute value of a number?

Abs(X)

What System object can be used to perform common math operations?

Math object

What method can be used to round numbers?

Round(X)

What method can be used to find the square root of a number?

Sqrt(X)

What C# object is used to create random numbers?

System.Random

a) What are the two short-cuts that can be used to add 1 to a variable? b) What are the two short-cuts that can be used to subtract 1 from a variable?

a) "++" and "+=1" b) "--" and "-="

What are the common math operators and how are they used in mathematical expressions?

a) +, -, *, /, % b) used to perform some math using two operands

How can you visually display an algorithm? Which symbol represents a decision? An action? The program flow?

a) a flow chart b) diamonds c) rectangles d) arrows

What is the program design process? What are the four steps included?

a) designing coded solutions to specific problems b) 1. start with a high-level description of your algorithm 2. write a program to test your algorithm 3. write code to implement your algorithm 4. run your test program to verify your algorithm works

a) Is the upper bound of the Next() function inclusive or exclusive? b) What about the lower bound?

a) exclusive b) inclusive

What is an algorithm and how can it be useful in computer science?

a) the exact sequence of steps your program takes to solve a problem is an algorithm. b) to produce an answer to a given problem description

Describe the algorithm for finding prime numbers.

bool isPrime(int possiblePrime) { if (possiblePrime == 1) // 1 is not a prime number return false; int topDivisor = possiblePrime / 2; for (int i = 2; i <= topDivisor; ++i) { if (possiblePrime % i == 0) // if evenly divisible return false; // not prime } return true; // yes this is prime! }

Describe the algorithm for finding the average of two or more numbers.

double findAverage(double[] inputs) { if (inputs.Length == 0) return 0; double sum = 0; foreach (double value in inputs) { sum += value; } return sum / inputs.Length; }

When might you need to use random numbers in a program?

games where you roll a dice, pick a random card, or have a monster behave randomly

Describe the algorithm for finding the greatest common divisor (GCD).

int findGCD(int value1, int value2) { int remainder; while (value2 != 0) { remainder = value1 % value2; value1 = value2; value2 = remainder; } return value1; }

Describe the algorithm for finding the largest of three given numbers.

int findGreatest(int value1, int value2, int value3) { if ((value1 > value2) && (value1 > value3)) { return value1; // first number is the largest } else if (value2 > value3) { return value2; // second number is the largest } else { return value3; // third number is the largest } }

What are some of the quick ways you can use a variable in a mathematical expression and then store the result of the expression in the same variable?

int i = 5; i += 1; // same as i = i + 1 i -= 2; // same as i = i - 2 i * = 4; // same as i = i * 4 i /= 2; // same as i = i / 2 i += (4 * 3 - 2) // same as i = i + (4 * 3 - 2)

How would you create a random number that is between 0 and 100?

int value = rand.Next(100); // use Next() method

How would you create a random number that is between 5 and 15?

int value = rand.Next(5, 15) // use Next() method

Rules of operator precedence

multiplication, division, and modulus operators will execute first from left to right, then the addition and subtraction operators will execute from left to right PARENTHESES OVERRIDE OPERATOR PRECEDENCE

Is there only one possible algorithm for one problem? Explain.

no; you can write more than one algorithm that will give you the correct answer

What is the purpose of the built-in Math object constants?

the exact value does not end so it is already built in, you will get a more exact answer

Describe the algorithm for making change from a sales transaction.

void makeChange(int amountDue) { int quarters = 0; int dimes = 0; int nickels = 0; int pennies = 0; while (amountDue > 25) { quarters++; amountDue -= 25; } while (amountDue > 10) { dimes++; amountDue -= 10; } while (amountDue > 5) { nickels++; amountDue -= 5; } while (amountDue > 1) { pennies++; amountDue -= 1; } Console.Write("You will need " + quarters + " quarters, " + dimes + " dimes, " + nickels + " nickels, and " + pennies + " pennies."); }


Ensembles d'études connexes

(FON) Unit 9, Chapter 18/23 Study Guide

View Set

Ch. 25: Chinese and Korean Art after 1279

View Set

Econ 320 Lecture 08 Practice Problems

View Set

immunization self-studies modules

View Set