I have a pseudocode assignment for a class. I have to design a program to output every even number starting with 2 and going to 100. I need someone to tell me if this is correct. If it isn't could someone point me in the right direction?
start
Declarations
num A = 0
num B = 100
num C
while A < B
C = A + 2
Output C
Endwhile
stop
start
Declarations
num A = 0
num B = 100
while A < B
A = A + 2
Output A
Endwhile
stop
Related
I am having a problem in really simple program. My problem is that i lose value (y = 50 or 100 or 150) because at that time first condition is not valid. so how can i repeat loop for let say y = 50. (i don't want to use '=' e.g y< = (50+increment) because this is just a dummy program.
thanks
increment = 0
b = 1
var = 0
for y in range(1,1000):
if y>= increment and y< (50+increment):
print(f'{y} in List {b}')
else:
var = y
increment += 50
b+=1
How to sum 2 numbers digit by digit with pseudo code?
Note: You don't know the length of the numbers - if it has tens, hundreds, thousands...
Units should be add to units, tens to tens, hundreds to hundreds.....
If there is a value >= 10 in adding the units you need to put the value of that ten with "the tens"....
I tried
Start
Do
Add digit(x) in A to Sum(x)
Add digit(x) in B to Sum(x)
If Sum(x) > 9, then (?????)
digit(x) = digit(x+1)
while digit(x) in A and digit(x) in B is > 0
How to show the result?
I am lost with that.....
Please help!
Try this,
n = minDigit(a, b) where a and b are the numbers.
let sum be a number.
m = maxDigit(a,b)
allocate maxDigit(a,b) + 1 memory for sum
carry = 0;
for (i = 1 to n)
temp = a[i] + b[i] + carry
// reset carry
carry = 0
if (temp > 10)
carry = 1
temp = temp - 10;
sum[i] = temp
// one last step to get the leftover carry
if (digits(a) == digits(b)
sum[n + 1] = carry
return
if (digits(a) > digits(b)
toCopy = a
else
toCopy = b
for (i = n to m)
temp = toCopy[i] + carry
// reset carry
carry = 0
if (temp > 10)
carry = 1
temp = temp - 10;
sum[i] = temp
Let me know if it helps
A and B are the integers you want to sum.
Note that the while loop ends when all the three integers are equal to zero.
carry = 0
sum = 0
d = 1
while (A > 0 or B > 0 or carry > 0)
tmp = carry + A mod 10 + B mod 10
sum = sum + (tmp mod 10) * d
carry = tmp / 10
d = d * 10
A = A / 10
B = B / 10
I'm new to programming and I'm trying to program something but there's some kind of syntax error which I can't work out. Any help would be much appreciated. Here's my code:
begin
puts"Enter a number to count, or to exit type 0."
y = gets.chomp.to_i
if y == 0
exit
end
puts"Now put the number you're starting with"
x = gets.chomp.to_i
if y + x == 12 or y + x < 12
print x + y
end
if y + x > 12
n = y + x - 12
end
begin
if n < 12 or n == 12
print n
end
if n > 12
n = n - 12
end
end until if n < 12 or n == 12
end until y == 0
end
Your use of until if if wrong. They are each control sequences. You shouldn't need both.
Your n is not visible later in the code. Declare n=0 for example before if y + x > 12 to make it visible and accessible in the relevant code blocks.
Then, until if is wrong, this should simply be until
Lastly, delete the last end keyword.
I want to write a loop that scans all binary sequences of length n with k 1's and n-k 0's.
Actually, in each iteration an action is performed on the sequence and if a criterion is met the loop will break, otherwise it goes to next sequence. (I am not looking for nchoosek or perms since for large values of n it takes so much time to give the output).
What MATLAB code do you suggest?
You could implement something like an iterator/generator pattern:
classdef Iterator < handle
properties (SetAccess = private)
n % sequence length
counter % keeps track of current iteration
end
methods
function obj = Iterator(n)
% constructor
obj.n = n;
obj.counter = 0;
end
function seq = next(obj)
% get next bit sequence
if (obj.counter > 2^(obj.n) - 1)
error('Iterator:StopIteration', 'Stop iteration')
end
seq = dec2bin(obj.counter, obj.n) - '0';
obj.counter = obj.counter + 1;
end
function tf = hasNext(obj)
% check if sequence still not ended
tf = (obj.counter <= 2^(obj.n) - 1);
end
function reset(obj)
% reset the iterator
obj.counter = 0;
end
end
end
Now you can use it as:
k = 2;
iter = Iterator(4);
while iter.hasNext()
seq = iter.next();
if sum(seq)~=k, continue, end
disp(seq)
end
In the example above, this will iterate through all 0/1 sequences of length 4 with exactly k=2 ones:
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
ok so im trying to do an assignment and i have written this code but it has errors, and i cant see why.
its saying expected statement, and im looking over it and i cant find what the problem is. Would be amazing if someone could shed some light on the error and possibly help me out.
here is the do until statement:
Sub btnTransformY_onclick()
Do Until r = 4 And n = 3
Do Until m = 1 To 8
Change(r,c) = (transformationY(r,1) * meh(1, m)) + transformationY(r, 2) * meh(2,m) + TransformationY(r,3) * meh(3,m)
m = m + 1
Next
c = c + 1
If c = 8 Then r = r + 1 And c = 1
Next
End Sub
unfortunately this hasnt worked, although it does seem to be the Do Until m = 1 To 8
does anyone know if to is valid syntax for a do until loop?
I reckon you want something on these lines, except I suspect you want two Next loops for r and n rather than one Do Until.
Sub btnTransformY_onclick()
r = 0
n = 0
Do Until r = 4 And n = 3
For m = 1 To 8
Change(r,c) = (transformationY(r,1) * meh(1, m)) + transformationY(r, 2) * meh(2,m) + TransformationY(r,3) * meh(3,m)
''m = m + 1
Next
c = c + 1
If c = 8 Then
r = r + 1
c = 1
''Where does n get counted up?
End If
Loop
End Sub
Next is for for loops, to close a do loop use loop:
do until ..
...
loop
Also
if c = 8 then r = r + 1 and c = 1
Is a bitwise comparison (bits of LHS anded with RHS) which is probably not what you want, so instead;
if c = 8 then
r = r + 1
c = 1
end if