COSC-1337 Chapter 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

3.8 - What is cin.ignore?

cin.ignore tells the cin object to skip one or more characters in the keyboard buffer. cin.ignore(20, '\n'); this tells cin to skip the next 20 characters or until a newline is encountered, whichever happens first. cin.ignore(); cin only skips the very next character

3.8 - What is cin.get?

cin>> will not handle whitespace/enter. To overcome this us the cin member function cin.get. This will store any char including whitespace/enter. cin.get(ch); ch = cin.get(); or to pause for <ENTER> : cin.get();

3.7 - What are the output stream manipulators?

setw(n) - Sets a minimum print field width of size n for the next value output . fixed - Displays floating -point numbers in fixed point (i.e., decimal) form. showpoint - Causes a decimal point and trailing zeroes to be displayed for floating-point numbers, even if there is no fractional part. setprecision(n) - Sets the precision of floating -point numbers . left - Causes subsequent output to be left-justified. right - Causes subsequent output to be right-justified.

3.8 - How does a Cstring differ from a string object?

-cannot be assigned with the assignment operator -assign with strcpy strcpy(Cstring, value); //Cstring receives content of value -to manage memory allocation (and to avoid buffer overun) cin>>setw(10)>>word; or cin.width(10); cin >>word; -must use cin.getline rather than getline

3.5 - What is a named constant?

A named constant, also called a constant variable, is like a variable, but its content is read-only and cannot be changed while the program is running. Here is a definition of a named constant: const double INTEREST_RATE = 0.069; The key word const is a qualifier that tells the compiler to make the variable read-only. A named constant can have any legal C++ identifier name, but many programmers use all uppercase letters in the name to distinguish it from a regular variable. When a named constant is defined, it must be initialized with a value.

3.1 - What is a prompt?

A prompt lets the user know that an input is expected and prompts them as to what must be entered. When cin is used to get input from the user, it should always be preceded by a prompt.

3.3 - What is type casting?

A type cast expression lets you manually promote or demote a value in situations where C++ will not perform the desired conversion automatically. Its general format is static_cast<DataType>(Value) where Value is a variable or literal value that you wish to convert and Data Type is the data type you wish to convert it to. Here is an example of code that uses a type cast expression: double number= 3.7; int val; val=static_cast<int>(number);

3.4 - What happens when you encounter overflow or underflow?

Although some systems display an error message when an overflow or underflow occurs, most do not. The variable simply holds an incorrect value now and the program keeps running. Therefore, it is important to select a data type for each variable that has enough bits to hold the values you will store in it.

3.8 - What is the assign member function?

Assign lets you assign a set of repeated characters to a string without counting. If you wanted a string of 22 spaces: string spaces; spaces.assign(22, ' ');

3.6 - What is a combined assignment operator and how is it used?

Combined assignment operators, also known as compound operators or arithmetic assignment operators, allow you to perform arithmetic on a variable without typing the variable name twice. So num=num+1 can be written as num += 1 Note: the precedence of the combined assignment operators is lower than that of the regular arithmetic operators.

3.3 - In what order are data types ranked?

Data types are ranked in descending order as follows: long double double float unsigned long long int long long int unsigned long int long int unsigned int int

3.4 - What is underflow?

In addition to overflow , floating -point values can also experience underflow. This occurs when a value is too close to zero , so small that more digits of precision are needed to express it than can be stored in the variable holding it.

3.6 - What is multiple assignment and how is it performed?

Multiple assignment means to assign the same value to several variables with one statement. If a program has several variables, such as a, b, c, and d, and each variable needs to be assigned the same value, such as 12, the following statement may be written: a= b = c = d = 12; This works because the assignment operations are carried out from right to left. First 12 is assigned to d. Then d's value, now a 12, is assigned to c. Then c's value is assigned to b, and finally b's value is assigned to a. Here is another example. store1 = store2 = begInv;

3.3 - What are the exceptions to data type ranking?

One exception to data type ranking is when an int and a long int are the same size. In that case, an unsigned int outranks a long int because it can hold a higher value.

3.6 - What are examples of combined assignment operators?

Operator~Example Usage ~ Equivalent To += ~ x += 5; ~ x = x + 5; -= ~ y -= 2; ~ y = y - 2; *= ~ z *= 10; ~ z = z * 10; /= ~ a /= b; ~ a = a/b; %= ~ c %=3; ~ c = c % 3;

3.4 - What is overflow?

Overflow is a phenomenon where operations on 2 numbers exceeds the maximum (or goes below the minimum) value the data type can have.

3.2 - What is the pow function?

Pow is a library function that is used to raise a number to a power. To use the pow function the program must include the cmath header file, the first of the two arguments you pass to the function, if not both should be a double. Pow returns a double so any variable that value is assigned to should also be a double.

3.8 - What is a C-String?

Prior to the introduction of the string class, strings were stored as a set of individual characters as an array. To define word to be an array of characters that will hold a C-string and initialize it to "Hello" char word[10]="Hello"; because of the null terminator, this example of word can only hold 9 characters. -no special header file -can input with cin -can display with cout -everything else is different

3.7 - What is setw?

Setw is a stream manipulator that can be used to establish print fields of a specified width. It requires the <iomanip> preprocessor directive: value = 23; cout << "(" << setw(5) << value << ")"; ( 23) The number in the parenthesis after the word setw specifies the minimum field width for the value immediately following it.

3.1 - C3.2 - What is the >> symbol called?

The >> symbol is called the stream extraction operator.

3.7 - What is the fixed manipulator?

The fixed manipulator is a stream manipulator which indicates that floating-point output should be printed in fixed-point, or decimal, notation. -prevents conversion to scientific notation -requires the <iomanip> header file. -when used with setprecision, setprecision specifies the number of digits to display after the decimal point cout << fixed << setprecision(2) //for 2 decimal places

3.10 - What is rand?

The function called rand() generates random numbers. It returns a non-negative integer each time it is called. To use the rand() function, you must include the cstdlib header file in your program. randomNum =rand (); However, the numbers returned by the function are really pseudorandom. They are actually generated with an algorithm. The algorithm needs a starting value, called a seed, to generate the numbers.

3.8 - What is the getline function?

The getline function reads in an entire line, including leading and embedded spaces, and stores it in a string object. This contrasts with using cin which ignores leading spaces and stops at the first non character. getline(cin, inputLine); cin is the input stream we are reading from inputLine is the name of the string variable receiving the input stream

3.1 - C3.1 - What header file must be included in programs using cin?

The header file iostream must be included in programs using cin.

3.7 - What are the left and right manipulators?

The left manipulator allows you to force a value to print on the left side of its field, padded by blanks on the right. -remains in effect until you cancel with the right manipulator -requires the <iomanip> header file.

3.8 - What is the string member function length?

The length member function can inform you of the length of a string stored in a string object. string state= "New Jersey"; int size = state.length( ); The first statement creates a string object named state , and initializes it with the string "New Jersey·. The second statement defines an int variable named size , and initializes it with the length of the string in the state object. After this code executes, the size variable will hold the value 10. The blank space between "New" and "Jersey" is a character and is counted just like any other character.

3.7 - What is the setprecision manipulator?

The setprecision manipulator can control the number of significant digits with which floating-point values are displayed. -requires the <iomanip> header file. -rounds, rather than truncates, the number. -remains in effect until it is changed to another value cout << setprecision(5) << value << endl;

3.7 - What is formatting?

The way a value is printed is called its formatting.

3.10 - How do you generate a random number in a specified range?

To limit the range of the random number to an integer between 1 and some maximum value max, you can use the following formula. number= rand ()% max + 1; Here is how you could assign the variable number a random integer in the range of 10 through 18. const int MIN_VALUE = 10; const int MAX_VALUE = 18 ; number = rand () % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE; In this code MAX_VALUE - MIN_VALUE + 1 evaluates to 9, the number of integers in the desired range. The modulus operation thus returns a value between 0 and 8. Adding MIN_VALUE, which is 10, produces a value between 10 and 18.

3.3 - What is type coercion?

When C++ is working with an operator, it strives to convert the operands to the same type. This implicit, or automatic, conversion is known as type coercion. When a value is converted to a higher data type, it is said to be promoted . To demote a value means to convert it to a lower data type.

3.3 - What happens when an operator works with two values of different data types?

When an operator works with two values of different data types, the lower-ranking value is promoted to the type of the higher-ranking value. (Book rule 2)

3.3 - What happens when an operator's operands are of different data types?

When an operator's operands are of different data types, C++ automatically converts them to the same data type. When it does this it follows a set of rules based on their ranking.

3.8 - When used with strings, what does the + operator do?

When the + operator is used with string operands it concatenates them, or joins them together. string greeting1 = "Hello ", greeting2; string word1 = "World"; string word2 = "People"; greeting2 = greeting1 + word1 ; // greeting2 now holds "Hello World" greeting1 = greeting1 + word2; II greeting1 now holds "Hello People" Notice that the string stored in greeting1 has a blank as its last character. If the blank were not there, greeting2 would have been assigned the string "He11oWorld". The last statement could also have been written using the += combined assignment operator, like this: greeting 1 += word2 ;

3.3 - What happens when the final value of an expression is of a different data type than that of the variables?

When the final value of an expression is assigned to a variable, it will be converted to the data type of that variable (promoted or demoted as necessary)

3.1 - Can you use cin to enter multiple values at once?

Yes, you can use cin to enter multiple values at once as long as the user separates the value by one or more spaces and presses <ENTER> after the final input.

3.1 - C3.3 - Where does cin read its input from?

cin reads its input from the console (or keyboard)

3.9 - What are some common functions available from the cmath library?

abs -- y = abs (x); -- Returns the absolute value of the argument . The argument and the return value are integers. cos -- y = cos (x); -- Returns the cosine of the argument . The argument should be an angle expressed in radians. The return type and the argument are doubles. exp -- y = exp (x); -- Computes the exponential function of the argument, which is x. The return type and the argument are doubles. fmod -- y = fmod(x, z); -- Returns, as a double, the remainder of the first argument divided by the second argument . Works like the modulus operator, but the arguments are doubles. (The modulus operator only works with integers.) Take care not to pass zero as the second argument . Doing so would cause division by zero. log -- y = log(x); Returns the natural logarithm of the argument . The return type and the argument are doubles. log 10 -- y = log10(x); Returns the base-10 logarithm of the argument . The return type and the argument are doubles. pow -- y = pow(x , z ); Returns the first argument raised to the power of the second one. round -- y = round (x); Returns the argument rounded to the nearest whole number. The return value is an integer. sin -- y = sin(x); Returns the sine of the argument. The argument should be an angle expressed in radians. The return type and the argument are doubles. sqrt -- y = sqrt(x); Returns the square root of the argument . The return type and argument are doubles. The argument must be zero or greater. tan -- y = t an(x); Returns the tangent of the argument . The argument should be an angle expressed in radians. The return type and the argument are doubles.

3.2 - How do you use the pow function?

area = pow(4.0, 2); This statement contains a call to the power function. The pow function always raises the first argument to the power of the second. In this example, 4.0 is raised to the power of 2. The result is returned from the function and used in the statement where the function call appears.

3.3 - What happens when you use char, short, and unsigned short values in a mathematical expression?

char, short, and unsigned short values are automatically promoted to int values. You will notice that char, short , and unsigned short do not appear on the data type ranking list. That's because anytime values of these data types are used in a mathematical expression, they are automatically promoted to an int. (Book rule 1) (The only exception to this rule is when an unsigned short holds a value larger than can be held by an int . This can happen on systems where a short is the same size as an in t . In this case, the unsigned short is promoted to unsigned int)

3.7 - what is the showpoint manipulator?

showpoint overrides the default hiding of decimal digits for floating-point numbers. -requires the <iomanip> header file. -remains in effect until the programmer explicitly changes it.

3.10 - What is srand?

srand provides the seed for the rand random number generator. It also requires the cstdlib header file. Typically its used with the time function which requires the ctime header file. unsigned seed; seed=time(0); srand(seed); cout<<rand();


Conjuntos de estudio relacionados

Learning / Cognitive Test 2: Chapter 4 Multiple Choice

View Set

Basic Loadmaster Class: Block V: Unit 2

View Set

100% The Iroquois Creation Myth: "The World on Turtle's Back" PRE-TEST

View Set