P2 - demonstrate the use of procedural tools and techniques


P3 - design a procedural application to meet defined requirements

P4 - implement a working procedural application to meet defined requirements

M2 - create on-screen help to assist the users of a computer program

U6 P3 - Explain sequence, selection and iteration as used in computer programming

U6 P4 - Outline the benefits of having a variety of data types available to the programmer

--------------

The program generates 10 random multiplication questions. It is a console application, the user would open the exe and would be greeted and shown the menu, they would then enter a number as an option and press enter. This would then perform a task based on their option. 1 would begin the multiplication test. 2 would output instructions and 3 would exit the application. Answers would be entered with number keys on the keyboard and then the enter key. A mouse would not be required.

The inputs of the program will be the menu selection option and the answers to the question.

The outputs of the program will be this:

First they will get a welcome message, then the menu will be displayed, pushing 1 will make the test start, if they answer wrong they will be told, and if they are correct they will be told, if they enter a number out of the range they will have to try again.

Option 2 will give them help or instructions.

Once they have done 10 questions they will be given their score and will return to the menu. Then they can try again or quit.

Name

Type

Reason and purpose

Starting value

userOp

Global, int

Store users menu selection, global so it can be used anywhere.

0

iNumber1

Global, int

This will store one of the random numbers used for the multiplication.

0

iNumber2

Global int

This will store the other random number used for the multiplication.

6

input

Global, int

This will store the users input for an answer.

0

answer

Global, int

This will store what the correct answer is and check it against input.

0

score

Global, int

This variable will hold the users total score.

0

loops

Local, int

0

Type Name

Bytes

Range of Values

Reason for choice

string

Depends on number of character

Was not necessary

int

4

-2147483648 to 2147483647

Most suitable as only numbers were being used in program.

double

8

1.7E +/- 308 (15 digits)

Only whole numbers were used in the test

Char

1

-128 to 127

Wasn’t necessary as numbers could be used.

bool

1

0 to 1

Good

A string could be used in forms and used to personalise letters for people by just changing the name. A string would be used by writing string and then naming the string followed by a semi-colon eg, string s1; you could then put some words into the string such as s1 = “This is string one”; or you could get the user to do it with cin >> s1; The string could then be outputted any time by outputting s1.

An int could be used to store a number, you can get short int variables as well which use less memory, they could be useful in devices with little memory such as toys. Int would be used as int iOne = 5; or you could get the user to input a value to iOne. This could be the users age or even a selection.

The double variable is used for decimals and large numbers, It can produce the most numbers however it won’t always be spot on accurate and will make some mistakes eventually but it is the best c++ has.

The char variable can store a character, it could be useful for menu selection in my program. It is used in the same was as int but a letter has to be stored.

Bool is used for True or False and is used to make decisions when a condition is met or when it’s not. Bool is used as Boolean statements, and example is if (numOne == numTwo) { cout << “both are equal”; The bool statement is the part in these brackets ().

To make the program I will first open the IDE, I will use visual studio. I will then create a new c++ console application, I will then add a blank .cpp for me to write the program on. First of all I will include the directives used in the program such as #include <iostream>, this will allow standard c++ operations, any others used will allow more operations to be used to create a more complex program. I will then write ‘using namespace std; as I will be writing standard c++ throughout the program and it saves typing. I will then declare global variables, these will work anywhere In the program. I will then create a few functions, I will use the void function to output a welcome message. I will then leave a gap for functions I may add later. I will then create the main function. I will then output a menu for the user in a loop and will then use a switch statement so the selection will do something. I will create an input so the user’s menu choice can be taken. In the switch statement I will need to make the multiplication tester. I will need to make the two number variables random so the questions the user gets will be different each time, I will do this in a for loop so I can prevent the program from making random numbers bigger than 10 and from continuing if the user enter a number to large or small. I will then create the answer and create a function to check whether the answer is right or wrong, tell the user and add up the score. I will then make case 2 of the switch statement which will give the user help. Case 3 will quit the program, I will also add a default in case the user enters the wrong number, this case will tell them they entered an invalid option and the loop will start again. Finally their score will be outputted and they will return to the main menu where they can try again or quit.

I used the void function to output a welcome message. void printmessage() // A function to print this message when called

{

cout << "Welcome to multiplication test V2.0643\n" << endl;

}

This is declaring the function, then to use it, it has to be called. Once it is called it will run what is in between the braces which outputs the message. Here is the function being called (inside the main function) printmessage();

I made another function to add up the users score and tell them whether they got the question correct or not..

int totalScore() // A function to add up the users score

{

if (input == answer)

{

cout << "Correct " << endl;

score++;

}

else

{

cout << "Incorrect " << endl;

}

return score;

}

Because I used global variables they can be used in the function and anywhere else in the program.

I used a while loop with a for loop inside which had a do while loop inside. The first while loop was used to loop the whole program once it’s finished so it returns to the menu, from there they can push 3 to quit. The for loop was used to create the multiplication part of the program, it was the best loop as I could control the amount of loops to 10 so 10 questions were asked. I did this with’ for (int loops = 0; loops < 10; loops++)’. Finally a do while loop was used to output an error when an invalid number was entered and stop them from getting a wrong answer for anything over 100 or less than 1. do {

cin >> input;

if (cin.fail()) { // again this is used so that only numbers are entered

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

if (input > 100 || input < 1) // This is so that only numbers between 0 1 and 100 can be inputed

{

cout << "Re-try, enter a number equal to or less than 100 and more than 0.\n";

}

} while (input > 100 || input <1); // This is so that the program does not continue if they enter a number out of the specified range.

It says to do the following while the condition at the bottom is true (met). The condition here is that a number less than 1 and more than a hundred is entered. When the condition is met an error message is shown. If the condition isn’t me then the program continues and ignores it.

I used one selection statement to make a choice in the menu. Switch statements allow more options to be chosen and take the user down different paths.

switch (userOp) // A switch statement to make the users choice happen

{

case 1:

for (int loops = 0; loops < 10; loops++)

{

iNumber1 = rand() % 10 + 1;

iNumber2 = rand() % 10 + 1;

cout << iNumber1 << " x " << iNumber2 << endl;

do {

cin >> input;

if (cin.fail()) { // again this is used so that only numbers are entered

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

if (input > 100 || input < 1) // This is so that only numbers between 0 1 and 100 can be inputed

{

cout << "Re-try, enter a number equal to or less than 100 and more than 0.\n";

}

} while (input > 100 || input <1); // This is so that the program does not continue if they enter a number out of the specified range

answer = iNumber1*iNumber2;

totalScore(); // calling the function to output the total socre

}

break;

case 2: // this is option two for if they want to see instructions

cout << "You will be given a multiplication question and you will need to type the answer as an integer and then press enter. Another question"

<< " will appear and you will return to the menu after 10 questions.\n" << endl;

break;

case 3: // the third option (to exit)

system("exit");

return 0;

break;

default: // This shows when they enter a wrong number for the menu selection

cout << userOp << " Is not a valid choice, Enter a number\n";

break;

}

Pre-Defined Functions

I used srand to create a random number; it works by seeding random numbers which can then be multiplied. I used it as srand(time(NULL)); The random number is seeded and shouldn’t be the same each time (being random).

My program is run in a sequence, first the main function is run in order and then the other functions when they are called. Things are outputted in a logical order, the welcome message is outputted first, then the menu and so on.

-----------------