. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver a Question
Answered Questions on algorithms and control structures
I personally think you should never use the "do-while". Just an opinion based on 30 years experience.... I used them from 1964 through to 1978 and then abandoned them and started to have fewer bugs.
Pitfalls of the do-while
Basically having to write code in the do-while that works
whether or not the condition is true.
Also changing a problem with zero-repetitions to one where there must be at least one repetition.
What is the purpose of Pseudocode
To be able to think about a solution of a problem without worrying about
the C++ syntax.
What is the reason for nested control statements
Because we have nested problems, of course:-)
Seriously: In the lab I showed you a program that output a grid of symbols. The structure is (
In general most probems demand a complicated algorithm to solve them... one where a selection has to be repeated, or where we have to choose between two loops. We can have solutions where a repeated pattern has to (itself) be repeated.
Is the member function determineClassAverage a C++ keyword
No. In fact, only keywords that are operators can be redefined as member functions.
Don't do this in CS201!
Is the member function determineClassAverage assigned a value
No. Functions are not assigned values... you must define them. So
we need to include a definition like this somewhere in our compilation:
void GradeBook::determineClassAverage()
{
//whatever
}
Should I use ++ and -- rather than writing the assignment out
Yes!
Don't forget it is simpler to type
myLongNamedCounter++;than
myLongNamedCounter=myLongNamedCounter+1;
Pre and post increment operators
Table
| Before | Operation | After | Value returned | |
|---|---|---|---|---|
| v is v0 | v++ | v is v0+1 | v0 | |
| v is v0 | ++v | v is v0+1 | v0+1 | |
| v is v0 | v+1 | v is v0 | v0+1 | |
| v is v0 | v-- | v is v0-1 | v0 | |
| v is v0 | --v | v is v0-1 | v0-1 | |
| v is v0 | v-1 | v is v0 | v0-1 |
The wise programmer uses "++" and "--" very simply to add and subtract 1 like his
v++;and avoids complicated trickery like this:
v= (++v) * (v--);which takes a lot of thought to get right and even more thought to figure out.
Do assignment operators help or just be confusing
Both. It depends on experience and the precise use.
Many people use them and as a result you have to be able to read them.
Usually it does not make much difference. But when you have a long variable and want to add 2 to it:
myObject.forExampleAttribute0+=2it is too easy to mistype the expanded version:
myObject.forExampleAttribute0= myObject.forExampleAttributeO+2
If output shows an object to have value zero does it mean it wasn't allocated a value
Not really, it is easy for a variable to become zero as the result of a complex
calculation.
Also -- there is no guarantee that ints and doubles are set to zero initially.
Can you set a repetition to terminate at any value
Yes.
Is it better to use a merge symbol in UML for multiple activities
When the UML activity diagram or flowchart shows a choice of two or more
alternatives... it should show the end of the selection with merge "---<>--" symbols.
What is the purpose of a sentinel symbol if it is not acceptable value
It's value is that it can not be used as input to the algorithm and so it
can be used to signal the need to move on to something else.
How does float differ from double
Floats don't use as many binary digits, are more approximate, and cover a smaller range of
real numbers.
Examples of conversions
[ convert.cpp ]
Explain implicit and explicit conversion
In C++ you can often convert data of one type to another type -- for example
converting an integer into a double. If you use a "cast" then it is explicit.
In an implicit conversion, you allow the compiler to choose a conversion from one number type to another when necessary.
Notice that if you write
double answer = 1/2;the division is int division and so answer is set to zero. However, if one of the divisors was a double
double answer = 1/2.0;then we get answer set to 0.5. Neither does "double(1/2)" work. The division is done first (using int division) and then is converted to double. :! As a rule it is wise to use explicit conversions.
char(32)(in ASCII a space character). Similarly to get an accurate average from the sum of a number of int's we used to write:
average = double(sum)/number;This still works but is deprecated (jargon for: "will not work forever").
The more modern and complex form is shown on line 81 of the listing on page 158 of Deitel:
average = static_cast<double>(sum)/number;this should continue to compile and run correctly even if the standard changes. And so this is the form you should use:-(
Similar code can handle
other numeric data and let you choose the formatting.
How do you output numbers to only 2 decimals
Use an IO Manipulator like this
cout << .... << setprecision(2) << ....;
[ ../examples/giomanip.cpp ] [ ../examples/iomanip.cpp ]
This changes the format of doubles for the whole prgram.... until you change it to some other
precision.
Avoiding infinite loops
The only way to avoid infinite loops is to think. You must be sure,
or better, must have proved that each time through the body, you
are closer to the terminating condition. For example, if you are
counting up, you condition should be counter < some_limit. If
you are going down then the condition will be counter > some_value.
Avoid using equality tests, by the way, in case the variable jumps over
the limit. This also stops you type variable=limit when you want
variable==limit.
The example in the book showed the need for braces {...} to include the commands that change the controlled variable in the body of the loop.
It is important that you understand the syntax of loops and use braces when needed. Indeed I tend to put them in in every loop, just in case.
But, there is no replacement for thought when coding loops and selections!
Why can uninitialized variables lead to errors
Because 'int's and 'double's are not set up to be any particular value.
So if you are adding up data in an uninitialized variable the results
are unpredictable, and probably wrong.
How do I make sure the user has given me one the small number of numbers I expect
This is a
Data Validation
problem. Let us assume that the user must input either 1, 2, or 3. Also assume
that the user know enough to not input anything but an integer (not Roman "III", or
a real number "1.234". Then we need to
(
This looks like a loop -- but how can we check the loop condition?
Here is a standard way to tackle it using the tools covered in the course so far.
int valid = 0; // 0 means invalid, 1 means valid
while(valid==0)
{ cout << "Input either 1, 2, or 3: ";
cin >> number;
if (number==1)
valid=1;
else if (number==2)
valid=1;
else if (number==3)
valid=1;
if(valid==0)
cout << number << " is invalid\n";
}Using Boolean Expressions and variables from Chapter 5 makes this much easier!