PDA

View Full Version : Simple Question from Stupid Person...



Monmouth3
10-02-2002, 06:40 AM
Hi. I have a series of question about a simple hypothetical program. It is part of a test-review sheet, and I can't figure it out for the life of me:

Here's the sim-program:

GET #
STORE N1
GET
STORE N2
SUB N1 # subtracts N1 from what's on scratch-pd
IFPOS A # if s-pad is pos, go to A
LOAD N1 # load N1 to s-pad
GOTO DONE
A LOAD N2
DONE PRINT
STOP
N1 INIT 0
N2 INIT 0

Here are the questions:
(a) If you run this and submit the numbers 1O and 3O in that order, what value will be printed?

(b) If you run this and submit 10 and 3 in that order, what value will print?

(c) How could you phrase in plain English what this program does?

(d) Sketch out a program that will read a sequence of numbers and print the value of how many numbers there were. "Read" should stop when the number 0 is read. This 0 will not be included in the count.

(e) Write the code for a program that prints the numbers 1, 2, 3, ..., and will loop infinitely.


Thanks!!!

Hammer
10-02-2002, 06:44 AM
Seems to me like you've posted the complete set of questions straight from the sheet. Why don't _you_ try answering them.

Read the second post in this thread (http://www.cprogramming.com/cboard/announcement.php?s=&forumid=4).


[edit]Also, this isn't C related. I'll leave it here for about 30min, then I'm going to move it to GD.

Monmouth3
10-02-2002, 08:20 AM
OK, so I figured the rest out. However, I still can't figure out how to do the COUNT program. I could program it to ADD a list of consecutive numbers until one of them was zero or until the sum itself was zero, but I am not aware if there is a "COUNT" command or if there is some way to make one function with other basic commands. Any suggestions?

Govtcheez
10-02-2002, 08:23 AM
Which one's the count program? The 4th or 5th?

Monmouth3
10-02-2002, 08:30 AM
The fourth is the "count" program, though I am also having trouble with #5. This is what I have four COUNT:

start GET
IFZERO print
ADD COUNT
STORE COUNT
GOTO start
print LOAD COUNT
PRINT
STOP
count INIT 0 (to make it zero to start with, I guess)

Monmouth3
10-02-2002, 08:37 AM
And this is what I have for #5... can anyone tell me if it would work or if this it totally insane and wrong?
*Write a program that prints the numbers 1, 2, 3, ..., and does not stop, i.e., is an infinite loop.

start LOAD INTEGER
ADD 1 INTEGER
STORE INTEGER
GOTO print
print LOAD INTEGER
PRINT
GOTO start
STOP
INTEGER INIT 0

(Is that STOP necessary or redundant. And would this program work?)

Govtcheez
10-02-2002, 08:47 AM
>And would this program work?)

What the hell language are you trying to write it in?

> GOTO print
print LOAD INTEGER

That GOTO's redundant - there's no reason to tell anything "goto the next line".

Here's the code (in C - I still don't know what you're trying to do) for #5
#include <stdio.h>

int main(void)
{
int count = 1;
while(1)
{
printf("%d\n", count);
count++;
}
return 0;
}