# 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. ## 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

2. Originally Posted by jh0720
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.

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. 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.

5. 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. Originally Posted by Adak
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

7. As opposed to what? A backward function declaration? Lol

8. Originally Posted by Adak
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

9. 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. Originally Posted by Adak
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

11. > Sometimes I seriously suspect the whole OOP thing in C++ was made to promote job security for programmers.
"That Interview"

Popular pages Recent additions