Alright, I'm still stuck on this homework problem. C++
- by Josh
Okay, the past few days I have been trying to get some input on my programs. Well I decided to scrap them for the most part and try again. So once again, I'm in need of help. For the first program I'm trying to fix, it needs to show the sum of SEVEN numbers. Well, I'm trying to change is so that I don't need the mem[##] = ####. I just want the user to be able to input the numbers and the program run from there and go through my switch loop. And have some kind of display..saying like the sum is?.. Here's my code so far.
#include <iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{
const int READ = 10;
const int WRITE = 11;
const int LOAD = 20;
const int STORE = 21;
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;
const int HALT = 43;
int mem[100] = {0}; //Making it 100, since simpletron contains a 100 word mem.
int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized.
int operand;
int accum = 0; // the special register is starting at 0
int counter;
for ( counter=0; counter < 100; counter++)
mem[counter] = 0;
// This is for part a, it will take in positive variables in
//a sent-controlled loop and compute + print their sum. Variables from example in text.
mem[0] = 1009;
mem[1] = 1109;
mem[2] = 2010;
mem[3] = 2111;
mem[4] = 2011;
mem[5] = 3100;
mem[6] = 2113;
mem[7] = 1113;
mem[8] = 4300;
counter = 0; //Makes the variable counter start at 0.
while(true)
{
operand = mem[ counter ]%100; // Finds the op codes from the limit on the mem (100)
operation = mem[ counter ]/100;
//using a switch loop to set up the loops for the cases
switch ( operation ){
case READ: //reads a variable into a word from loc. Enter in -1 to exit
cout <<"\n Input a positive variable: ";
cin >> mem[ operand ]; counter++; break;
case WRITE: // takes a word from location
cout << "\n\nThe content at location " << operand << " is " << mem[operand]; counter++; break;
case LOAD:// loads
accum = mem[ operand ];counter++; break;
case STORE: //stores
mem[ operand ] = accum;counter++; break;
case ADD: //adds
accum += mem[operand];counter++; break;
case SUBTRACT: // subtracts
accum-= mem[ operand ];counter++; break;
case DIVIDE: //divides
accum /=(mem[ operand ]);counter++; break;
case MULTIPLY: // multiplies
accum*= mem [ operand ];counter++; break;
case BRANCH: // Branches to location
counter = operand; break;
case BRANCHNEG: //branches if acc. is < 0
if (accum < 0)
counter = operand;
else
counter++;
break;
case BRANCHZERO: //branches if acc = 0
if (accum == 0)
counter = operand;
else
counter++;
break;
case HALT: // Program ends
break;
}
}
return 0;
}
part B
int main()
{
const int READ = 10;
const int WRITE = 11;
const int LOAD = 20;
const int STORE = 21;
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 41;
const int HALT = 43;
int mem[100] = {0};
int operation;
int operand;
int accum = 0;
int pos = 0;
int j;
mem[22] = 7; // loop 7 times
mem[25] = 1; // increment by 1
mem[00] = 4306;
mem[01] = 2303;
mem[02] = 3402;
mem[03] = 6410;
mem[04] = 3412;
mem[05] = 2111;
mem[06] = 2002;
mem[07] = 2312;
mem[08] = 4210;
mem[09] = 2109;
mem[10] = 4001;
mem[11] = 2015;
mem[12] = 3212;
mem[13] = 2116;
mem[14] = 1101;
mem[15] = 1116;
mem[16] = 4300;
j = 0;
while ( true )
{
operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
operation = memory[ j ]/100;
//using a switch loop to set up the loops for the cases
switch ( operation ){
case 1: //reads a variable into a word from loc. Enter in -1 to exit
cout <<"\n enter #: ";
cin >> memory[ operand ]; break;
case 2: // takes a word from location
cout << "\n\nThe content at location " << operand << "is " << memory[operand]; break;
case 3:// loads
accum = memory[ operand ]; break;
case 4: //stores
memory[ operand ] = accum; break;
case 5: //adds
accum += mem[operand];; break;
case 6: // subtracts
accum-= memory[ operand ]; break;
case 7: //divides
accum /=(memory[ operand ]); break;
case 8: // multiplies
accum*= memory [ operand ]; break;
case 9: // Branches to location
j = operand; break;
case 10: //branches if acc. is < 0
break;
case 11: //branches if acc = 0
if (accum == 0)
j = operand;
break;
case 12: // Program ends
exit(0); break;
}
j++;
}
return 0;
}