0

We are going over a sample test paper for a course and would like assistance from a C programmer translating pseudo code into C code.

We are having a disagreement becasue two of the possible options look correct. (I am NOT a programmer and nor are my colleagues)

The pseudo code is:

"If we have written 200 characters to the buffer variable, the stack should stop because it cannot hold any more data."

The possible answers are:

  1. If (i >= 200) then exit
  2. If (i > 200) then exit
  3. If (i < 200) then exit
  4. If (i <= 200) then exit

We know the answer is NOT 3 and 4.

I beleive that the answer is "2" (if 200 characters have been written; end the program), but my colleagues believe that the correct answer is "1" (if 200 or more characters have been written; end the program).

Please advise as to what the correct answer is.

4
  • 3
    C doesn't have a then at all. They're all incorrect. Commented Apr 28, 2011 at 19:24
  • It seems to me that it would be (1), but I would rather say not enough information has been given. Commented Apr 28, 2011 at 19:27
  • None of the possible answers are correct, simply because neither is C. There is no "then" keyword, no "exit" keyword. It is still a pseudo code. Commented Apr 28, 2011 at 19:28
  • I think he needs the C code for the correct answer, which can only be determined if we know how i is indexed (from zero or one or something else). Commented Apr 28, 2011 at 19:29

8 Answers 8

4

I don't see any "200 or more" or "more than 200" in the pseudo code. Maybe the right answer is if (i == 200) then exit.

Sign up to request clarification or add additional context in comments.

Comments

2

Pretend you're the computer. Every time you write a character to the buffer, you increment i.

Assuming you start at 0, which is normal in C, after you've written a single character, i would be 1. After the second character, i is 2. Extrapolate.

1 Comment

I guess he/she will have to pretend to be a central processor unit rather than a program :)
1

Looks like "If (i >= 200) then exit" to me.

Comments

1

In C code, the angle bracket means exactly the same as in mathematics:
< = smaller than
> = greater than

And if you add an =sign, you get smaller/greater or equal.

If i is the number of characters written to the stack, then i >= 200 (i is greater or equal to 200) is your solution.

Comments

1

Well none of the answers is actually C. You are translating a statement into psudo code (sorry I know its symantics but there is still a distinction). In any case since it doesn't state more than 200 I would vote for 1. If (i >= 200) then exit myself as it states when you hit 200 so its inclusive.

Comments

1

"If we have written 200 characters to the buffer variable, the stack should stop because it cannot hold any more data."

The correct answer is 1: If (i >= 200) then exit.

Here's why.

Your pseudo code specifically says, if 200 characters are written, exit.

If the answer was 2: It would only exit after 201 characters were written (since 201 > 200).

Comments

0

This depends on how i is indexed and how your loop is setup. If it is indexed by 0 (likely) and you had a buffer (buf) you would do something like this.

data_type buf[200];
int i;
for (i = 0; i < 200; i++) {
  // write data to buffer
  buf[i] = data;
}

Therefore, when i becomes 200, the loop exits (and the condition is i < 200). data_type is what ever data you are using in the buffer. If i was indexed starting at one then your loop condition would be i <= 200.

Comments

0

i vote for 1.if(i>=200) since that is what your spec says. C doesnt have 'then' keyword, and it should have been 'exit(0);' instead of just 'exit'.

if(i >= 200){
    exit(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.