Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Sequence print

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?
Code
D:\Scripts_Sources>python t.py
2 2 2
2 2 1
2 2 0


2 1 2
2 1 1
2 1 0


2 0 2
2 0 1
2 0 0


============

1 2 2
1 2 1
1 2 0


1 1 2
1 1 1
1 1 0


1 0 2
1 0 1
1 0 0


============

0 2 2
0 2 1
0 2 0


0 1 2
0 1 1
0 1 0


0 0 2
0 0 1
0 0 0


============


D:\Scripts_Sources>

I 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]
arrlen = len(arr) - 1

for i in range(0, arr[0]+1):
    first = arr[0]-i
    for j in range(0, arr[1]+1):
        second = arr[1]-j
        for k in range(0, arr[2]+1):
            third = arr[2]-k

            print(first, second, third)
        print("\n")
    print("============\n")

Code
#include <stdio.h>

int main(){
    int arr[3] = {2, 2, 2};
    
    for (int i = 0; i <= arr[0]; i++){
        int first = arr[i]-i;
        for (int j = 0; j <= arr[1]; j++){
            int second = arr[1] - j;
            for (int k = 0; k <= arr[2]; k++){
                int third = arr[2] - k;
                // Could proly use sprintf to concat these values if you want em inside a variable
                printf("%d%d%d\n", first, second, third);
            }
        }
        printf("\n");
    }

    return 0;
}

Got bored, lol.

You possibly could make the code more dynamic by figuring out the lenght of the array and doing a loop accordingly.
(Feb 10, 2020 07:34 AM)dracx Wrote:  
Code
D:\Scripts_Sources>python t.py
2 2 2
2 2 1
2 2 0


2 1 2
2 1 1
2 1 0


2 0 2
2 0 1
2 0 0


============

1 2 2
1 2 1
1 2 0


1 1 2
1 1 1
1 1 0


1 0 2
1 0 1
1 0 0


============

0 2 2
0 2 1
0 2 0


0 1 2
0 1 1
0 1 0


0 0 2
0 0 1
0 0 0


============


D:\Scripts_Sources>

I 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]
arrlen = len(arr) - 1

for i in range(0, arr[0]+1):
first = arr[0]-i
for j in range(0, arr[1]+1):
second = arr[1]-j
for k in range(0, arr[2]+1):
third = arr[2]-k

print(first, second, third)
print("\n")
print("============\n")

Code
#include <stdio.h>

int main(){
int arr[3] = {2, 2, 2};

for (int i = 0; i <= arr[0]; i++){
int first = arr[i]-i;
for (int j = 0; j <= arr[1]; j++){
int second = arr[1] - j;
for (int k = 0; k <= arr[2]; k++){
int third = arr[2] - k;
// Could proly use sprintf to concat these values if you want em inside a variable
printf("%d%d%d\n", first, second, third);
}
}
printf("\n");
}

return 0;
}

Got bored, lol.

You possibly could make the code more dynamic by figuring out the lenght of the array and doing a loop accordingly.

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>
#include <stdio.h>

int array[3] = {2,2,2};

int main() {

        int arraySize = sizeof(array) / sizeof(array[0]); //get the length of the array by dividing the total size by the size of one of the elements

        char *integerString = NULL; //create a pointer to a string that we will build that will hold each element consecutively to build a new number
        int integerStringSize = 0; //create an int to hold the size of the string as we grow it
        int offset = 0; //create an int that will hold an offset so that we can add each element to the end of the old element

        for(int i = 0; i < arraySize; i++) { //iterate over each element of the array

                int integerSize = snprintf(NULL, 0, "%d", array[i]); //get the length of the integer within the element
                integerStringSize += integerSize; //increment the int holding the current size of the string we are building

                char *newPtr = NULL; //allocate a new pointer for the following realloc()
                newPtr = realloc(integerString, integerStringSize + 1); //reallocate the string we are building to hold the element
                integerString = newPtr; //assign the old pointer to the new pointer

                snprintf(integerString + offset, integerStringSize + 1, "%d", array[i]); //copy the value of the element to the string

                offset += integerSize; //increase the offset so we can add the following element to the end
        }

        int newInteger = atoi(integerString); //convert string to new integer

        int counter = 0; //create a counter so we can print only the numbers we want by incrementing it each iteration of the following loop and checking the current value

        for(int i = newInteger; i >= 200; i--) { //loop from the new integer backwards until you reach whatever number you want

                if(counter < 3) { //print a number if counter isn't 3
                        printf("%d\n", i);
                }

                counter++;

                if(counter == 10) { //reset the counter and print a new line if we have skipped 8 numbers
                        counter = 0;
                        printf("\n");
                }
        }
}

Have fun OP and hope you learned a couple of things!
(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'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>
#include <stdio.h>

int array[3] = {2,2,2};

int main() {

       int arraySize = sizeof(array) / sizeof(array[0]); //get the length of the array by dividing the total size by the size of one of the elements

       char *integerString = NULL; //create a pointer to a string that we will build that will hold each element consecutively to build a new number
       int integerStringSize = 0; //create an int to hold the size of the string as we grow it
       int offset = 0; //create an int that will hold an offset so that we can add each element to the end of the old element

       for(int i = 0; i < arraySize; i++) { //iterate over each element of the array

               int integerSize = snprintf(NULL, 0, "%d", array[i]); //get the length of the integer within the element
               integerStringSize += integerSize; //increment the int holding the current size of the string we are building

               char *newPtr = NULL; //allocate a new pointer for the following realloc()
               newPtr = realloc(integerString, integerStringSize + 1); //reallocate the string we are building to hold the element
               integerString = newPtr; //assign the old pointer to the new pointer

               snprintf(integerString + offset, integerStringSize + 1, "%d", array[i]); //copy the value of the element to the string

               offset += integerSize; //increase the offset so we can add the following element to the end
       }

       int newInteger = atoi(integerString); //convert string to new integer

       int counter = 0; //create a counter so we can print only the numbers we want by incrementing it each iteration of the following loop and checking the current value

       for(int i = newInteger; i >= 200; i--) { //loop from the new integer backwards until you reach whatever number you want

               if(counter < 3) { //print a number if counter isn't 3
                       printf("%d\n", i);
               }

               counter++;

               if(counter == 10) { //reset the counter and print a new line if we have skipped 8 numbers
                       counter = 0;
                       printf("\n");
               }
       }
}

Have fun OP and hope you learned a couple of things!
I dont get it, lol, haha.

first your code produces this:
Code
222
221
220

212
211
210

202
201
200

Mine produces this:
Code
222
221
220
212
211
210
202
201
200

122
121
120
112
111
110
102
101
100

022
021
020
012
011
010
002
001
000

The OP's sample output was
Code
222
221
220

212
211
210

202
201
200
...          // <---- This probably says that there should be more output right?

My 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.
(Feb 10, 2020 09:07 PM)dracx Wrote:  I dont get it, lol, haha.

first your code produces this:
Code
222
221
220

212
211
210

202
201
200

Mine produces this:
Code
222
221
220
212
211
210
202
201
200

122
121
120
112
111
110
102
101
100

022
021
020
012
011
010
002
001
000

The OP's sample output was
Code
222
221
220

212
211
210

202
201
200
...          // <---- This probably says that there should be more output right?

My 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.

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 :)
(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}.
(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
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
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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.

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

Only if @OP gave more info tho.
(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.

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

My solution is dynamic enough where it does not care how many elements there are in the array or the width of the elements.
(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:
[Image: 2GxM6Q9.png]
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Feb 17, 2020 01:23 PM)0xDEAD10CC Wrote:  Except it doesn't work:
[Image: 2GxM6Q9.png]

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.
(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?)
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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.

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?)

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.
(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.

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.

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?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by unknown321Dec 8, 2019 01:35 PM 11 467 Dec 8, 2019 01:35 PM
Last Post: 0xDEAD10CC
[Java] by cryptobassboostMay 20, 2019 11:49 AM 4 248 May 20, 2019 11:49 AM
Last Post: bluriy
[Python] by F.A.S.TApr 15, 2019 01:25 PM 0 77 Apr 15, 2019 01:25 PM
Last Post: F.A.S.T