Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

C Code inquiry on how to continue with this program

I have some code here that I need to fill in but I have no idea how to call a function from another function from the outside of main, Im a total beginner on this and any help would be appreciated.


Code
    #include <stdio.h>
    #include <stdlib.h>

    /* void ArithmeticSequence(start, increment, count)

       Print an arithmetic sequence with the provided properties:
        - start: first value in the sequence
        - increment: the increment value between each pair of values in the sequence
        - count: number of terms to generate

    */
    void ArithmeticSequence(int start, int increment, int count){

        int k;
        int a;

        printf("Arithmetic Sequence: \n");
        for (k=0; k<count; k++ ){
            printf("%5d", a);
            a = a + increment;

        }
        printf("\n");

    }

    /* HarmonicSequence()

       Print the first 10 entries of the harmonic sequence.

    */
    void HarmonicSequence(){
        /* Your code here */
    }


    /* LeonardoSequence()

       Print the first 10 entries of the Leonardo sequence.

    */
    void LeonardoSequence(){
        /* Your code here */
    }

    /* Do not change any code below this line */
    int main(){
        ArithmeticSequence(17, 6, 5);
        ArithmeticSequence(10, 6, 10);
        HarmonicSequence();
        LeonardoSequence();
        return 0;
    }
    return EXIT_SUCCESS;
}

The output is supposed to look something like this:

Arithmetic Sequence: 17 23 29 35 41
Arithmetic Sequence: 10 16 22 28 34 40 46 52 58 64
Harmonic Sequence: 1.00 0.50 0.33 0.25 0.20 0.17 0.14 0.12 0.11 0.10
Leonardo Sequence: 1 1 3 5 9 15 25 41 67 109
(Sep 27, 2016 06:17 PM)Flash7475 Wrote:  I have no idea how to call a function from another function from the outside of main

You call it the same way. What I don't understand why would you need to call function from a function in this example.

For harmonic sequence just create a loop that will divide 1/n and print it out.
For leonardo sequence you need to use 2 variables in a loop.

Since these 2 functions don't require input you have them called from main and that's it. If you really have no idea how to do what I just explained I can give you code but I don't know how much it's gonna help you.

(Sep 27, 2016 06:26 PM)SIM-574702 Wrote:  You call it the same way. What I don't understand why would you need to call function from a function in this example.

For harmonic sequence just create a loop that will divide 1/n and print it out.
For leonardo sequence you need to use 2 variables in a loop.

Since these 2 functions don't require input you have them called from main and that's it. If you really have no idea how to do what I just explained I can give you code but I don't know how much it's gonna help you.

I dont understand how the functions relate to one another. is the (int start;int increment;int count) variable part already defined by what is in the "int main "function? would you be able to show me how the first set is done? Also is there something worng with the code I have in the first example since its giving me no output.
I don't want to insult you but you're missing core concept of using functions. I'll explain it the way I know it but if you're serious about coding you should read it from better source.

When creating function you decide what it will take as input.
Code
void ArithmeticSequence(int start, int increment, int count){
//Code for sequence
}

So when you want to call function, it doesn't matter if you're in main or any other function you have to call it with 3 integer parameters. Let's say you're in main and you call function ArithmeticSequence(17,6,5) this is same as having
Code
void ArithmeticSequence(){
int start = 17;
int increment = 6;
int count = 5;
//Rest of code...
}


The point of having variables as parameters is so you can use same function in multiple cases without changing it.
And yes, while in function you use the name of variable you have in function declaration.

There's no way to call ArithmeticSequence() from the other functions and achieve that output because:

1. "Arithmetic Sequence" is outputted to STDOUT each time that function is called, and there is no such output after the first 2 calls in main
2. Harmonic sequence deals with floating point data, and Arithmetic Sequence deals with only integers.

Hardcode it into your 2 functions.

Btw, there's an extra brace at the end of the code and return EXIT_SUCCESS it out of proper function scope.

For example though, here's something like what the first one should look like:
Code
void HarmonicSequence()
{
    /* Your code here */
    int i;
    printf("Harmonic Sequence:");
    for (i = 1; i <= 10; ++i)
    {
        printf(" %.2f", 1.0 / i);
    }
    printf("\n");
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by CYBERWAR WEAPONSApr 15, 2026 06:08 PM 0 139 Apr 15, 2026 06:08 PM
Last Post: CYBERWAR WEAPONS
by EnigmatikkJun 22, 2025 08:59 AM 12 323 Jun 22, 2025 08:59 AM
Last Post: ʀ00ȶʐɨʟʟǟ
[C#] by randomvodkaloverAug 8, 2020 11:32 PM 8 240 Aug 8, 2020 11:32 PM
Last Post: randomvodkalover