Pseudo-code questions on setting values to a large minus or large positive number - pseudocode

In a program where you input 10 positive numbers and output the smallest number input, if I created a variable called "Small", why do I need to set that to a large positive number. In other programs, i've seen it being set up as a large minus number. Why is this?

I think what you mean by calling your variable 'small' is that you're gonna compare it to each input value and use it to store the smallest value then return it.
But if the default value of your variable 'small' is already smaller than any input value then you will return a wrong result.
You can either set it to an arbitrary large number or use the first value in your input set to avoid the problem.

Related

LC-3 How to store a number large than 16-bit and print it out to console?

I'm having difficulty storing and displaying numbers greater than 32767 in LC-3 since a register can only hold values from -32768 to 32767. My apology for not being able to come up with any idea for the algorithm. Please give me some suggestion. Thanks!
You'll need a representation to store the larger number in a pair or more of words.
There are several approaches to how big integers are stored: in a fixed number of words, and in a variable number of words or bytes.  The critical part is being able to detect the presence and amount of overflow/carry on mathematical operations like *10.
For that reason, one simple approach is to use a variable number of words/bytes (for a single number), and store only one decimal digit in each of the words/bytes.  That way multiplication by 10, means simply adding a digit on the end (which has the effect of moving each existing digit to the next higher power of ten position).  Adding numbers of this form numbers is fairly easy as well, we need to line up the digits and then, we add them up and detect when the sum is >= 10, then there is a carry (of 1) to be added to the next higher order digit of the sum.  (If adding two such (variable length) numbers is desired, I would store the decimal digits in reverse order, because then the low order numbers are already lined up for addition.)  See also https://en.wikipedia.org/wiki/Binary-coded_decimal .  (In some sense, this is like storing numbers in a form like string, but using binary values instead of ascii characters.)
To simplify this approach for your needs, you can fix the number of words to use, e.g. at 7, for 7 digits.
A variation on (unpacked) Binary-coded Decimal to pack them two decimal digits per byte.  Its a bit more complicated but saves some storage.
Another approach is to store as many decimal digits as will fit full in a word, minus 1.  Which is to say if we can store 65536 in 16-bits that's only 4 full decimal digits, which means putting 3 digits at a time into a word.  You'd need 3 words for 9 digits.  Multiplication by 10 means multiplying each word by 10 numerically, and then checking for larger than 999, and if larger, then carry the 1 to the next higher order word while also subtracting 10,000 from the overflowing word.
This approach will require actual multiplication and division by 10 on each of the individual words.
There are other approaches, such as using all 16-bits in a word as magnitude, but the difficulty there is determining the amount of overflow/carry on *10 operations.  It is not a monumental task but will require work.  See https://stackoverflow.com/a/1815371/471129, for example.
(If you also want to store negative numbers, that is also an issue for representation.  We can either store the sign as separately known as sign-magnitude form (as in stored its own word/byte or packed into the highest byte) or store the number in a compliment form.  The former is better for variable length implementations and the latter can be made to work for fixed length implementations.)

How to figure out a previous value that is randomly generated without crazy amounts of recursion?

Let's say I need to generate 100 numerical values. The value that is generated each time must be greater than or equal to the previous generated value. The issue is is that each individual value MUST be generated on the spot when needed, so we do not store any of the data. I have a solution using recursion, but the issue is is that if I want to generate the 100th value, it will have to recursively go through and generate and check all of the previous values, which you can imagine is terrible. The issue is further complicated by the fact that, if I'm generating the 100th value, I don't know for sure that the 99th value is accurate. Perhaps the 99th value is smaller than the 98th value, so the 99th value must do that check - so when generating the 100th value, we must not only check the 99th value, but potentially the 98th value, etc... Is there any way of solving this more eloquently without generating and storing all of the data before hand? Thank you!
The value that is generated each time must be greater than or equal to the previous generated value.
Just generate a random displacement from the previous value; like:
next_value = previous_value + get_random_value_from_zero_to_whatever();
previous_value = next_value;
The problem you need to worry about (regardless of how you do it) is exhaustion - what happens when the previous value was the highest possible value (e.g. INT_MAX if you're using int). Do you continually use the same highest possible value with no randomness at all (to avoid a smaller value), or...?
You actually only have to store one value, not all of them. The proof is by induction:
If you generate a value and it's the very first time, that value is trivially greater or equal to all previous
If you know the previously generated value v, which by induction is greater or equal to all previous values, then you merely have to generate a value larger than v for it to be greater or equal to all previous
You could also just generate 100 values and then sort them.

Finding duplicate digits in integers

Lets say we have an integer array of N elements which consists of integers between 0 and 10000. We need to detect the numbers including a digit more than once e.g 1245 is valid while 1214 is not. How can we do this optimally? Thanks!
You need two loops. One loop you scan each element of the array.
In the inner loop, you determine if for the given element it's valid or not based on the criteria you indicated. To determine if a number has the same digit more than once, you need a routine that effectively extracts each digit one by one. I think the most optimal way to do that is to do "mod 10" on the number, then loop dividing the original by 10. keep doing that until you don't have number left (zero). Now that you have a routine for looking at each digit of an integer, the way to determine if there are duplicate digits the most optimally is to create an array of 10 booleans. Start with a cleared array. For every digit, use it as an index into the bool array and set it to true. If you see "true" again in that spot before you set it, that means that element in the bool array was visited before, thus it's a duplicate digit. So you break out of the loop altogether and say you found an invalid value.

how to generate random numbers with a specified mean

I have a question like, I should genearate 'k' random numbers lets say it is from 1 to 1000. But the generated numbers should have a mean value of 300. I used rand() function to generate random numbers. But I am stuck with the mean value. How can I do so that the numbers generated have a mean value.
I'd generate k-1 random numbers, and then set the K number to be (mean*k-[sum of all the numbers you generated so far]).
Unfortunately, the C standard does not guarantee that the random numbers are uniform (it doesn't specify any distribution, for that matter), so the only way to do it is to generate the 1000 numbers in advance, calculate the mean (M) and subtract M-300 from every element

Best item from list based on 3 variables

Say I have the following for a bunch of items.
item position
item size
item length
A smaller position is better, but a larger length and size are better.
I want to find the item that has the smallest position, largest length and size.
Can I simply calculate a value such as (total - position) * size * length for each item, and then find the item with the largest value? Would it be better to work off percentages?
Either add a fourth item, which is your calculated value of 'goodness', and sort by that OR if your language of choice allows, override the comparason operators for sorting to use your formula and then sort. Note that the latter approach means that the function to determine betterness will be applied multiple times per item in the list, but it has the advantage of ease of making a procedural comparason possible (eg first look at the position, then if that is equal, look at size then length) - athough this could also be expressed as a formula resulting in a single number to sort by.
As for your proposed formula, note that each item has the same numerical weight even though they are measured on completely unrelated scales. Furthermore, all items with either position=total, size=0 or length=0 evaluate to zero.
If what you want is that position is the most important thing, but given equal positions, size is the next most important thing, but given equal positions and sizes, then go by length, this can be formulated into a single number as follows:
(P-position)*(S*L) + size*L + length
where L is a magic number that is greater than the maximum possible length value, S is a number greater than the maximum possible size value, and P is a number greater than the maximum possible position value.
If, on the other hand, what you want is some scale where the items are of whatever relative importances, one possible formula looks like this:
((P-position)/P)*pScale * (size/S)*sScale * (length/L)*lScale
In this version, P, S and L have much the same definitions as before - but it is very inmportant that the values of P, S and L are meaningful in a compatible way, e.g all very close to expected maximum values. pScale, sScale and lScale are there so you can essentially specify the relative importance of each item. They could all be 1 if all atems are equally important, in which case you could leave them out entirely.
As previously answered, though, there are also a potentially infinite number of other ways you could choose to code this. As a random example, for large sizes, length could become less important; those possibilities would require much additional thought as to what is actually meant by such a vague statement.

Resources