1 Read a series of numbers and determine and print the large
1. Read a series of numbers and determine and print the largest number. The first number read indicates how many numbers should be processed.
Solution
1.
#include <iostream>
using namespace std;
int main()
{
int memory[100];
int operation; int operand;
int accum = 0;
int j;
for (j = 0; j < 100; j++ ).
memory[j] = 0;
memory [00] = 1010;
memory [01] = 2009;
memory [02] = 3008;
memory [03] = 2109;
memory [04] = 1109;
memory [05] = 4300;
memory [06] = 1009;
j = 0;
while ( true )
{
operand = memory[ j ]%100;
operation = memory[ j ]/100;
switch ( operation ){
case 1: //reads a variable into a word from loc. Enter in -1 to exit
cout <<\"\ Input a positive variable: \";
cin >> memory[ operand ]; break;
case 2: // takes a word from location
cout << \"\ \ The 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 = accum + memory[ operand ]; break;
case 6: // subtracts
accum = accum - memory[ operand ]; break;
case 7: //divides
accum = accum / (memory[ operand ]); break;
case 8: // multiplies
accum = accum*memory [ operand ]; break;
case 9: // Branches to location
j = -1; break;
case 10: //branches if acc. is < 0
if (accum < 0)
j = 5;
break;
case 11: //branches if acc = 0
if (accum == 0)
j = 5;
break;
case 12: // Program ends
exit(0); break;
}
j++;
}
return 0;
}
2.
int operand;
int accum = 0;
int pos = 0;
int j;
for (j = 0; j < 100; j++ )
memory[j] = 0;
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; operation = memory[ j ]/100;
switch ( operation ){
case 1: //reads a variable into a word from loc. Enter in -1 to exit
cout <<\"\ enter #: \";
cin >> memory[ operand ]; break;
case 2: // takes a word from location
cout << \"\ \ The 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 = accum + memory[ operand ]; break;
case 6: // subtracts
accum = accum - memory[ operand ]; break;
case 7: //divides
accum = accum / (memory[ operand ]); break;
case 8: // multiplies
accum = 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;
}
3.
int main()
{
int mem[100];
int operation;
int operand;
int accum = 0;
int j;
for (j = 0; j < 100; j++ )
memory[j] = 0;
mem[23] = 1; mem[0] = 1030; mem[01] = 4123; mem[02] = 4134;
mem[03] = 1011;
mem[04] = 3204;
mem[05] = 4005; mem[06] = 4006;
mem[07] = 4007;
mem[08] = 4008;
mem[09] = 4009;
mem[10] = 4010;
mem[11] = 4311; // exits
j = 0; // this is the starting value..
while ( true )
{
operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
operation = memory[ j ]/100;
switch ( operation ){
case 1: //reads a variable into a word from loc. Enter in -1 to exit
cout <<\"\ enter #: \";
cin >> memory[ operand ]; break;
case 2: // takes a word from location
cout << \"\ \ The 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 = accum + memory[ operand ]; break;
case 6: // subtracts
accum = accum - memory[ operand ]; break;
case 7: //divides
accum = accum / (memory[ operand ]); break;
case 8: // multiplies
accum = 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;
case 13: // checks > than
if (accum < mem[operand])
accum = mem[operand];
break;
}
j++;
}
return 0;
}





