if you want cout, you can't have it only in main the way you are thinking.
you can have main do the printing by returning arr to main and printing it there, but that does not seem to fit.
honestly I can't tell what you want to do here... why is your function int and only return 0? should it be void (no return type or value)? why the while(1)/break approach, it is convoluted.
for(int i = 0; i < L; i++)
arr[i] = arr[i+1];
arr[L-1] = fb;
Line 3 is not in the loop.
You're saying you called printit(arr, L); in main, after line 37 (but before line 40), and it only prints one result? Show the updated code that is still producing a wrong result.
for(int i = 0; i < L; i++)
arr[i] = arr[i+1];
arr[L-1] = fb;
in foo() I get:
1 2 3 4 5 6 7 8 9 10
Enter the length of foo: 3
Enter the feedback polynomial of foo: 1 0 1
Enter the initial state of foo at t = 0: 1 1 1
0 1 1
1 0 1
0 1 0
0 0 1
1 0 0
1 1 0
1 1 1
int foo(std::ostream& outs, int *poly, int *init_st, int *arr, int L) {
int j = 0, count = 0;
while(1) {
...
for(int i = 0; i < L; i++) outs << arr[L-i-1] << " ";
outs << '\n';
...
}
return 0;
}
int main() {
...
foo(std::cout, poly, init_st, arr, L);
A lot of times C++ will encapsulate things in a class, which you can then add an override function for to output. A simple example:
1 2 3 4 5 6 7 8 9 10
struct point
{
double x, y;
};
std::ostream& operator << ( std::ostream& outs, const point& p )
{
outs << "(" << x << "," << y << ")";
return outs;
}
For your code, however, that would be overkill.
Also, I did not bother to analyze your code, which some other answers here seem to do. Watching your indentation and braces are essential in C and C++.
do one thing and do it well
that's one of the purpose of «don't use i/o in functions»
your foo function (terrible name, by the way) is currently doing three things:
1- From an initial state, compute the next state (lines 5--10)
2- Print the current state (lines 12--13)
3- Find the length of the sequence, how many steps to repeat itself (lines 15--19)
consider separating each of those things into its own function.
1 2 3 4 5 6 7 8
current = initial
count = 0
//sequence = []
do:
current = next_state(current)
print(current) // sequence.push(current)
count += 1
while not equal(current, initial)