![]() |
|
[C] Sequence print - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Sequence print (/showthread.php?tid=6048255) Pages: Pages
1
2
|
Sequence print - ShallowME - Feb 10, 2020 I have an array, int dwArr[3] = {2,2,2}; I want the output as: 222 221 220 212 211 210 202 201 200 ... How would I go about this route? RE: Sequence print - dracx - Feb 10, 2020 Code D:\Scripts_Sources>python t.pyI dunno if I understood you right, but if the output you want is something like the above one, I have made some python script for it and it shouldn't be hard to convert it to C. Code arr = [2,2,2]Code #include <stdio.h>Got bored, lol. You possibly could make the code more dynamic by figuring out the lenght of the array and doing a loop accordingly. RE: Sequence print - Shisho - Feb 10, 2020 (Feb 10, 2020 07:34 AM)dracx Wrote: Nice try but he wants to print the descending sequence of numbers in groups of 3 where the next group has skipped 8 numbers. If you were really bored you would have done things more cleanly and provided a solution that the user could perhaps submit as a homework assignment :) I've written a solution, which I have commented, that is dynamic to the point where the array (as long as it's not multi-dimensional) can be as long as you want and the numbers inside of it can be as big as you want as long as joined together produce a value that is not larger than an integer. It properly manages memory by only allocating what is needed to produce the string and reallocates as needed to grow it. It then prints the values in descending order with the requested format! Code #include <stdlib.h>Have fun OP and hope you learned a couple of things! RE: Sequence print - dracx - Feb 10, 2020 (Feb 10, 2020 07:11 PM)Shisho Wrote: Nice try but he wants to print the descending sequence of numbers in groups of 3 where the next group has skipped 8 numbers. If you were really bored you would have done things more cleanly and provided a solution that the user could perhaps submit as a homework assignment :)I dont get it, lol, haha. first your code produces this: Code 222Mine produces this: Code 222The OP's sample output was Code 222My code may really not separeted the output properly (even tho it was on the python script), and it could be fixed by simply re-positioning the printf function (and as I assumed, nothing the OP could not handle). But about the "print the descending sequence of numbers in groups of 3 where the next group has skipped 8 numbers.", I thought he was just tryin to recursively descend each number to 0, and not -8 every after 3(you know what am saying, lol). And I was actually focusing on the algorithm more than the usability/dynamicity of the the code as I assumed, OP was only struggling on the algorithm to produce the desired output, so I just tried to show some. RE: Sequence print - Shisho - Feb 10, 2020 (Feb 10, 2020 09:07 PM)dracx Wrote: I dont get it, lol, haha. I stopped it at 200 but it can be easily changed to any number, even a negative value in the reverse for loop. We have no idea where he wants it to stop at do we? The reason I did this was to not flood my terminal but still show the functionality. His example prints 3 numbers, then it skips 8 and prints 3 more. The title is "Sequence print" not recursively subtract 1 :) RE: Sequence print - dracx - Feb 10, 2020 (Feb 10, 2020 09:30 PM)Shisho Wrote: I stopped it at 200 but it can be easily changed to any number, even a negative value in the reverse for loop. We have no idea where he wants it to stop at do we? The reason I did this was to not flood my terminal but still show the functionality. His example prints 3 numbers, then it skips 8 and prints 3 more. The title is "Sequence print" not recursively subtract 1 :) HAHAHAHA, we looked and understood his sample in different ways lol. I maybe would have thought of something similar like yours if he would have given 222 instead of {2,2,2}. RE: Sequence print - Shisho - Feb 10, 2020 (Feb 10, 2020 09:44 PM)dracx Wrote: HAHAHAHA, we looked and understood his sample in different ways lol. I maybe would have thought of something similar like yours if he would have given 222 instead of {2,2,2}. You are right, we did understand in different ways :) if he had just given us the number 222 it would have been way too easy. The array makes it more challenging! When he logs back in he can clear up what he wants :P RE: Sequence print - 0xDEAD10CC - Feb 12, 2020 This doesn't explain what should happen if the numbers in the array weren't the same (i.e. { 3, 2, 2 }), and the solutions posted aren't dynamic enough to account for an array that would contain 100 elements. And if the numbers in the array are always the same, why would you need an array in the first place as input? Just pass params to a function which specify the size and the number to start at. Btw, this is more of a recursive problem RE: Sequence print - dracx - Feb 15, 2020 (Feb 12, 2020 04:41 PM)0xDEAD10CC Wrote: This doesn't explain what should happen if the numbers in the array weren't the same (i.e. { 3, 2, 2 }), and the solutions posted aren't dynamic enough to account for an array that would contain 100 elements. Only if @OP gave more info tho. RE: Sequence print - Shisho - Feb 16, 2020 (Feb 12, 2020 04:41 PM)0xDEAD10CC Wrote: This doesn't explain what should happen if the numbers in the array weren't the same (i.e. { 3, 2, 2 }), and the solutions posted aren't dynamic enough to account for an array that would contain 100 elements. My solution is dynamic enough where it does not care how many elements there are in the array or the width of the elements. RE: Sequence print - 0xDEAD10CC - Feb 17, 2020 (Feb 16, 2020 10:41 PM)Shisho Wrote: My solution is dynamic enough where it does not care how many elements there are in the array or the width of the elements. Except it doesn't work:
RE: Sequence print - Shisho - Feb 17, 2020 (Feb 17, 2020 01:23 PM)0xDEAD10CC Wrote: Except it doesn't work: I read the scope of the program as building an integer from concatenation of each element of the array and then printing a decrementing sequence in the specified pattern. The description of what the user wanted is vague so I wrote the case I understood they wanted. From the example you’ve shown it built the integer “2222” and did exactly what the user showed in his example. RE: Sequence print - 0xDEAD10CC - Feb 17, 2020 (Feb 17, 2020 01:29 PM)Shisho Wrote: I read the scope of the program as building an integer from concatenation of each element of the array and then printing a decrementing sequence in the specified pattern. The description of what the user wanted is vague so I wrote the case I understood they wanted. From the example you’ve shown it built the integer “2222” and did exactly what the user showed in his example. Only for the first couple groups. At no point in the OP's example output did he show a numerical column increasing. Also, I didn't show what happens at the end values where the rows become 3 digits long instead of 4. From what I can tell, this is a recursive problem where if the leftmost column digit reaches 0 in the current row, the cycle will repeat with the next leftmost column with the rightmost columns resetting back to their initial values... All you need to do is watch the rightmost column in his example to reason with this; at no point does it reset to 9 (so why would the tens column reset to 9 with your code when dealing with 4 digits?) RE: Sequence print - Shisho - Feb 17, 2020 (Feb 17, 2020 02:33 PM)0xDEAD10CC Wrote: Only for the first couple groups. At no point in the OP's example output did he show a numerical column increasing. Also, I didn't show what happens at the end values where the rows become 3 digits long instead of 4. Can we assume it’s purely coincidence that it looks like he would want a recursive subtraction of 1 from each element? Even then it’s not because, like you said, the last element resets itself under what condition? There is no pattern there to suggest recursion. It’s simple arithmetic. It looks like he’s counting down from 222 in groups of 3 while then skipping 8 numbers. That is a sequence, not what you suggest. RE: Sequence print - 0xDEAD10CC - Feb 17, 2020 (Feb 17, 2020 02:54 PM)Shisho Wrote: Can we assume it’s purely coincidence that it looks like he would want a recursive subtraction of 1 from each element? Even then it’s not because, like you said, the last element resets itself under what condition? There is no pattern there to suggest recursion. I'm saying that this is a typical recursion problem similar to lexical permutations - the pattern is quite clear. The last elements reset once the current leftmost column hits 0 as I said; that's the condition. If it's a sequence, then why does the rightmost column not go "2, 1, 0, 9, ..."? Why would the tens column be any different? |