Thread: Cyclic function

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    6

    Cyclic function

    Hello !
    I am a beginner in C programming and I came across a problem while taking a test. The requirement sounded something like this: Write a function that is called cyclically and displays the current value as well as the last two values of a variable. Does anybody have any tips on how to do this because I have absolutely no idea. Please keep in mind that I am new to this.
    Thank you !

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Step 1: Be sure the problem statement is completely understood.
    Step 2: Determine the solution to the problem, in general terms.
    Step 3: Refine the problem solution into discrete steps. A pen and paper will come in handy for this part.
    Step 4: Begin implementing the solution in code. Build the code gradually, testing as you go. A development process

    It sounds like your "cyclically" function should be able to take a value, since local variables need to be updated. You may also want to read up on static variables.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    6
    Trust me, I tried to wrap my mind around it. But I have no idea where to start at. For me, the question is very vague but this is the way they formulated it.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Perhaps start by writing a program that calls a "cyclically()" function, which simply receives and prints a value. Call this function a few times in a loop.

    If you can get that far, we can proceed from there.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    6
    So..maybe something like this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int cyclical(int);
    int main()
    {
        int x;
        while(x!=0)
    
        printf("x = %d \n", cyclical(x));
    
    
    }
    
    int cyclical(int n)
    {
    
        printf("Input value: ");
        scanf("%d", &n);
    
        return n;
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Not quite; I was thinking more along the lines of simply printing its argument:

    Code:
    void cyclical(int n)
    {
        printf("%d\n", n);
    }
    And instead of a while, use a for loop so you can just print it a certain number of times (maybe 5 times). No input is needed at this stage.

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    6
    Ok. How about this ?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void cyclical(int n);
    int main()
    {
        int i;
        for(i=0; i<5; i++)
            cyclical(i);
    
    
    }
    
    void cyclical(int n)
    
    {
        printf("%d\n", n);
    
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Perfect! Now you can continue to develop the function.

    You will need a way for your function to "remember" previous values. Have you looked into static variables yet?

  9. #9
    Registered User
    Join Date
    Feb 2017
    Posts
    6
    Kind of. I know that static variables keep their values between two function calls.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Correct (though I would say between successive function calls).

    According to the requirements you posted, the function needs to print the current value of a variable, as well as its previous two values.

    Think about how static variables might help achieve this.

  11. #11
    Registered User
    Join Date
    Feb 2017
    Posts
    6
    I honestly have no idea. Could you give me a small hint ?

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Sorry to step in when Matticus (the master!) is at work, but I would suggest firstly only saving the previous value instead of the two previous values.
    You need to define a static int variable in your function to hold the previous value.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    I gave you pretty much all the pieces, and algorism gave you the hint you requested. Let's see what you come up with.

    On a side note, there is a certain etiquette when posting to help forums, which includes not posting to multiple forums at once. If you want to get the most out of such communities, I suggest reading this (do not be taken aback by the title, it's actually a very informative read).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to find if array is cyclic
    By SA80 in forum C Programming
    Replies: 3
    Last Post: 05-05-2012, 03:53 AM
  2. Cyclic dependencies, observer, or...
    By g4j31a5 in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2011, 05:45 AM
  3. Cyclic Redundancy Check: disable
    By samuelmoneill in forum C Programming
    Replies: 5
    Last Post: 04-27-2009, 02:29 AM
  4. cyclic buffer
    By Fortune in forum C Programming
    Replies: 2
    Last Post: 04-23-2009, 05:19 AM
  5. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 05:01 PM

Tags for this Thread