Printing the difference of adjacent numbers without using an array

I recently found a C-puzzle to write a program to read a set of integers x 0 , x 1 , x 2 , ....... before entering -1 as input.

After reading -1 program should print the difference between adjacent numbers. That is, x 1 -x 0 , x 2 -x 1 , x 3 -x <sub> 2sub> .....

for example: Input:
1 2 4 7 11 -1

Output
1 2 3 4

Result - result (2-1), (4-2), (7-4), (11-7)

The problem is that the program should not use an array. Even dynamically allocated arrays will not do.

I tried a lot, and this is what I came with

 #include<stdio.h> int a, b; int fn() { int r=ba; a=b; scanf("%d", &b); if(b!=-1) { printf("%d ", fn()); } return r; } int main() { scanf("%d%d", &a, &b); printf("%d ", fn() ); return 0; } 

The above program uses a recursive function, but in this method, since it is like a stack, the value that was last calculated is calculated first, instead of the value that was first calculated for printing. A.

those. the output for the same input as above is: 4 3 2 1 instead of 1 2 3 4

Is there a way to save the values ​​taken from this call stack (please correct me if Im not using the correct conditions) and click them again to unload the first calculated value, which will be the first,

For example: I got the values ​​4, 3, 2, and 1 with fn() , because it looked like a stack before 4 popped up:

  4
 3
 2
 1

Suppose I pop all the items from the stack and pop them onto another stack in the order in which they popped. Then the new stack will be

  1
 2
 3
 4

(i.e. 4 got pop-up and clicked first (& therefore, it turned out below), then 3 got pop-up and clicked and so on.)

If this could be done, we could just put the elements from the new stack and display them in the order in which they were displayed.

Note. The stack I'm referring to is a call stack, not an explicitly created stack (which probably would have to have an array).

Or maybe an easier way?

EDIT: I need the input and output phases to be separate and not interleaved. No need to output output before the end of the input is signaled with -1 .

EDIT: The program cannot use files to store input for later reading.

+5
source share
5 answers

Is there a way to save the values ​​taken from this call stack ... and push them onto the stack again so that when the first value is first extracted, the first one is selected?

Repeat each time a number is read (except -1).

Create a linked list consisting of variables in the previous recursion.

Arrays are not used, except for the printf() format.

It takes a bit more work to avoid a space after the last printed int .

 #include<stdio.h> #include<stdlib.h> typedef struct list { const struct list *prev; int i; } list; void print_list(const list *previous) { if (previous && previous->prev) { print_list(previous->prev); printf("%d ", previous->i - previous->prev->i); } } void JSfoo(const list *previous) { list node = { previous, 0 }; int cnt = scanf("%d", &node.i); if (cnt == 1 && node.i != -1) { JSfoo(&node); } else { print_list(previous); puts(""); } } int main(void) { JSfoo(NULL); return 0; } 

Output (non-interlaced)

 1 2 4 7 11 -1 1 2 3 4 

An alternative would be queuing to deny the need for recursive printing. The same way out.

The following is a circular queue. Please note that only 1 next is required. The queue handle should only point to the end of the queue. No need for pointers to the head and tail. Box O(1) .

 #include<stdio.h> #include<stdlib.h> typedef struct que { struct que *next; int i; } que; // Pass in the tail of the queue and return the updated tail // `tail` points to the head. (Circular queue) que *que_append(que *tail, que *node) { if (tail) { node->next = tail->next; tail->next = node; } else { node->next = node; } return node; } void JSfoo(que *tail) { que node; int cnt = scanf("%d", &node.i); if (cnt == 1 && node.i != -1) { tail = que_append(tail, &node); JSfoo(tail); } else if (tail) { que *head = tail->next; while (head != tail) { printf("%d ", head->next->i - head->i); head = head->next; } puts(""); } } 
+4
source

Do you expect the program to stop accepting any input characters from the user immediately after entering "-1", and then print diff seq out?

If not, that is, if you allow the user to enter more characters after "-1" so that your program is ignored until a new line is entered, at which point your program can print a sequence of differences. Then you can use the following snippet:

 unsigned int x, y; while( scanf("%d",&x)==1 && x != -1 && scanf("%d",&y)==1 && y != -1 ) printf("%d ",yx); printf("\n"); 

EDIT: thanks @ JoëlHecht for pointing out the error in the above code, I read the required output spec incorrectly, the following code should fix it:

 int x, y = -1; while( scanf("%d",&x)==1 && x != -1 ) { if( y != -1 ) printf("%d ",xy); y = x; } printf("\n"); 
+2
source

This is a classic problem requiring a recursive function. The recursive function (1) takes a value and gives a test for the exit condition to interrupt the recursion; (2) does some work on the data; then (3) calls itself again (makes a recursive call), forcing the process to repeat.

In your case, the value that is required for the recursive function is (1) the previously read integer, and then it will read the current value from stdin (checking and processing any error ) and checking whether current value of -1 for the exit condition. Then the function should (2) print the difference between current and previous . Then it is necessary (3) to pass the value of current as an argument to the recursive function, calling itself again until the exit condition is satisfied.

By combining these fragments, you can do something similar to the following difference() function:

 #include <stdio.h> #include <stdlib.h> /* for EXIT_FAILURE */ /* recursive function taking previous read value as input */ void difference (int prev) { int current; /* variable to hold current value */ if (scanf ("%d", &current) != 1) { /* read / validate input */ fprintf (stderr, "error: invalid input.\n"); exit (EXIT_FAILURE); } if (current == -1) /* recusion exit condition */ return; printf (" %d", current - prev); /* print difference */ difference (current); /* recursive call passing current */ } int main (void) { int n; /* variable for first value */ if (scanf ("%d", &n) != 1) { /* read / validate input */ fprintf (stderr, "error: invalid input.\n"); exit (EXIT_FAILURE); } if (n == -1) /* verify not -1 */ return 0; difference (n); /* recursive call passing first value */ putchar ('\n'); /* tidy up */ return 0; } 

Usage / Output Example

 $ ./bin/diffints 1 2 4 7 11 -1 1 2 3 4 

Using the helper function to trigger recursion

If you want to embellish things a bit in main() , you can create a helper function that will read the first value and begin the recursion. This would leave you only one call to the helper function in main() . This does not change the program at all. Instead of invoking and checking the scanf return and making the first difference call in main() , these tasks simply carry over to a new function called diffcheck() , which then calls difference() to start the recursion. Totally up to you.

 #include <stdio.h> #include <stdlib.h> /* for EXIT_FAILURE */ /* recursive function taking previous read value as input */ void difference (int prev) { int current; /* variable to hold current value */ if (scanf ("%d", &current) != 1) { /* read / validate input */ fprintf (stderr, "error: invalid input.\n"); exit (EXIT_FAILURE); } if (current == -1) /* recusion exit condition */ return; printf (" %d", current - prev); /* print difference */ difference (current); /* recursive call passing current */ } /* helper function to read 1st value and call difference */ void diffcheck (void) { int n; /* variable for first value */ if (scanf ("%d", &n) != 1) { /* read / validate input */ fprintf (stderr, "error: invalid input.\n"); exit (EXIT_FAILURE); } if (n == -1) /* verify not -1 */ return; difference (n); /* recursive call passing first value */ putchar ('\n'); /* tidy up */ } int main (void) { diffcheck(); return 0; } 

note: as main() comes down to a single diffcheck() call, which is much cleaner than repeating a read / check and first check wherever you want to call the difference function in all of your code.

Take a look by going through the recursive function operation for difference() , and let me know if you have any further questions.

+1
source

Program will

 #include <stdio.h> int main() { unsigned int x, y; if(!(scanf("%d",&x)==1 && x != -1)) return 0; while(scanf("%d",&y)==1 && y != -1 ) { printf("%d ",yx); x = y; } printf("\n"); } 
0
source

Here is what I came up with:

 #include <stdio.h> int a, b; void fn() { int r=ba; a=b; scanf("%d", &b); printf("%d ", r); if(b!=-1) fn(); } int main() { scanf("%d%d", &a, &b); fn(); printf("\n"); return 0; } 
-1
source

Source: https://habr.com/ru/post/1270662/


All Articles