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: 3666648 • Letter: I

Question

Implement the following in the Simulator program below: (a, b, and c must all be put in the program below.) To be able to test the programs specified in a,b,c.

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.

--******Note: If you use what I have, please make the following changes to fix the errors that I currently have that I need to fix.

-Use constants for the operation codes.

-Constant names should be in UPPER CASE.

-Constants are usually declared outside of main() (so they have global scope).

-Memory dump is not formatted neatly

It is C++ and problems came from C++ How to program (8th or 9th edition).

Here is my code below (feel free to add any comments to what you implement):

#include <iostream>

using namespace std;

int main() {
   int memory[100]; // array of 100 integers with a max of 100 instructions
   int temp;
   int count = 0; // will count how many 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; // Sets 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 the loop ends
           break;
       }
       else {
           // stores instruction to memory if sentinel is not entered
           memory[i] = temp;
           count++; // increases count
       }
   }
   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;
   int operand = 0;
   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) {
               // when denomenator is 0, end 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) and only jump when 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) and only jump if accumulator is not 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;

Explanation / Answer

#include <iostream>
using namespace std;
int main() {
    int COUNTER_MAX = 7;
    cout <<"a) Sentinal Controller Loop b)Counter Controlled Loop c)Series of Numbers n Please enter your choice.";
    char choice;
    cin >> choice;
    int sum;
    int num;
    if( choice == 'a'){
        int count = 0;
        cout << "Enter numbers : ";
        while(true){
            cin >> num;
            if(num <= 0){
                cout << sum*1.0/count;
                break;
                }else{
                sum += num;
                count++;
            }
        }
        }else if(choice == 'b'){
        cout << "Enter 7 numbers : ";
        int i = COUNTER_MAX;
        while(i > 0){
            cin >> num;
            sum += num;
            i--;
        }
        cout << "Average : "<<sum*1.0/7;
        }else if(choice == 'c'){
        int max = -12112121;
        int numCount = 0;
        cout << "Enter the count of number which you wish to enter : ";
        cin >> numCount;
        cout << "Enter "<<numCount<<" numbers : ";
        int i = numCount;
        while(i > 0){
            cin >> num;
            if(max < num){
                max = num;
            }
            i--;
        }
        cout << numCount <<" "<<max;
    }
    return 0;
}

Let me know if you looking different requirement.