Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / NOTE: Please for the 2 questions submit the code in a text format and please also include the run result screenshots

NOTE: Please for the 2 questions submit the code in a text format and please also include the run result screenshots

Computer Science

NOTE: Please for the 2 questions submit the code in a text format and please also include the run result screenshots.

QUESTION 1

Let’s create a computer we’ll call the Simpletron. As its name implies, it’s a simple machine, but, as we’ll soon see, it’s a powerful one as well. The Simpletron runs programs written in the only language it directly understands, that is, Simpletron Machine Language, or SML for short. 

The Simpletron contains an accumulator—a “special register” in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number, such as +3364, -1293, +0007, -0001, etc. The Simpletron is equipped with a 100-word memory, and these words are referenced by their location numbers 00, 01, …, 99.

 

Before running an SML program, we must load, or place, the program into memory. The first instruction (or statement) of every SML program is always placed in location 00. The simulator will start executing at this location.

 

Each instruction written in SML occupies one word of the Simpletron’s memory; thus, instructions are signed four-digit decimal numbers. Assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Simpletron’s memory may contain an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the operation code that specifies the operation to be performed. SML operation codes are shown in the following table.

 

Operation code

Meaning

Input/output operations

 

const int read = 10;

Read a word from the keyboard into a specific location in
memory

const int write = 11;

Write a word from a specific location in memory to the screen

Load and store operations

 

const int load = 20;

Load a word from a specific location in memory into the
accumulator.

const int store = 21;

Store a word from the accumulator into a specific location in
memory

Arithmetic operations

 

const int add = 30;

Add a word from a specific location in memory to the word
in the accumulator (leave result in accumulator).

const int subtract = 31;

Subtract a word from a specific location in memory from the
word in the accumulator (leave result in accumulator).

const int divide = 32;

Divide a word from a specific location in memory into the
word in the accumulator (leave result in accumulator).

const int multiply = 33;

Multiply a word from a specific location in memory by the
word in the accumulator (leave result in accumulator).

Transfer-of-control operations

 

const int branch = 40;

Branch to a specific location in memory

const int branchneg = 41;

Branch to a specific location in memory if the accumulator is
negative.

const int branchzero = 42;

Branch to a specific location in memory if the accumulator is
zero.

const int halt = 43;

Halt—the program has completed its task.

 

The last two digits of an SML instruction are the operand—the address of the memory location containing the word to which the operation applies.

 

Now let’s consider two simple SML programs. The first (shown in the following table) reads two numbers from the keyboard and computes and displays their sum. The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to zero). Instruction +1008 reads the next number into location 08. The load instruction, +2007, places (copies) the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator.  The store instruction, +2109, places (copies) the result back into memory location 09. Then the write instruction, +1109, takes the number and displays it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution.

 

Location

Number

Instruction

00

+1007

(Read A)

01

+1008

(Read B)

02

+2007

(Load A)

03

+3008

(Add B)

04

+2109

(Store C)

05

+1109

(Write C)

06

+4300

(Halt)

07

+0000

(Variable A)

08

+0000

(Variable B)

09

+0000

(Result C)

 

The SML program in the following table reads two numbers from the keyboard, then determines and displays the larger value. Note the use of the instruction +4107 as a conditional transfer of control, much the same as C++’s if statement.

 

Location

Number

Instruction

00

+1009

(Read A)

01

+1010

(Read B)

02

+2009

(Load A)

03

+3110

(Subtract B)

04

+4107

(Branch negative to 07)

05

+1109

(Write A)

06

+4300

(Halt)

07

+1110

(Write B)

08

+4300

(Halt)

09

+0000

(Variable A)

10

+0000

(Variable B)

 

 

Now it may seem outrageous, but in this question you are going to build your own computer. No, you won’t be soldering components together. Rather, you’ll use
the powerful technique of software-based simulation to create a software model of the Simpletron.  Your Simpletron simulator will turn the computer you are using into a Simpletron, and you actually will be able to run, test and debug the SML programs above.

 

When you run your Simpletron simulator, it should begin by displaying
*** Welcome to Simpletron! ***
*** Please enter your program one instruction ***
*** (or data word) at a time. I will type the ***
*** location number and a question mark (?). ***
*** You then type the word for that location. ***
*** Type the sentinel -99999 to stop entering ***
*** your program. ***

 

Your program should simulate the Simpletron’s memory with a single-subscripted, 100-element built-in array memory. Now assume that the simulator is running, and let’s examine the dialog as we enter the program of the second example from above:

00 ? +1009
01 ? +1010
02 ? +2009
03 ? +3110
04 ? +4107
05 ? +1109
06 ? +4300
07 ? +1110
08 ? +4300
09 ? +0000
10 ? +0000
11 ? -99999
*** Program loading completed ***
*** Program execution begins ***

 

The numbers to the right of each ? in the preceding dialog represent the SML program instructions input by the user.

 

The SML program has now been placed (or loaded) into built-in array memory. Now the Simpletron executes your SML program. Execution begins with the instruction in location 00 and, like C++, continues sequentially, unless directed to another part of the program by a transfer of control.

 

Use variable accumulator to represent the accumulator register. Use variable instructionCounter to keep track of the location in memory that contains the instruction being performed. Use variable operationCode to indicate the operation currently being performed (i.e., the left two digits of the instruction word). Use variable operand to indicate the memory location on which the current instruction operates. Thus, operand is the rightmost two digits of the instruction currently being performed. Do not execute instructions directly from memory. Rather, transfer the next instruction to be performed from memory to a variable called instructionRegister. Then “pick off ” the left two digits and place them in operationCode, and “pick off ” the right two digits and place them in operand. When Simpletron begins execution, the special registers are all initialized to zero.

 

Now let’s “walk through” the execution of the first SML instruction, +1009 in memory location 00. This is called an instruction execution cycle.

 

The instructionCounter tells us the location of the next instruction to be performed. We fetch the contents of that location from memory by using the C++ statement

instructionRegister = memory[ instructionCounter ];

 

The operation code and operand are extracted from the instruction register by the statements

operationCode = instructionRegister / 100;
operand = instructionRegister % 100;

 

Now, the Simpletron must determine that the operation code is actually a read (versus a write, a load, etc.). A switch differentiates among the 12 operations of SML. In the switch statement, the behavior of various SML instructions is simulated as shown in the following table (you can come up with more):

read:

cin >> memory[ operand ];

load:

accumulator = memory[ operand ];

add:

accumulator += memory[ operand ];

branch:

see below discussion

halt:

This instruction displays the message
*** Simpletron execution terminated ***

 

The halt instruction also causes the Simpletron to display the name and contents of each register, as well as the complete contents of memory. Such a printout is often called a register and memory dump. To help you program your dump function, a sample dump format is shown below. Note that a dump after executing a Simpletron program would show the actual values of instructions and
data values at the moment execution terminated. To format numbers with their sign as shown in the dump, use stream manipulator showpos. To disable the display of the sign, use stream manipulator noshowpos. For numbers that have fewer than four digits, you can format numbers with leading zeros between the sign and the value by using the following statement before outputting the value:

cout << setfill( '0' ) << internal;

 

 

 

Parameterized stream manipulator setfill (from header ) specifies the fill character that will appear between the sign and the value when a number is displayed with a field width of five characters but does not have four digits. (One position in the field width is reserved for the sign.) Stream manipulator internal indicates that the fill characters should appear between the sign and the numeric value .

 

Now proceed with the execution of our program’s first instruction—+1009 in location 00. As we’ve indicated, the switch statement simulates this by performing the C++ statement

cin >> memory[ operand ];

 

A question mark (?) should be displayed on the screen before the cin statement executes to prompt the user for input. The Simpletron waits for the user to type a value and press the Enter key. The value is then read into location 09.

 

At this point, simulation of the first instruction is complete. All that remains is to prepare the Simpletron to execute the next instruction. The instruction just performed was not a transfer of control, so we need merely increment the instruction counter register as follows:

++instructionCounter;

 

This completes the simulated execution of the first instruction. The entire process (i.e., the instruction execution cycle) begins anew with the fetch of the next instruction to execute.

 

Now let’s consider how to simulate the branching instructions (i.e., the transfers of control).  All we need to do is adjust the value in the instructionCounter appropriately. Therefore, the unconditional branch instruction (40) is simulated in the switch as

instructionCounter = operand;

 

The conditional “branch if accumulator is zero” instruction is simulated as

if ( 0 == accumulator )
    instructionCounter = operand;

 

At this point, you can implement your Simpletron simulator and run each of the SML
programs above. The variables that represent the Simpletron simulator’s memory and registers should be defined in main and passed to other functions by value or by reference as appropriate.

 

Your simulator should check for various types of errors. During the program loading phase, for example, each number the user types into the Simpletron’s memory must be in the range -9999 to +9999. Your simulator should use a while loop to test that each number entered is in this range and, if not, keep prompting the user to reenter the number until the user enters a correct number.

 

During the execution phase, your simulator should check for various serious errors, such as attempts to divide by zero, attempts to execute invalid operation codes, accumulator overflows (i.e., arithmetic operations resulting in values larger than +9999 or smaller than -9999) and the like.  Such serious errors are called fatal errors. When a fatal error is detected, your simulator should display an error message such as

*** Attempt to divide by zero ***
*** Simpletron execution abnormally terminated ***

and should display a full register and memory dump in the format we’ve discussed previously. This will help the user locate the error in the program.

 

 

 

 

 

 

 

 

 

 

NOTE:  Please for the 2 questions submit the code in a text format and please also include the run result screenshots.

 

QUESTION 2

In this question, you’ll re-create the classic race of the tortoise and the hare. You’ll use random number generation to develop a simulation of this memorable event. 

 

Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.


There is a clock that ticks once per second. With each tick of the clock, your program should use function moveTortoise and moveHare to adjust the position of the animals according to the rules in the following table. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare.

 

Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70).  Start each animal at position 1 (i.e., the “starting gate”). If an animal slips left before square 1, move the animal back to square 1.


Generate the percentages in the preceding table by producing a random integer i in the range 1 ≤i ≤10. For the tortoise, perform a “fast plod” when 1 ≤ i ≤5, a “slip” when 6 ≤ i ≤7 or a “slow plod” when 8 ≤ i ≤10. Use a similar technique to move the hare.
Begin the race by displaying


BANG !!!!!
AND THEY'RE OFF !!!!!


For each tick of the clock (i.e., each repetition of a loop), display a 70-position line showing the letter T in the tortoise’s position and the letter H in the hare’s position. Occasionally, the contenders land on the same square. In this case, the tortoise bites the hare and your program should display OUCH!!! beginning at that position. All positions other than the T, the H or the OUCH!!! (in case of a tie) should be blank.


After displaying each line, test whether either animal has reached or passed square 70. If so, display the winner and terminate the simulation. If the tortoise wins, display TORTOISE WINS!!!  YAY!!! If the hare wins, display Hare wins. Yuch. If both animals win on the same clock tick, you may want to favor the tortoise (the “underdog”), or you may want to display It's a tie. If neither animal wins, perform the loop again to simulate the next tick of the clock.

 

Animal 

Move type

Percentage of the time

Actual move

Tortoise

Fast plod

50%

3 squares to the right

 

Slip

20%

6 squares to the left

 

Slow plod

30%

1 square to the right

Hare

Sleep

20%

No move at all

 

Big hop

20%

9 squares to the right

 

Big slip

10%

12 squares to the left

 

Small hop

30%

1 square to the right

 

Small slip

20%

2 squares to the left

 

 

Option 1

Low Cost Option
Download this past answer in few clicks

15.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions