Pseudocode: Confusing syntax "<>" and "variable:2:6" - pseudocode

I am currently studying pseudo code, and despite my 3 year history in programming, this 1 particular practice-exam question has me perplexed with its unconventional code (shown below):
Highlighted in Pink, are my 2 main problems with the code. I have experience across 3 languages, yet I have never encountered the control flow method <>, and cannot imagine exactly what it would be used for. In addition to this, the variable average appears in the code in the form of "average:6:2", for which I am equally clueless.
To Summarise:
What is the function of the control flow method "<>"
As is stated in question (a) in the image above, what is the purpose of 'average:6:2'?

<> is common for not equal
While number is not equal to 999
number:filed_width:precision is pascal formatter for real number with filed_width being the space for field and precision is numer of digits after dot. so 3.141519:4:1 will print <space>3.1

What is the function of the control flow method "<>"
- It is "less than or greater than". If the input is equal to 999 then the loop ends. The number 999 is used as a Sentinel Value.
What is the purpose of 'average:6:2'?
- I'm thinking this is 6 digits with 2 decimal places.

Related

In BCPL what does "of" do?

I am trying to understand some ancient code from a DEC PDP10 written in BCPL. A sample of the code is as follows:
test scanner()=S.DOTNAME then
$( word1:=checklook.up(scan.info,S.SFUNC,"unknown Special function [:s]")
D7 of temp:=P1 of word1
scanner()
$) or D7 of temp:=SF.ACTION
What do the "D7 of temp" and "P1 of word1" constructs do in this case?
The unstoppable Martin Richards is continuing to add features to the BCPL language(a), despite the fact that so few people are aware of it(b). Only seven or so questions are tagged bcpl on Stack Overflow but don't get me wrong: I liked this language and I have fond memories of using it back in the '80s.
Some of the things added since the last time I used it are the sub-field operators SLCT and OF. As per the manual on Martin's own site:
An expression of the form K OF E accesses a field of consecutive bits in memory. K must be a manifest constant equal to SLCT length:shift:offset and E must yield a pointer, p say.
The field is contained entirely in the word at position p + offset. It has a bit length of length and is shift bits from the right hand end of the word. A length of zero is interpreted as the longest length possible consistent with shift and the word length of the implementation.
Hence it's a more fine-grained way of accessing parts of memory than just the ! "dereference entire word" operator in that it allows you to get at specific bits within a word.
(a) Including, apparently, a version for the Raspberry PI, which may finally give me an excuse to break out all those spare PIs I have lying around, and educate the kids about the "good old days".
(b) It was used for at least one MC6809 embedded system I worked on, and formed a non-trivial part of AmigaDOS many moons ago.

App Inventor 2 - Separating numbers

In my app I will have several lines of numbers from 4 to 10
characters in length. How would I go around separating the generated
numbers every 3 numbers (at thousand, million, billion).
I will not know the length of the numbers beforehand so the same .text
label can sometimes be longer or shorter.
e.g. from 1234567 12345 1234567890
to 1,234,567 12,345 1,234,567,890
The separators can be a space, a dot or a comma (any is fine). I am
quite familiar with AI2 but can't figure this one out.
EDIT: Seems like I managed to segment the text (yay!)
What would I have to do now if I have let's say 10 labels with all different lengths? It would be extremely excessive to check length of each label using an if and then segmenting it using corresponding length option. Is there a way to shorten the process?
Thanks a lot :)
You can write your own custom procedure, for example see the following App Inventor Classic example, which uses a for range loop:
The algorithm works like this (example=2134256):
result after first loop: ,256
result after second loop: ,134,256
then add the remaining digits of the number, in this case 2
result in the end: 2,134,256
Edit: For App Inventor 2, you also have to use a procedure with result. Also let me recommend to use a local variable result instead of a global variable.

ZPL - Code 128 Understanding better how to use Subsets B and C

I'm getting involved with ZPL (a little bit) since a few days, so I'm sorry if the questions will look stupid.
I've got to build a bar code 128 and I finally realized: I got to make it as shorter as possible.
My main question is: is it possible to switch to subset C and then back to B for just 2 digits? I read the documentation and subset C will ready digits from 00 to 99, so in theory it should work, practically, will it be worth it?
Basically when I translate a bar code with Zebra designer, and print it to a file, it doesn't bother to switch to subset C for just a couple of digits.
This is the text I need to see in the bar code:
AB1C234D567890123456
By the documentation I read, I would build something like this:
FD>:AB1C>523>64D>5567890123456
Instead Zebra Designer does:
FD>:AB1C234D>5567890123456
So the other question is, will the bar code be the same length? Actually, will mine be shorter? [I don't have a printer with me at the moment]
Last question:
Let's say I don't want to spend much time scripting this up, will the following work ok, or will it make the bar code larger?
AB1C>523>64D>556>578>590>512>534>556
So I can just build a very simple script which checks two chars per time, if they're both numbers, then add >5 to the string.
Thank you :)
Ah, some nice loose terminology. Do you mean couple="exactly 2" or couple="a few"?
Changing from one subset to another takes one code element, so for exactly 2 digits, you'd need one element to change and one to represent the 2 digits in subset C. On the other hand, staying with your original subset would take 2 elements - so no, it's not worth the change.
Further, if you were to change to C for 2 digits and then back to your original, the change would actually be costly - C(12)B = 3 elements whereas 12 would only be 2.
If you repeat the exercise for 4 digits, then switching to C would generate C(12)(34) = 3 elements against 4 to stay with what you have; or C(12)(34)B = 4 elements if you switch and change back, or 4 elements if you stick - so no gain.
With 6 or more successive numerics, then you gain regardless of whether or not you switch back.
So overall,
2-digit terminal : No difference
2-digit other : code is longer
4-digit terminal : code is shorter
4-digit other : no difference
more than 4 digits : code is shorter.
And an ODD number of digits would need to be output in code A or B for the first digit and then the above table applies to the remainder.
This may not be the answer you're looking for, but specifying A (Automatic Mode) as the final parameter to the ^BC command will make the printer do this for you.
Example:
^XA
^FO100,100
^BY3
^BCN,100,N,N,A
^FD0123456789^FS
^XZ

Print integer with "most appropriate" kilo/mega/etc multiplier [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert byte size into human readable format in java?
Given an integer, I'd like to print it in a human-readable way using kilo, mega, giga etc. multipliers. How do I pick the "best" multiplier?
Here are some examples
1 print as 1
12345 print as 12.3k
987654321 print as 988M
Ideally the number of digits printed should be configurable, e.g. in the last example, 3 digits would lead to 988M, 2 digits would lead to 1.0G, 1 digit would lead to 1G, and 4 digits would lead to 987.7M.
Example: Apple uses an algorithm of this kind, I think, when OSX tells me how many more bytes have to be copied.
This will be for Java, but I'm more interested in the algorithm than the language.
As a starting point, you could use the Math.log() function to get the "magnitude" of your value, and then use some form of associative container for the suffix (k, M, G, etc).
var magnitude = Math.log(value) / Math.log(10);
Hope this helps somehow

Likeliest way to compute validation using MOD 7 check on the last number

I am attempting to perform number validation for a proprietary ID implementation. I was anticipating a spec document for the algorithm in detail, but this is all that they sent:
The IDs are 9 digits.
The check digit is a MOD 7 check on the last number.
I think I am to assume that "MOD 7" means to apply modulus 7 to something--I suspect that "the last number" means more than just applying mod 7 to the last digit, otherwise every valid ID would end in 0 or 7.
Meanwhile someone in a separate conversation said that it was actually "a CRC MOD 7 check on the last number" (throwing in "CRC"), but I think that this was a misspoken detail and that CRC is not used at all, but what do I know?
I am having difficulty finding information on some standard way of reading this specification and interpreting this for some "standard algorithm". Most of the samples I've found consist of many different varieties of algorithms, such as weighted or unweighted, etc.
Does anyone know how I am most likely to interpret this, or if I am supposed to ask for more information? If I am supposed to ask for more information, what questions should I ask?
As I said in my comment, this is horrible documentation, but here's the only way to read this that makes sense to me:
You do a MOD 7 check on the whole card number (all nine digits), and then check if that result equals the last digit in the card number.
Slight variation on Briguy37's answer, this variation so far appears to actually be the correct answer in my case since initial tests seem to pass.
You do a MOD 7 check on the first eight digits of the 9-digit number as a single 8-digit integer, and then check if that result equals the last digit in the card number.
You should ask for more information. Think of the consequences if you don't and it turns out the interpretation you received from the internets is wrong.

Resources