Question: Starter Code: #include Alphatron.h #include int main ( int argc, char * argv [ ] ) { Alphatron a; a . read _ program

Starter Code:
#include "Alphatron.h"
#include
int main(int argc, char *argv[]){
Alphatron a;
a.read_program();
a.run_program();
return 0;
}
TASK
Your task is to implement a simulation of the Alphatron-001. The simulated computer will read a program from cin, and will execute it when the special word RUN is entered. This means that you can interactively type in a Alphatron program to your simulation program, and enter RUN to run the program you have entered.
All Alphatron programs consist of a series of 4-digit numbers, one number per line, and have the file extension .alp. A few example programs have been provided (see below).
You will complete the task by implementing Alphatron.cpp, following the specification given in Alphatron.h.(The implementation of the read_program() method has already been provided.) To test your program, run the main method in main.cpp, and enter a program, followed by RUN.
You should not modify main.cpp for this task. You may need to add methods to Alphatron.h and Alphatron.cpp.
You are also given an implementation of the method memory_dump(), which prints out a dump of the current values in memory. You are not required to use this method, but it may be useful for you for debugging purposes.
(NOTE: Because of a small design error, and also in order to facilitate programming by human programmers, the Alphatron cannot access a program statement located at memory location 0, and cannot store data there either. Hence, all Alphatron machine code programs start running at the instruction stored in the 2nd location in memory, i.e. at memory location 1.)
Of course, it's tedious to type in a whole program line by line every time you test. A more convenient way is to store your program in a text file. Then you can either copy and paste the program from the file into the Alphatron as input, or you can redirect the input into your Alphatron program to get cin from the file instead of the keyboard, as we've done in previous programming tasks. You can even, if you really want to automate everything, add the word RUN to the end of the file, as well as any input values!
The following example programs have been provided:
The program countto4.alp prints the value of the accumulator, then repeatedly increments the accumulator and prints it out, 4 times in succession. The result is that it prints
0
1
2
3
4
The program inputplus2.alp inputs a number from the keyboard into the accumulator, increments it twice (in the accumulator), and then prints it out. The result is that it prints out the input number +2.
The program sum.alp reads in two numbers from the keyboard, and prints out their sum.
The program maxof2.alp reads in two numbers from the keyboard. If the first one is larger, it prints 1; if the second one is larger, it prints 2, and if they are equal, it prints 0.
Level 3
(For levels 3 and 4, your task is to write programs in the Alphatron machine language. Your submissions will be two .alp programs, called loop.alp and prime.alp. Your files should NOT contain the word RUN, or any inputs!!!)
Write the Alphatron program loop.alp, a program which reads in two numbers X and Y. The program needs to loop X times, printing Y each time.
I.e. if the input is
4
3
then the output is
3
3
3
3
Level 4
Write the Alphatron program prime.alp. This program reads in a number X, and prints out all of the prime numbers, in order, that are smaller or equal to X.
For Level 4, you may research online into algorithms to find primes. The simplest one is something like:
for i =2 to X
for j =2 to i-1
if i is divisible by j, break and continue with the i loop
else continue and increment j
if you have finished the j loop, then i is prime, so print it out, then continue with the i loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!