i'm confused about forward functions and integrating them in my loop

This is a discussion on i'm confused about forward functions and integrating them in my loop within the C Programming forums, part of the General Programming Boards category; -----<<<<< i'm writing a program to find the prime digital root, which is when you add all the indiv. #s ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    i'm confused about forward functions and integrating them in my loop

    -----<<<<<
    i'm writing a program to find the prime digital root, which is when you add all the indiv. #s of an integer until you reach a single digit number OR when you reach a prime number

    2^31 = (2147483648) = 2+1+4+7+4+8+3+6+4+8 = (47) = 4+7 = (11) = 1+1 = (2)

    the prime digital root is 47 because 47 is prime... but assuming 47 and 11 aren't prime, it would stop at 2 because it is a single digit number and that would be the prime digital root

    right now, my forward functions are countNumbers(counts the numbers) --> digitSum(adds the digits of an integer) --> primeChecker(checks if digitSum is prime) and then after that i'd just have to add one that checks whether it is a single digit or not

    i am confused about a few things:
    1.) what if i only want to run countNumbers() in my forward function to count the number of digits in the integer to put in my while loop in main. how would i do that?
    2.) how would i incorporate my forward functions into my while loop so that the program (not finished) actually adds the digits of an integer, checks if it's prime, checks if it's a single digit or not and then goes back to the sum of the digits of the integer and starts the process all over again?

    thanks!

    Code:
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ @
    @ Forward Function Declarations @
    @ @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    int countNumbers(unsigned int N)
    unsigned int digitSum(unsigned int N, unsigned int numberLength);
    unsigned int primeChecker(unsigned int digitSum);
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    The main function
    
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    int main(void)
    {
    unsigned int N, numberLength, duplicateN, digitSum, digitSumDuplicate, i, sum[max_array_length];
    int isPrime;
    FILE *input, *output;
    numberLength = 1; //initialize numberLength at 1
    isPrime = 0; //we assume number is not prime until proven by primeChecker()
    i = 0; //initialize first value in array sum[] to be 0
    
    if ((input = fopen("prime_root.dat", "r")) == NULL)
    {
    printf("Error: cannot read the file prime_root.dat\n");
    return 1;
    }
    
    if ((output = fopen("prime_root.out", "w")) == NULL)
    {
    printf("Error: cannot write to the file prime_root.out\n");
    fclose(input);
    return 1;
    }
    
    while()
    {
    fscanf(input, "&#37;d\n", &N);
    while(N != 0)
    {
    if (numberLength > 1)
    
    
    
    return 0;
    }
    
    
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ @
    @ Function Definitions @
    @ @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    /************************************************** ****************************
    *
    * Function countNumbers()
    *
    ************************************************** *****************************
    *
    * This function takes an integer N as input, counts how many
    * individual numbers it consists of and returns the number.
    *
    ************************************************** ****************************/
    int countNumbers(unsigned int N)
    {
    duplicateN = N;
    while(duplicateN >= 10)
    {
    duplicateN = duplicateN / 10;
    numberLength++;
    }
    return numberLength;
    }
    
    /************************************************** ****************************
    *
    * Function digitSum()
    *
    ************************************************** *****************************
    *
    * This function takes integers N and numberLength as input and ,
    * length of the integer, respectively, and adds the digits of the
    * input and returns the result
    ************************************************** ****************************/
    unsigned int digitSum(unsigned int N, unsigned int numberLength)
    {
    for(numberLength > 0)
    {
    sum[i] = N % 10;
    N = N / 10;
    i++;
    numberLength--;
    }
    for(i > 0)
    {
    digitSum += sum[i-1];
    i--;
    }
    return digitSum;
    }
    
    /************************************************** ****************************
    *
    * Function primeChecker()
    *
    ************************************************** *****************************
    *
    * This function takes integer digitSum and checks if it is the prime
    * number and then returns isPrime either 0 for no or 1 for yes.
    *
    ************************************************** ****************************/
    unsigned int primeChecker(unsigned int digitSum)
    {
    for(digitSumDuplicate = digitSum; digitSumDuplicate > 1; digitSumDuplicate--)
    {
    if(digitSum % digitSumDuplicate == 0)
    {
    isPrime = 1;
    }
    else
    {
    N = digitSum;
    }
    }
    return isPrime;
    }
    ----->>>>>
    :P :P
    Last edited by Salem; 12-15-2008 at 10:37 PM. Reason: Restore original edit

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jh0720 View Post
    i'm writing a program to find the prime digital root, which is when you add all the indiv. #s of an integer until you reach a single digit number OR when you reach a prime number

    2^31 = (2147483648) = 2+1+4+7+4+8+3+6+4+8 = (47) = 4+7 = (11) = 1+1 = (2)

    the prime digital root is 47 because 47 is prime... but assuming 47 and 11 aren't prime, it would stop at 2 because it is a single digit number and that would be the prime digital root

    right now, my forward functions are countNumbers(counts the numbers) --> digitSum(adds the digits of an integer) --> primeChecker(checks if digitSum is prime) and then after that i'd just have to add one that checks whether it is a single digit or not

    i am confused about a few things:
    1.) what if i only want to run countNumbers() in my forward function to count the number of digits in the integer to put in my while loop in main. how would i do that?
    2.) how would i incorporate my forward functions into my while loop so that the program (not finished) actually adds the digits of an integer, checks if it's prime, checks if it's a single digit or not and then goes back to the sum of the digits of the integer and starts the process all over again?

    thanks!
    When you divide a number in base 10 (our common numbering base), what have you got?

    ie:

    1250 / 10 = 125
    125 / 10 = 12
    12 / 10 = 1
    1 / 10 = 0

    So if I divided 1250 in a while loop, and counted how many loops before it had our original 1250 down to less than 1, wouldn't I have a count of how many digits were in the original number?

    When you mod a number, you will get the remainder: 1250 &#37; 10 = 0, and 125 % 10 = 5, etc.

    Try to make up a separate function for these tasks: isPrime(), etc. Then you can just call these functions from inside your big while loop.

    If you try to include code for all this in one while loop, I will ask God to have mercy on your madness.

    In pseudo code, something like this:

    While(some condition or just 1)
    Call isPrime()
    Call countDigits()
    Call addItUp()

    etc.

    Loop back.

    Break it down into small parts, and check each function and block of code, as you go along. Don't get 100 lines of code, with 100 errors, and then say "Ack! I need help!".

    That's not what a programmer does.
    Last edited by Adak; 12-15-2008 at 08:00 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    thanks!

    did you see my forward functions i put at the bottom? that's what I am trying to do

    how would i call my functions, though? for example, how would i get it so that i have

    while (# of digits < 1)
    call digitSum
    call primeChecker
    call digitCount (haven't written it yet) --> output: # of digit
    loops back

    how do i call upon my functions? is calling upon just one of the forward functions legal?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Please don't edit your code after you've got a "reply". Especially just deleting the whole post down to a couple of stupid smilies.

    Nobody else can learn anything, nobody else can contribute, and it makes existing replies which don't quote excessively look pretty stupid.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm very unclear what a "forward function" is. Never heard that term before.

    Any function can be called individually, as long as your function definitions and parameters, are set up properly - that is, the function definition (or prototype if you prefer that name), must be in place where C can find it, before the actual function call is reached.

    Say I had a main menu function and two other functions. Function 1 is the getData function, and function 2 is the printIt function.

    So in the main menu, I might have something like this:

    Enter Your Choice:

    get the choice

    if(choice needs more data)
    getData()


    if(choice needs NO more data)
    then just call printData()

    So in the main menu() or before it, I need a function definition for getData, and for printData.

    Now, say I wanted to always call printData() right from getData(). I could do that, no problem. Because C already had the printData() definition.

    But, if I put the definition for printData inside getData, (instead of menu() ), then C would flag that as an error when it went to printData via the second if statement in the menu function, and skipped over getData() completely (thus skipping over the function definition for printData().

    I'm unclear how to be more specific without actually doing your program. I need you to post a specific code problem, in order to be more helpful.

    And please forget about "forward functions", because I have no idea what that refers to.
    I suspect it's just a term you've latched onto, that means nothing.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Adak View Post
    I'm very unclear what a "forward function" is. Never heard that term before.
    I believe he means "forward function declaration", i.e. a function declaration.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As opposed to what? A backward function declaration? Lol

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Adak View Post
    As opposed to what? A backward function declaration? Lol
    Yes. If you define a function before it is used, there is no need for a declaration (though it still makes good sense). This is sometimes used in C++ when you need some kind of circular dependency between two classes.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, on the whole, I'll just say "God save us from C++'s excesses". Sometimes I seriously suspect the whole OOP thing in C++ was made to promote job security for programmers.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    Well, on the whole, I'll just say "God save us from C++'s excesses". Sometimes I seriously suspect the whole OOP thing in C++ was made to promote job security for programmers.
    Except sometimes you need it in C as well, if you have mutual pointers between two struct - you need to tell that struct B exists before you can let struct A have a pointer to struct B, and struct A needs to be known before struct B can hold a pointer to struct A.

    Rare, for sure - but it does exist (usually when you have a "parent" struct, and you need a way to get the parent from the child itself).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Sometimes I seriously suspect the whole OOP thing in C++ was made to promote job security for programmers.
    "That Interview"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21