Given the Simulator program below, a) Use a sentinel-controlled loop to read pos
ID: 639619 • Letter: G
Question
Given the Simulator program below,
a) Use a sentinel-controlled loop to read positive numbers and compute and print their
sum. Terminate input when a negative number is entered.
b) Use a counter-controlled loop to read seven numbers, some positive and some negative,
and compute and print their average.
c) Read a series of numbers, and determine and print the largest number. The first number
read indicates how many numbers should be processed.
#include <iostream>
using namespace std;
int main(){
int memory[100]; // array of 100 integes for max of 100 instructions
int temp;
int count = 0; // to count number of valid inputs
cout << "*** Welcome to Simpletron! ***" << endl;
cout << "*** Please enter your program one instruction ***" << endl;
cout << "*** (or data word) at a time. I will type the ***" << endl;
cout << "*** location number and a question mark (?). ***" << endl;
cout << "*** You then type the word for that location. ***" << endl;
cout << "*** Type the sentinel -99999 to stop entering ***" << endl;
cout << "*** your program. *** " << endl;
for (int i = 0; i < 100; ++i){
memory[i] = 0; // initializing all memory locations to 0
}
for (int i = 0; i < 100; ++i){
cout << i << " ? ";
cin >> temp; // taking user input into temp to check for sentinal(-99999)
if (temp == -99999){
// if(-99999) is entered, we break out of for loop(stopping taking inputs)
break;
}
else{
// if sentinal is not entered(storing instruction in memory)
memory[i] = temp;
count++; // incrementing count(Nuber of valid instructions in memory)
}
}
cout << "Program loading completed Program execution begins" << endl;
// initializing all registers to zero
int counter = 0; // for instruction counter
int acc = 0; // for Accumulator
int IR = 0; // for Instruction Register
int opcode = 0; // for opcode
int operand = 0; // for operand
for (int i = 0; i < count; ++i){
counter = i; // taking i into instruction counter
IR = memory[counter]; // take instruction into Instruction Register
opcode = IR / 100; // left most two digits of instruction represent opcode
operand = IR % 100; // right most two digits of instruction represent operand
if (operand < 0 || operand > 99){
cout << "*** operand Out of Range ***" << endl;
cout << "*** Simpletron execution abnormally terminated ***" << endl;
return 0;
}
switch (opcode){
case 10:
// if opcode is 10 (take input from user and store in memory represented by operand)
cout << "?: ";
cin >> memory[operand];
break;
case 11:
// if opcode is 11(give output to user)
cout << memory[operand] << endl;
break;
case 20:
// if opcode is 20 (Load data from memory represented by operand into Accumulator)
acc = memory[operand];
break;
case 21:
// if opcode is 21 (Store data from Accumulator into memory represented by operand)
memory[operand] = acc;
break;
case 30:
// if opcode is 30 (add data in memory represented by operand with Accumulator)
// and store it in accumulator
acc += memory[operand];
break;
case 31:
// if opcode is 31 (subtract data in memory represented by operand with Accumulator)
// and store it in accumulator
acc -= memory[operand];
break;
case 32:
// if opcode is 32 (divide data in memory represented by operand with Accumulator)
// and store it in accumulator
if (memory[operand] == 0){
// if denominator is zero (we can not divide. so, Terminate the program)
cout << "*** Attempt to divide by zero ***" << endl;
cout << "*** Simpletron execution abnormally terminated ***" << endl;
return 0;
}
else{
acc /= memory[operand];
}
break;
case 33:
// if opcode is 33 (multiply data in memory represented by operand with Accumulator)
// and store it in accumulator
acc *= memory[operand];
break;
case 40:
// if opcode is 40 (Jump to adress given by data in memory represented by operand)
i = memory[operand];
break;
case 41:
// if opcode is 41 (Jump to adress given by data in memory represented by operand)
// jump only if accumulator is Negative
if (acc < 0){
i = memory[operand];
}
break;
case 42:
// if opcode is 42 (Jump to adress given by data in memory represented by operand)
// jump only if accumulator is Non-Negative
if (acc >= 0){
i = memory[operand];
}
break;
case 43:
// if opcode is 43 (Terminate the program after displaying the information)
cout << endl << endl;
cout << "Execution Halted Normally" << endl;
cout << "REGISTERS: " << endl;
cout << "Accumulator: " << acc << endl;
cout << "Instruction Counter: " << counter << endl;
cout << "Instruction Register: " << IR << endl;
cout << "Opcode: " << opcode << endl;
cout << "Operand: " << operand << endl;
cout << " MEMORY: " << endl;
cout << " ";
for (int j = 0; j < 10; ++j){ // print first line of output from 0-9
cout << j << " ";
}
cout << endl;
for (int j = 0; j < 10; ++j){ // print all the remaining rows (one row at a time)
cout << j * 10 << " ";
for (int k = 0; k < 10; ++k){ // print data stored in memory
if (memory[(j * 10) + k] > 1000){
cout << memory[(j * 10) + k] << " ";
}
else{
cout << memory[(j * 10) + k] << " ";
}
}
cout << endl;
}
system("pause");
return 0;
break;
default:
cout << "*** Unknown Opcode ***" << endl;
cout << "*** Simpletron execution abnormally terminated ***" << endl;
break;
}
}
system("pause");
return 0;
}
Explanation / Answer
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.