Problem Solving - Page 2
Variables
Include data table in design. This should give a valid variable declaration that states its type. Choose an identifier. this should reflect the purpose of the variable.
Identifiers - parameters
- Up to 32 characters (compiler will not recognise any more than 32).
- Begin with a letter
- Subsequent characters - letters, underscore and digits 0-9
- Identifiers are case sensitive
- Keywords cannot be identifiers
Main variable types
- Integers i.e. whole numbers - positive or negative. Integer division always rounds towards zero e.g. (11/4)=2 - actually 2.75 rounded to 0. (-11/4)=-2 for reasons as above.
- Real Numbers - digits either side of decimal point. Positive or negative. Any size you like! Floating Point - 5.9743000000E2 where 5 is the mantissa and the 2 is the exponent.
- Characters - as laid out in the ASCII table.
- Strings - a sequence of characters.
- Boolean variables - true or false. This will depend on the condition of the expression.
More to come on these - I think!
Mean Problem
Loops - Extend the four number mean program to read in fifty numbers. Let the machine do the counting using a loop expression. (A repeated sequence) a top level design would be:
- set up loop
- loop while there are more numbers
- process next number
- loopend
- determine mean value
The count starts at zero. On every loop test for count = 50. One is added to the count for every loop. If count = 50 loopend - if not then loop.
| Type | Identifier | Description |
| Integer | Count | Counts the numbers that are input |
| Integer | Total | Records the total value of the numbers that are input |
| Integer | NextNumber | The next number to be input |
| Real | Mean | The output value of total divided by fifty |
Count must be initialised to 0. Total must be initialised to 0.
What happens if there are an undetermined number to be input? One solution is to provide a sentinel value. A predetermined signal to end the loop. This is dependent on the user knowing the sentinel value! Program must prompt for input of the sentinel value to terminate the program.
Testing - Format of the if step -
- if condition then
- action for then branch
- else
- action for else branch
- ifend
Loops and Selection
The if step
if (condition)
{
statement 1;
statement 2;
...;
}
else
{
statement 1;
statement 2;
...;
}
- The word then is not in C++, it is only used in design.
- Condition in brackets () is any boolean expression.
- then and else are delimited by braces {} (if more than one statement) containing C++ statements (then clause and else clause).
- Many if steps require else action of nothing - omit keyword else and statement.
- More than two statements are a compound statement.
- A compound statement is always in braces with no semi-colon after the final brace.
The while step
while (condition)
{ single or compound statement }
- Condition in brackets (), is any boolean expression giving true or false results.
- Loop body is a single or compound statement.
- As with if-else a compound statement is always in braces with no semi-colon after the final brace.
C++ Code for extended Mean
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int Count;
int Total;
int NextNumber;
float Mean;
Count = 0;
Total = 0;
NextNumber = 0;
while(NextNumber != -1)
{
NextNumber = ReadIntPr("Enter next number or -1 to stop: ");
if(NextNumber != -1) //Do not put a semi-colon here!
{
Total = Total + NextNumber;
Count = Count + 1;
}
}
Mean = float(Total)/Count;
WriteFloatPr("The mean value is ", Mean);
getchar();
return 0;
}
//---------------------------------------------------------------------------
The above code section includes: if statement, while statement and a sentinel value.
Attention to correct syntax is crucial. If the line:
if(NextNumber != -1)
was to be followed by a semi-colon, the program would compile and run, but the results would be totally wrong.
The code below was a practical exercise to design and code to measure sequence lengths in coin tosses. I did'nt get it right first time! I missed out this section and a corresponding closing brace further down.
if (Toss != 'Q')
{
The code compiled and ran but seemed to count the 'Q' to Quit as an extra in the Total giving a wrong result.
int main(int argc, char* argv[])
{
int SequenceNumber;
int Total;
char LastToss;
char Toss;
float Mean;
SequenceNumber = 0;
Total = 0;
Toss = 'Z';
LastToss = 'Z';
while(Toss != 'Q')
{
Toss = ReadCharPr("Enter next toss H or T (or Q to Quit): ");
if (Toss != 'Q')
{
Total = Total + 1;
if (Toss != LastToss)
{
SequenceNumber = SequenceNumber + 1;
LastToss = Toss;
}
}
}
Mean = float(Total)/SequenceNumber;
WriteFloatPr("The mean sequence length is ", Mean);
getchar();
return 0;
Comments, suggestions, ideas to
Stuart Banner
