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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:37 PM. Reason: Restore original edit

Popular pages Recent additions subscribe to a feed