Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

I suck, plz help lol...


  • Please log in to reply

#1
yafim

yafim

    Member

  • Member
  • PipPipPip
  • 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

Advertisements


#2
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 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

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 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

#4
darth_ash

darth_ash

    Member 1K

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

#5
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 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

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 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 :tazz:
  • 0

#7
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Ok, problems lolz..
i have like 50 excersizes to hand in and i'm already really late behind everyone else... :tazz:... 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

comanighttrain

    Member

  • Member
  • PipPipPip
  • 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

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 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

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 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

Advertisements


#11
comanighttrain

comanighttrain

    Member

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

#12
yafim

yafim

    Member

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

#13
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 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

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Good, your making progress. :tazz:
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

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 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

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP