MT 262 - Block 1 - Unit 2 - Problem Solving
fellstrider.com - the logo!
Home| OU Study Rooms | MT262 Index | Block 1 - Beginnings
 
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

Main variable types

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:

  1. set up loop
  2. loop while there are more numbers
  3. process next number
  4. loopend
  5. 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 -

 
Loops and Selection

The if step


if (condition)
{
	statement 1;
	statement 2;
	...;
}
else
{
	statement 1;
	statement 2;
	...;
}

The while step


while (condition)
	{ single or compound statement }

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;
Home| OU Study Rooms | MT262 Index | Block 1 - Beginnings
Back to Problem Solving Page 1
Move on to looping and branching

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner