Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# I suck, plz help lol...

### #1 yafim Posted 24 September 2005 - 07:26 AM

yafim

Member

• Member
• 116 posts
Ok, first of all tnx to the forum for helping me getting the Turbo C program installed. Second of all, I'm trying to learn this Recursive thing myself... because my teacher is a complete idiot and I obvioiusly won't learn anything from her.
So i'm learning from scratch, from my workbook, but it's confusing me.

I had an excersize to do: "write a recursive sub-program that gets a number (n) and prints all the whole numbers from n to 1. meaning if n=5 it should print 5 4 3 2 1.

This is the workbook solution:
{
if(n>0)
{
printf("%d", n);
printf(n-1);
}
}

My solution was this: (they only gave the sub-program, excluding the program body so i'm not sure if the sub-program is void or int or w/e...)
mine didn't work:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int num(int n);
void main(){
int n,s;
clrscr();
printf("Enter a number:");
scanf("%d", &n);
s=num(n);
printf("%d", s);
getch();
}
int num(int n)
{
if (n>0){
printf("%d", n);
return n-1;
}

I don't know what i did wrong, cause i tried to do it like they wanted and it just printed "55555" endlessly, and my method prints out "54" only...
• 0

### #2 darth_ash Posted 24 September 2005 - 11:24 AM

darth_ash

Member 1K

• Member
• 1,382 posts
Recursive fuction is a fuction that calls itself; if you notice your code, there is no statement in the defination of num where it calls itself.

Here is one possible solution:
```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void num(int n);

void main(){
int n,s;
clrscr();
printf("Enter a number:");
scanf("%d", &n);
num(n);
getch();
}

void num(int n)
{
if (n>0){// this is the exit condition for ending recursion
printf("%d", n);
n--;
num(n);//this step is what makes a fuction recursive
}
}```

Edited by darth_ash, 24 September 2005 - 11:25 AM.

• 0

### #3 yafim Posted 24 September 2005 - 01:55 PM

yafim

Member

• Topic Starter
• Member
• 116 posts
can i write instead of n--, in the recursive func. line "num(n-1)" ?

Edited by yafim, 24 September 2005 - 01:55 PM.

• 0

darth_ash

Member 1K

• Member
• 1,382 posts
Sure you can.
• 0

### #5 yafim Posted 24 September 2005 - 02:00 PM

yafim

Member

• Topic Starter
• Member
• 116 posts
Hey, since i'm in school learning all of this recursion, and i'm having difficulties, i might have tons of questions all the time, is it ok for me to post alot here like i did with this problem or is that flooding?
• 0

### #6 darth_ash Posted 24 September 2005 - 02:23 PM

darth_ash

Member 1K

• Member
• 1,382 posts
No, problem.
Keep posting.
Thats what the forums are for.

But it would be better if you stick to a single thread; because when other users will be helping you they have an idea about your history.

C\C++ are wonderful languages; enjoy them
• 0

### #7 yafim Posted 30 September 2005 - 08:38 AM

yafim

Member

• Topic Starter
• Member
• 116 posts
Ok, problems lolz..
i have like 50 excersizes to hand in and i'm already really late behind everyone else... ... this is the first one i tried and i still can't do it:

Get number n, and print out sum of digits in the number by using a recursive function:

this is what i tried:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int sum(int n);
void main(){
int n,x;
printf("Enter a number:");
scanf("%d", &n);
x=sum(n);
printf("The sum of digits is %d", x);
}
int sum(int n)
{
int x=0;
x=x+(n%10);
n=n/10;
return sum(n);
}

It doesn't work, i run it and get a screen saying "Enter a number:", i enter "123" and then it drop one line but it's blank and the program doesn't do anything else..

(btw, how do i break a running program w/o exiting the TC?)

Edited by yafim, 30 September 2005 - 08:40 AM.

• 0

### #8 comanighttrain Posted 01 October 2005 - 01:05 PM

comanighttrain

Member

• Member
• 553 posts
hrmm...im not so good with the modulus operator (never got to grips with that...yet i program GUI's in C++). as for your second question, make a pause() function if your compiler doesnt come with one, to do this, simple use printf and getch to enter a loose character.
• 0

### #9 yafim Posted 01 October 2005 - 01:17 PM

yafim

Member

• Topic Starter
• Member
• 116 posts
What? i didn't understand what you wanted me to do... and will that solve the program, or break the run? (which question were you reffering to?).
• 0

### #10 darth_ash Posted 02 October 2005 - 09:27 AM

darth_ash

Member 1K

• Member
• 1,382 posts
HI yafim,
Your program is going in a infinite loop because there is no exit condition for recursion. Also, where is the recursive call step?

Since this is homework assignment, an its main objective is for you to learn, I won't give you the full-code this time, but I'll give you some clues on what is wrong in your program.

You made the same mistake as last time, no statement of recursion and no exit condition for recursion.

`return sum(n)`
This step is causing of the infinite loop, and you program will go into an infinite even if you have a exit condition with this line. As your logic(approach) for the summing seems ok, I suggest you re-think your logic(approach) for the recursion part.

`int x=0;`
Your resetting the value of x on every recursion, you will loose the previous value of x in the next recursion.
I'll give you a clue here, use the static data-type modifier.

Also since your program is gone in an infinite loop; use Ctrl+Break, to exit.

Edited by darth_ash, 02 October 2005 - 11:55 AM.

• 0

### #11 comanighttrain Posted 02 October 2005 - 10:53 AM

comanighttrain

Member

• Member
• 553 posts
itllstop the run so u can see whats going on
• 0

### #12 yafim Posted 02 October 2005 - 12:00 PM

yafim

Member

• Topic Starter
• Member
• 116 posts
What's a static data-type modifier?
• 0

### #13 yafim Posted 02 October 2005 - 12:13 PM

yafim

Member

• Topic Starter
• Member
• 116 posts
New program:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int sum(int n, int y);
void main(){
int n,x,y=0;
clrscr();
printf("Enter a number:");
scanf("%d", &n);
x=sum(n, y);
printf("The sum of digits is %d", x);
getch();
}
int sum(int n, int y)
{
if ((n/10)!=0){
y=y+(n%10);
n=n/10;
}
return sum(n,y);
}

now it still doesn't work.. i don't get it.
• 0

### #14 darth_ash Posted 02 October 2005 - 01:41 PM

darth_ash

Member 1K

• Member
• 1,382 posts
But still there some mistakes:

if ((n/10)!=0)

Your exit condition is actually termianting the recursion sequence one step early.
It should be
`if(n!=0)`

Also your recursive statement is outside the exit condition. So, again your fuction is really is'nt recursive.

Plus, you have given the exit-condition, but you have'nt given the steps to follow for the exit-condition.

Since you were close this time and they are some small changes, below is the modified qworking code for the sum function:
```int sum(int n, int y)
{
if (n!=0){    //note the change to the exit condition
y=y+(n%10);
n=n/10;
return sum(n,y);   //as I said you need to put the step of recursion within the exit-condition
}
return y;          //these are step(s) that should be followed if exit-condition is true.
}
```

Since you are not aware of the static data-type modifierI'll show you how to do it with the it, since its easier.
The static data-type modifier, makes a the value of a variable (not the variable itself) global to the entire program.
Below is how this program can be done using static data-type modifier:
```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int sum(int n);

void main(){
int n,x;
printf("Enter a number:");
scanf("%d", &n);
x=sum(n);
printf("The sum of digits is %d", x);
}

int sum(int n)
{

static int s=0;         /*This is the static data-type variable s,
even though its value to trying to be reset to 0 every recursion,
it wo'nt happen because I made it static*/

if (n!=0)
{
s=s+(n%10);
n=n/10;
sum(n);
}
return s;
}```

• 0

### #15 yafim Posted 03 October 2005 - 12:16 AM

yafim

Member

• Topic Starter
• Member
• 116 posts
Great! tnx a lot man, I finally understood, good thing btw not giving me the full solution right at the top, cause i wanna learn.
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users