在我的C ++程序中寻找一些输入。 Simpletron,机器语言 [英] Looking for some input on my C++ program. Simpletron, Machine language
问题描述
编辑:所以,似乎我在编码的某处有一个问题。每当我运行程序和输入一个变量,它总是返回相同的答案..位置76的内容是0。
发布于几天前关于一个问题,但它只是一个编译错误,所以如果这看起来很熟悉,这是为什么。我会重申,我是新的编程,我不是最好的,所以我要去所以我一直在寻找输入,并确保这个程序将做我希望他们正在寻找的是什么,这是一个SML程序,无论如何,这是一个家庭作业任务,我想要一个良好的成绩,无论如何,这里是指令:
编写SML(Simpletron机器语言)程序来完成以下每个任务:
Okay guys, I posted on here a few days ago about a question but it was just a compilation error, so if this looks familiar, thats why. I will reiterate, I'm new to programming, I'm not the best, so I'm going for simplicity. Also this is an SML program. Anyway, this IS a homework assignment and I'm wanting a good grade on this. So I was looking for input and making sure this program will do what I'm hoping they are looking for. Anyway, here are the instructions: Write SML (Simpletron Machine language) programs to accomplish each of the following task:
A)使用哨兵控制循环读取正数并计算并打印它们的和当输入负数时终止输入
B)使用计数器控制循环读取七个数字,一些为正,一些为负,并计算+打印avg。
C)读取一系列数字,确定并打印最大数字。第一个数字表示应该处理多少数字。
A) Use a sentinel-controlled loop to read positive number s and compute and print their sum. Terminate input when a neg number is entered. B) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute + print the avg. C) Read a series of numbers, and determine and print the largest number. The first number read indicates how many numbers should be processed.
没有进一步的应付,这里是我的程序。一起。
Without further a due, here is my program. All together.
计划A
#include <iostream>
using namespace std;
int main()
{
int memory[100]; //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 j;
for (j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
memory[j] = 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.
memory [00] = 1010;
memory [01] = 2009;
memory [02] = 3008;
memory [03] = 2109;
memory [04] = 1109;
memory [05] = 4300;
memory [06] = 1009;
j = 0; //Makes the variable j start at 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 Input a positive variable: ";
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 = 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;
}
计划B
//Part b finding the sum + avg.
int main()
{
int mem[100];
int operation;
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; // 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 = 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;
}
计划C
///Part c
int main()
{
int mem[100];
int operation;
int operand;
int accum = 0;
int j;
for (j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
memory[j] = 0;
mem[23] = 1; //decrements 1 place in mem
mem[0] = 1030; // Takes in # of values to be stored.
mem[01] = 4123; // These 4 memory slots check for the largest variable then store
mem[02] = 4134;
mem[03] = 1011;
mem[04] = 3204;
mem[05] = 4005; // These 5 decrement the count+ store + branch.
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;
//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 = 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;
}
推荐答案
mem[09] = 4009;
哪个八进制数是09? :)
编辑:对于程序A(和其余的事情),你可能会发现它是信息丰富的打印出'operand'和'operation'变量之前,你打开switch语句。输出:
Which octal number is 09 exactly?? :)
For program A (and the rest for that matter) you may find it informative to print out the 'operand' and 'operation' variables before you hit the switch statement. Output:
Operand: 10 Operation: 10
Operand: 9 Operation: 20
Operand: 8 Operation: 30
Operand: 9 Operation: 21
Operand: 9 Operation: 11
Operand: 9 Operation: 10
Operand: 0 Operation: 0
Operand: 0 Operation: 0
... *LOTS of lines omitted....
Operand: 0 Operation: 0
Operand: -97 Operation: -9498072
Operand: 0 Operation: 0
Operand: 88 Operation: 20898776
Operand: 12 Operation: 0
Operand: 8 Operation: 22856
Operand: 69 Operation: 20898775
代码通过 switch()
运行几乎1300次,最后要求输入用户。在我看来,当操作数= 0时,程序A会跳出轨道,当你打这行: memory [05] = 4300;
The code runs through the switch()
almost 1300 times before finally asking for input from the user. It seems to me that program A goes off the rails when the operand = 0, which happens when you hit this line: memory [05] = 4300;
这篇关于在我的C ++程序中寻找一些输入。 Simpletron,机器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!