std::cout is out function

How can I make std::cout out function? I mean that std::cout should be only in main(). I commented this lines (12-13) below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int foo(int *poly, int *init_st, int *arr, int L) {
    int j = 0, count = 0;
    while(1) {
        j++;
        int fb = 0;

        for(int i = 0; i < L; i++) fb ^= (poly[i] * arr[L-i-1]);
        for(int i = 0; i < L; i++)
            arr[i] = arr[i+1];
            arr[L-1] = fb;

        //for(int i = 0; i < L; i++) std::cout << arr[L-i-1] << " ";
        //std::cout << '\n';

        count = 0;
        for(int i = 0; i < L; i++) {
            if((init_st[i] == arr[i])) count++;
        }
        if(count == L) break;
    }

    return 0;
}

int main() {
    int L, count = 0;
    int *poly, *init_st, *arr;
    std::cout << "Enter the length of foo: "; std::cin >> L;

    poly = new int[L];
    init_st = new int[L];
    arr = new int[L];
    
    std::cout << "Enter the feedback polynomial of foo: ";
    for(int i = 0; i < L; i++) std::cin >> poly[i];
                   
     foo(poly, init_st, arr, L);

     delete[] poly;
     delete[] arr;
     delete[] init_st;
}
Last edited on
just like any function...

1
2
3
4
5
6
void printit(int * arr, unsigned int L)
{
   for(unsigned int i = 0; i < L; i++) 
        std::cout << arr[L-i-1] << " ";
        std::cout << '\n';
}


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.
Last edited on
But when I call this function in main() I get only last result (only one). If I get it in foo() I have all results.
Your indentation is very misleading.
1
2
3
        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.
If I use

1
2
3
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



If I use it in main I get:

 
1 1 1
Last edited on
I thought we were talking about the printit function and lines 12 and 13 in your original post.
Pass it as argument:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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++.

Good luck!
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)
Topic archived. No new replies allowed.