this is C++ . thank you ~~!! Short answers: a. Describe the difference between t
ID: 3829573 • Letter: T
Question
this is C++ .
thank you ~~!!
Short answers: a. Describe the difference between the two commands j++ and ++j for an int j. b. Describe the difference between the statements b = 2 and b == 2. c. What are the three types of errors in coding, as described in lecture? Give an example of each. d. What is the difference between for, while and do-while loops? When would one prefer to use a for loop instead of while? e. Describe a function call vs. a function definition. Do parameter names need to match? Do parameter types need to match? When do we specify data types? f. Describe three functions (including their syntax and usage) from the string library. g. Describe three functions or keywords (including their syntax and usage) from the iomanip library. h. Describe the difference between integer division and floating-point division (including how each is invoked in your code). Give one example in which a programmer would want to use each. i. What do we mean by the term magic numbers? Explain good programming practice to avoid them using constant variables.Explanation / Answer
Que A
ANS:
j++ increments the value of variable j after processing the current statement.
++j increments the value of variable j before processing the current statement.
--------------------------------------------------------------------------------
Que B
Ans:
b = 2 is assignment ( value of b is 2)
b == 2 means the current value of is same as 2 . its equals to an operator
-----------------------------------------------------------------------------
QUE C
ANS:
Types of errors in C++
Syntax error: - This error occurs due to following reason.
I. Not following the grammatical rules used in the declaration of an identifier.
II. Not declaring an identifier used in a program.
III. Not terminating statement by a semicolon.
IV. Not providing an equal number of opening and closing braces etc.
These errors can be rectified by the user as it is displayed while compiling the program.
Logical error: - This error won’t be displayed on the screen. However, it will lead to display wrong results. Example: An infinite loop. This error leads to abnormal termination of a program or infinite loop.
Runtime error: - This error occurs while running a program by displaying the message listed below.
I. Division by 0.
II. Overflow
III. Underflow
--------------------------------------------------------------------------------------
Que D
ANS:
Primarily the condition statement in FOR is optional but it is mandatory in the case for WHILE.If condition statement is omitted then FOR will execute infinitely and WHILE will give you compilation error! .Generally, you wouldn't notice much difference as both is entry controlled loops, but WHILE loop is somewhat more flexible than FOR. In while loop you can insert the increment(++i) statement at any place but in for loop increment statement executes after all statements get executed(Although increment statement in FOR can be skipped in brackets and inserted at a suitable place, but that degenerates the FOR loop to WHILE and modifies the basic sense for FOR.)
-----------------------------------------------------------------------------
QUE E
ANS:
Function Call:::
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
Function defenation ::
The function itself is referred as function definition. Function definition in the above program is:
When the function is called, control is transferred to the first statement of the function body.
Then, other statements in function body are executed sequentially.
When all codes inside function definition is executed, control of program moves to the calling program.
When you define a function, you can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function.
--------------------------------------------------------------------------------------------
Que F
ANS:
Copies string s2 into string s1.
Concatenates string s2 onto the end of string s1.
Returns the length of string s1.
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
Example:::::::
----------------------------------------------------------------------------
QUE G
ANS
resetiosflags
setiosflags
setbase
setfill
----------------------------------------------------------------------------------------------------------------
QUE H
ANS
INTEGER Division
When the literal is stored in res, it is cast to an int. An int appears to be 32 bits wide on your system. The value 2147483648 cannot be represented as a 32 bit signed integer, so the cast causes an overflow. On your system, this overflow results in the value -2147483648 (likely it's using two's complement).
Floating Point divisor
Finally, when trying to perform the division at runtime (in the foo function), the SIGFPE exception occurs due to the overflow (because the int datatype cannot represent the result). The behavior of this operator depends on the type of the operands. If both operands are integers, C++ performs integer division. That means any fractional part of the answer is discarded, making the result an integer. If one or both operands are floating-point values, the fractional part is kept, making the result floating-point. Listing 3.11 illustrates how C++ division works with different types of values. As in Listing 3.10, Listing 3.11 invokes the setf() member function to modify how the results are displayed.
--------------------------------------------------------------
Que I
Ans
A Magic Number is a hard-coded value that may change at a later stage, but that can be therefore hard to update.
For example, let's say you have a Page that displays the last 50 Orders in a "Your Orders" Overview Page. 50 is the Magic Number here, because it's not set through standard or convention, it's a number that you made up for reasons outlined in the spec.
Now, what you do is you have the 50 in different places - your SQL script (SELECT TOP 50 * FROM orders), your Website (Your Last 50 Orders), your order login (for (i = 0; i < 50; i++)) and possibly many other places.
strcpy(s1, s2);Copies string s2 into string s1.
strcat(s1, s2);Concatenates string s2 onto the end of string s1.
strlen(s1);Returns the length of string s1.
strcmp(s1, s2);Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.