Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following in the Simulator program below: (a, b, and c must all be

ID: 640202 • Letter: I

Question

Implement the following in the Simulator program below: (a, b, and c must all be put in the 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 numberread indicates how many numbers should be processed.

Here is the program:

#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;
}

This question is straight from the textbook C++ How to Program 8th Edition (pages 372-375 problem #8.18)

Please add comments to each line of code as well!

Explanation / Answer

Program:

#include <iostream>

using namespace std;

void sentinal_controlled(int *&memory, int &count){
   int temp;
   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)
       }
   }
}

void counter_controlled(int *&memory, int &count){
   // this loop runs only 7 times
   // so, only seven inputs are taken in
   for(int i = 0; i < 7; ++i){
       cout << i << " ? ";
       cin >> memory[i];
       count++;
   }
}

void series(int *&memory, int &count){
   int size;
   cout << "Size: ";
   cin >> size;
   // first user input is considered as how many numbers to read in
   for(int i = 0; i < size; ++i){
       cout << i << " ? ";
       cin >> memory[i];
       count++;
   }
}

int main(){
   int option; // this is to input from user(to select a specific way of taking input)
   int *memory = new int[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
   }
   cout << "Enter 1 for sentinal controlled loop" << endl;
   cout << "Enter 2 for counter controlled loop" << endl;
   cout << "Enter 3 to read series of numbers" << endl;
   cout << "(first number read is how many numbers should be processed)" << endl;
   cout << "Enter Your option: ";
   cin >> option;
   switch(option){
       case 1:
           // this is call to sentinal controlled loop
           sentinal_controlled(memory, count);
           break;
       case 2:
           // this is call to counter controlled loop
           counter_controlled(memory, count);
           break;
       case 3:
           // this is call to take numbers in serial, with first number
           // indicating first number read as how many numbers to take in
           series(memory, count);
           break;
       default:
           cout << endl << "Error: Invalid Input" << endl;
   }
   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){
                   // this for loop is to print first line of output(From 0 to 9)
                   cout << j << " ";
               }
               cout << endl;
               for(int j = 0; j < 10; ++j){
                   // this for loop is to output all the remaining rows(one row at a time)
                   cout << j * 10 << " ";
                   for(int k = 0; k < 10; ++k){
                       // this for loop prints the data stored in memory
                       if(memory[(j * 10) + k] > 1000){
                           cout << memory[(j * 10) + k] << " ";
                       }
                       else{
                           cout << memory[(j * 10) + k] << " ";
                       }
                   }
                   cout << endl;
               }
               return 0;
               break;
           default:
               cout << "*** Unknown Opcode ***" << endl;
               cout << "*** Simpletron execution abnormally terminated ***" << endl;
               break;
       }
   }
   return 0;
}