# Algorithm

• Feb 25th 2007, 06:44 PM
chillerbros17
Algorithm
pu
• Feb 25th 2007, 08:58 PM
CaptainBlack
Quote:

Originally Posted by chillerbros17
Consider the following algorithm.

Input X
A:= 0
B:= 0
Count:= 1
While X <>0
If > A then
Do
A:= X
B:= Count
End Do
Input X
Count :=Count + 1
End While
Output A, B

Suppose the inputted values of x are:
3, 4, 2, 9, 3, 9, 2, 0, 4, 7, 1 (all values may not be used)

(a) Trace through the algorithm.

(b) Given a list of numbers, what will A output and what will B output?

1). Indentation is very helpful in following pseudo code, you can use [code]..[/code]
tags to delimit a code block where the indentation will sort of work:

Code:

```Input X A:= 0 B:= 0 Count:= 1 While X <>0     If > A then         Do             A:= X             B:= Count         End Do     Input X     Count :=Count + 1 End While Output A, B```
2). What do you want the "> A" in "If > A then" to mean?

RonL
• Feb 25th 2007, 09:04 PM
chillerbros17
X > A
• Feb 26th 2007, 05:42 AM
earboth
Quote:

Originally Posted by chillerbros17
Consider the following algorithm.

Input X
A:= 0
B:= 0
Count:= 1
While X <>0
If > A then
Do
A:= X
B:= Count
End Do
Input X
Count :=Count + 1
End While
Output A, B

Suppose the inputted values of x are:
3, 4, 2, 9, 3, 9, 2, 0, 4, 7, 1 (all values may not be used)

(a) Trace through the algorithm.

(b) Given a list of numbers, what will A output and what will B output?

Hi, to (a): Use a trace table. I've used the given list of numbers.
Code:

```    x    A    B    count     -    0    0      1              start     3    3    1      2     4    4    2      3     2    4    2      3     9    9    3      4     3    9    3      4     9    9    4      5     2    9    4      5     0  → break off of program output: A = 9         B = 4```
EB