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.