 |
|
 |
 |
 |
 |
 |
|
|
|
|
|
Assignment 3 - Creating a Mental Arithmetic
Testing Program
Introduction
The Assignment
- Mental Arithmetic Tester
Step1 - Welcome Message
Step 2 - Input and Output
Step 3 - Using Variables
and Constants
Step 4 - Creating Character
Arrays
Step 5- Using Functions
Step 6 - Validation
Step 7 - Operators
Step 8- Conditional Statements
Step 9 - Loops
Step 10 - Local and
Global Variables
Step 11 - More on Functions
Step 12 - Testing your
Program
Introduction
For twelve weeks of this course, we
will be working step-by-step through assignment 3. This will familiarize
you with the C++ language and concepts and give you practice in organizing
the structure of a program.
You will need the assignment
paper.
The
Assignment - Mental Arithmetic Tester
Briefly, the scenario is:-
To create mental arithmetic testing
program. The program must be run in the command console and display
a series of multiple choice questions. The user is given a final score
and an opportunity to repeat the test or repeat only those questions
incorrectly answered.
For the full scenario - please read
the paper.
To see the program running, you may
download my program executable if you wish.
Step
1 - Welcome Message
When your program
first starts, it should display a welcome screen.
-
Create a project and source code file. Call it something appropriate.
-
Include the library file iostream.h . Create a function
called main .
- Using
cout , write
code for displaying a welcome message on screen when your program starts.
If you get a compiler error - indicating that cout is
an undeclared identifier, then change the line...
#include <iostream.h>
to the following two
lines...
#include <iostream>
using namespace std;
Step
2 - Input and Output
When your program
first starts, after displaying a welcome screen there should be an invitation
for the user to press ANY key in order to continue.
-
Investigate
the following functions too see which method you want to use to achieve
this:-
system("pause"); getch(); cin cin.getline
Note: You may
get some strange behaviour from the system("pause")
and getch() functions - pausing right at the start of
your program, even if it is supposed to pause later on. If so, make
sure you have included the following lines of code...
#include <iostream>
using namespace std
If you use getch you will need
to include the library file conio.h . If you use system("pause")
you will need to include the library file stdlib.h.
-
Using one of these functions, insert the code into main
so that your program waits for the user to press ANY key before continuing.
-
On depression of any key, using cout ,
display the first test question to the user. All the questions for
the test are in the appendix of the assignment paper. Here is the
first one:-
Question 1
123 – 39 =
1 64
2 44
3 74
4 84
-
The brief is for the question to be laid out towards
the centre of the display screen. You will need to experiment with
the escape characters \n , \t and endl
to get the layout similar to mine.
-
Use cin to get the users answer.
-
Once you are happy with the layout of your first
question, type in code to display a second question. Use cin
to get the users answer again.
- Add comments to your code.
Step
3 - Using Variables and Constants
You may have noticed that you have
no way of keeping a record of the users score at present. To fix this
you will create some variables.
- To practice creating variables of the appropriate
type, create the variables listed below. Declare them just before the
main function. You will be using them in later exercises.
Make sure you add comments.
score - integer for holding
the users score
fScore - float for working
out the users score as a percentage
- Pretend that the user gets 1 out of 2 questions
correct. Using the variables you created, write a few lines of code
to display the users score in the format:-
Your total score is: Number of correct questions / 10
Your percentage is: Score calculated as percent %
Step
4 - Creating Character Arrays
You have no way of keeping a record
of the users answer at present. To fix this:-
- Create a
char array variable with say
255 elements in which to store the users answers and change your code
appropriately. I.e.
cin >> userAns;
Check your code is working properly by echoing the
users answer to the screen. I.e.
cout << userAns;
Remove this line when you are happy that everything
works ok.
- To practice creating arrays, create the following
variables. Declare them just before the
main function.
You will be using them in later exercises. Make sure you add comments.
correctAnswer[2] - character array
to hold the correct answer number for a question
ansRecord[NUMQ] - integer array to
hold record of users answers, element value of 1 means answer is
correct
Step
5 - Using Functions
You may have noticed that your code
is getting disorganized. To fix this we will start using functions to
organize code.
- Create a new C++ header file in which to declare
your function
prototypes . At the top of your .cpp
source code file, type in the #include directive to include
your header file.
- Create the following function in your source code
file and move your code for displaying the users score into this function.
void displayScore (void)
Call this function from an appropriate place in
the main function.
Don't forget to declare the function prototype .
Compile, link and run your program to check that everything works.
- Now create the following function and move your
code for displaying the questions into this function.
void displayQuestions(void)
Call this function from an appropriate place in
the main function. Don't forget to declare the function
prototype . Compile, link and run your program to
check that everything works.
Step
6 - Validation
You we be creating more of your own
functions to organize your code. You will also be using some pre-defined
functions to manipulate strings and character arrays.
- Create the following function.
int getAns(void)
Don't forget to declare the function prototype .
We want this function to get the users answer. In step 4, you should
have created the variables correctAnswer and userAns .
Move the line
cin >> userAns;
from main into this function and call
the getAns from main .
- When the user types in an
answer to a question, the entry must be in the form of the letters
1, 2, 3 or 4. These entries must be validated.
An incorrect entry must cause an error message to appear on screen
and a request for re-entry of the selection letter. To do all this,
create the following function:-
int validateAnswer(void)
Don't forget to declare the function prototype .
In the function, write code to check only one character
was input by the user using the pre-defined strlen function.
The function should return the length of the string which you should
store in a variable called numChars .
In the function, write code to check for an integer
input between 0 and 5. You should use the pre-defined function atoi
to convert the string stored in userAns to an integer
value. The atoi function will let you know if it cannot
convert the string to an integer by returning 0. Catch the value returned
in a variable called numChars .
Using the return value of the atoi function,
we need to write code to check its value is one of the numbers, 1,
2, 3, or 4. We shall do that bit in step 8.
Finally, the validateAnswer should return
a 1 if the user input was valid and a 0 if it was invalid but we shall
also do that bit in step 8.
You should call the validateAnswer function
from inside the getAns function.
- Now create the following function.
int compareAns(void)
Don't forget to declare the function prototype .
We want this function to compare the users answer against the correct
answer for a particular question. In step 4, you should have created
the variables correctAnswer and userAns .
Using those variables, write code in this function to compare the two
variables. The function should return the number if the variables the
hold the same answer and 0 if they don't.
Note: You can use the pre-defined
strcmp function to compare two strings.
Step
7 - Operators
You will now use the equality and
increment operators in your code.
- In the
compareAns you should have
the strcmp function comparing the users answer with the
correct answer. Change the code as follows:-
if (strcmp(userAns, correctAnswer) == 0)
return 1;
else
return 0;
Notice the use of the equality operator. Compile, link
and run your program to check that everything works.
- In my main function I have a variable called
questionCount
which is incremented every time a question is asked of the user. I.e.
questionCount++;
Find an appropriate place in your code to put this
line and don't forget to declare it as well.
Step
8 - Conditional Statements
You will now include if...else
and switch statements in your
code.
- We need to finish off the
validateAnswer
function that we started in step 6, so that the function returns a
1 if the user input was valid and a 0 if it was invalid.
To do this first declare a integer variable called validate
and initialize it to 0.
then include an if...else statement as follows:-
if (numChars == 1) validate = 1;
else validate = 0;
You should also have already created some code that gets the integer
value of the users answer back using the atoi function.
Now you need to check for an integer input between 0 and 5. So include
an if...else statement that sets validate
to 0 or 1 depending if the input is between 0 and 5 or not.
Now return the value of the validate variable
from the validateAnswer function. Compile, link and run
your program to check that everything works.
- You should have created a
function called
displayQuestions in step 5. You will
have probably written out a series of questions to be displayed to
the user. But how do you control which question is displayed at any
one time in your program? One way is to use the switch
statement. First change your displayQuestions declaration
as follows:-
int displayQuestions(int
qNumber)
Don't forget to update
the prototype.
Inside this function you should use the switch
function. To start you off I will give you a little bit of my code:-
switch( qNumber )
{
case 1:
strcpy (questionBuff, "\n\t\tQuestion 1:\n\t\t123 - 39
=");
strcpy (opt1Buff , "64");
strcpy (opt2Buff , "44");
strcpy (opt3Buff , "74");
strcpy (opt4Buff , "84");
strcpy (correctAnswer,"4");
break;
Finish off this switch
function by including at least one more question and a default case.
You should also declare the variables questionBuff
etc. at the top of displayQuestions function. Here is
one variable declaration, you do the rest:-
char questionBuff[255];
Change any cout statement
you have, that display the questions and answers to the user, so that
the appropriate variables are used instead. Here is one line as an
example, you do the rest:-
cout<< questionBuff << endl;
Call the function from main, using a number to indicate
which question you want displayed. I.e.
displayQuestions (1);
Compile, link and run your program to check that
everything works.
Step
9 - Loops
You will now include loop statements
in your code.
- When you check the users answer using
validateAnswer ,
what should you do if the input is invalid? You should let them input
again. To do that, wherever you are calling the validateAnswer
function from in your code, insert an if statement as
follows:-
if (validateAnswer() != 1)
{
cout << "Invalid input!\n" << "Enter
1 to 4 to specify the correct answer." << endl;
}
You need to put this inside a do...while
loop, so that the user has to enter input until it is valid. Do that
then compile, link and run your program to check that everything works.
- I expect you call the
displayQuestions
function in main a number of times. I.e.
displayQuestions(1);
displayQuestions(2);
Well what if you have lots of questions to display? You could keep
on adding displayQuestions lines but it is simpler to
use a loop. So, add a loop to you code so that you only have one displayQuestions
line but all of your questions are displayed in turn.
You will need to move your getAns and
compareAns functions inside this loop too. You will also
need to update the score variable inside the loop.
- In the function
main , you need to update
the score variable every time the user chooses the correct
answer. Now, every time you call the compareAns function,
it returns a value of 1 if a question was answered correctly and 0
if it was answered incorrectly. So all we have to do to update score
is write a line such as:-
score = score + compareAns;
Step
10 - Local and Global Variables
So far you have been creating variables
without considering if they are local or global. Most of them will probably
be global. Your task now is to try to eliminate as many global variables
as you can and make them local.
- You created a global
score variable
in an earlier exercise. Move the declaration of the variable into main .
You also created a function called displayScore that needs
to access to the score variable. Try compiling your code
and you will see an error message displayed now because displayScore
cannot access score . However, instead of changing score
back to global, change the declaration of displayScore
as follows:-
void displayScore (void)
to
void displayScore (int ascore)
and change all references to score inside the function
to ascore . Don't forget to change the function prototype .
When you call displayScore from main ,
you can just pass the score parameter. I.e.
displayScore (score)
Compile, link and run your program to check that everything works.
- Now look through the rest of your code and change
as many global variables as possible to local variables. I wouldn't
expect more than a handful of global variables.
Step
11 - More on Functions
Here is an example of a function that
passes a character array variable by reference:-
- I have the following function in my program, that
displays any incorrectly answered questions to the user:-
void showIncorrectAns (int score,int record[])
{
/* If score is less than the number of questions,
display the questions incorrectly answered */
if (score < NUMQ) // check if score is less
than number of questions
{
cout << "The questions incorrectly answered
were:" << endl <<endl;
//loop through record of answers,
//if element value is 0 question was incorrectly answered
//if element value is 1 question was correctly answered
for (int i = 0;i < NUMQ; i++)
{
if (record[i] == 0)
{
cout << "\t\t\t\t\tQuestion
" << i+1 <<endl <<endl;
}
}
}
else
cout << "You answered all the questions correctly:"
<< endl;
}
Is the record[] parameter passed to the function by
value or by reference?
You can use this function in your own code if your wish. However,
you will need to update the record[] array every time
the user input the answer to a question somewhere in your code.
Step
12 - Testing your Program
However far you have got with this
practice assignment, it is time to test the program. We shall carry
out black-box testing where the expected outputs are
compared with actual outputs.
- Create a testing table with at least 5 test cases.
When creating each test case you should consider combinations of inputs
where incorrect and correct answers are entered, invalid inputs are
entered or the user wants to retake the test etc.
Thats
it!
|
|
|
|