• 02-16-2011
litzkrieg
ok so i have this code and i must be missing something, im just starting to program and need to make a similar program to this but i just cant understand too much of this code can someone run through it with me (like almost line for line after the first {.

Thank you heres the code its a factorial program
Sorry for not formatting it right, im fairly new here

Code:

``` . #include<stdio.h> main() { int n,a; int b(int ); printf("Enter positive number less than 10\n"); scanf("%d",&n); a=b(n); printf("\nResults of:  %d",n,a); } int b(int n) { int k; if(n==1) return(1); else k=n*b(n-1); return(k); }```
.
ok think i got it right this time

//*again thank you even just for looking*//
• 02-16-2011
anduril462
First, start by reading the forum guidelines, homework policy and "Posting code" section, then come edit your post to use proper code tags and indentation.

Some tips to start off:
It's int main(void) and a return 0; at the end.
Don't put a function prototype inside another function. Move "int b(int );" up above main, or move the whole definition of b up there.

As for what's happening, have you tried running through this by hand? If you go through line by line, keeping track of all the variables, you'll see what happens when you enter 1, 2, ..., 5 for n, and call b with these values. You'll probably recognize the pattern by that point.
• 02-16-2011
anduril462
I'll give you a B- for effort, since you nicely re-edited your post, but forgot to indent the code in your shiny new code tags:
Code:

```#include<stdio.h> int b(int); int main(void) {     int n,a;     printf("Enter positive number less than 10\n");     scanf("%d",&n);     a=b(n);     printf("\nResults of: %d",n,a);     return 0; } int b(int n) {     int k;     if(n==1)         return(1);     else         k=n*b(n-1);     return(k); }```
Now, given your previous post, I'm guessing you get the bulk of this (unless you copied that from somewhere -- for shame!). Why don't you write down what you think each line does and we'll help you if there's a problem. I'm feeling tempted to lower that effort grade.
• 02-16-2011
litzkrieg
no not trying to copy just trying to fully understand and learn it all, ok well the stuff im still not so sure on are would be:

1. "int b(int n)"..... does this simply set b equal to n?
2. "if(n==1)"...... the double equals signs mean what as compared to just "="?
3. difference between return 0 and return 1?
4. and variable k? like the printf function is told to display the result as variable a, where does this k from and why is it there? i can see what seems to be the main point of k how its really doing most the work of the program in the line "k=n*b(n-1);" but after "int b(int n)" i get really lost as to whats happening
• 02-16-2011
CommonTater
Quote:

Originally Posted by litzkrieg
no not trying to copy just trying to fully understand and learn it all, ok well the stuff im still not so sure on are would be:

1. "int b(int n)"..... does this simply set b equal to n?
2. "if(n==1)"...... the double equals signs mean what as compared to just "="?
3. difference between return 0 and return 1?
4. and variable k? like the printf function is told to display the result as variable a, where does this k from and why is it there? i can see what seems to be the main point of k how its really doing most the work of the program in the line "k=n*b(n-1);" but after "int b(int n)" i get really lost as to whats happening

I'm thinking you need to sit down with the books and this time actually learn C...
I don't know where you got that code from, but it's getting real obvious you didn't write it.
• 02-16-2011
litzkrieg
No i didnt is from my instructor for us to do our best over the week to understand and write a similar program on Friday really just looking for some understanding
• 02-16-2011
CommonTater

Whew... does he know it contains errors?
• 02-16-2011
litzkrieg
he wrote it in class and it works so i dont think he cared (and ill be honest no one really likes him or understands his accent so yea no one said anything) an i might have copied some stuff off the paper wrong to lol
• 02-16-2011
anduril462
So Tater's right. It's Wednesday and you have some time to read your books and some tutorials on functions and recursion in C.