ParsX.com
پذیرش پروژه از دانشجویی ... تا سازمانی 09376225339
 
   ProfileProfile   Log in to check your private messagesLog in to check your private messages  |  FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups Log inLog in   RegisterRegister 

پاسخ تمرين هاي توابع بازگشتي

 
Post new topic   Reply to topic    ParsX.com Forum Index -> C/C++ Programming
View previous topic :: View next topic  
Author Message
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Mon Dec 13, 2004 11:19 pm    Post subject: پاسخ تمرين هاي توابع بازگشتي Reply with quote

تمرین 1: تابعی برای محاسبه k امین جمله سری فیبوناچی بنویسید ؟ ( تابع را به صورت بازگشتی بنویسید )

#include<stdio.h>
#include<conio.h>
long int febo(int);
main()
{
 int x=1;
 long int y=1;
 clrscr();
 printf("\n\n\t Enter a number : ");
 scanf("%d",&x);
         if((x>2)||(x<26))
                     y=febo(x);
 printf(" \n\n\n\t\t\t Febo = %d",y);
 if((x<1)||(x>=26))
 {
   clrscr();
   printf("\n\n\n\n\n\n\t\t Error !!! ");
  }
 getch();
 return 0;
}
             long int febo(int n)
                    {
                       if(n>2)return(febo(n-1)+febo(n-2));
                       return 1;
                    }
 



Last edited by arash on Tue Dec 14, 2004 10:11 pm; edited 3 times in total
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Tue Dec 14, 2004 4:46 pm    Post subject: Reply with quote

<<<<پاسخ باقيمانده تمرينها رو در صورتيكه بيشتر از پنج نفر بخواهند مينويسم >>>>
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Sun Dec 19, 2004 12:43 am    Post subject: Reply with quote

Evil or Very Mad Evil or Very Mad Evil or Very Mad فکر نمی کنم زدن دکمه ارسال پاسخ و نوشتن چند کلمه انقدر ها هم کار سخت باشه نه !!!؟؟؟ Evil or Very Mad Evil or Very Mad Evil or Very Mad

Last edited by arash on Sun Dec 19, 2004 1:39 am; edited 4 times in total
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Sun Dec 19, 2004 1:31 am    Post subject: حل مثال فوق ... Reply with quote

اولا اگر می بینید جواب مثالها رو نگفتم به خاطر اینکه نمی خوام از حرفی که زدم برگردم ، وگرنه منظور خاصی ندارم .
مثالی که در قسمت بالا نوشته شده کاملا صحیح می باشد . فقط آقا احسان نکته جالبی رو گفتن :
آقا احسان گفت که ما k امین جمله سری فیبوناچی رو نمی خواهیم بلکه مجموع k جمله اول سری رو می خواهیم . پس مثال فوق به این شکل تغییر می کنه :

مثال : برنامه ای بنویسید که مجموع k جمله اول سری رو محاسبه کند ؟

#include<stdio.h>
#include<conio.h>
int febo(int);
int f(int);
main()
{
int x=1,k=1,i;
do{
clrscr();
printf("\n \n Enter a number : ");
scanf("%d",&x);
}while(x>21);
if(x>1)k=febo(x);
printf(" \n \n \n \t Sum(");
for(i=1;i<x;printf("%d+",f(i)),++i);
printf("%d)= %d\n",f(x),k);
getch();
return 0;
}
int f(int x)
{
if(x>2)return f(x-1)+f(x-2);
return 1;
}
int febo(int x)
{
if(x>2)return febo(x-1)+f(x);
return 2;
}

و خروجی برنامه به شکل زیر می شود :



Last edited by arash on Sun Dec 19, 2004 11:46 pm; edited 1 time in total
Back to top
Mahnazz
مهمون يكي دو روزه


Joined: 04 Dec 2004
Posts: 10

PostPosted: Sun Dec 19, 2004 10:19 am    Post subject: Reply with quote

سلام اقا ارش . دستتون درد نكنه . كه تمرينها رو جواب ميديد . راستش اگه ميشه من رو دو نفر حساب كنيد . چون منكه جواب تمرين رو ميخواهم . يكي ديگه از دوستهام هم هست كه جواب رو ميخواد منتهي به اينترنت فعلا دسترسي نداره . مرصي ...
Back to top
unknown
مدير بخش ويژوال بيسيك
مدير بخش ويژوال بيسيك


Joined: 05 Dec 2004
Posts: 439
Location: Tehran

PostPosted: Sun Dec 19, 2004 7:10 pm    Post subject: Reply with quote

آرش جون خیلی باحالی...
دوست دارم.
Back to top
unknown
مدير بخش ويژوال بيسيك
مدير بخش ويژوال بيسيك


Joined: 05 Dec 2004
Posts: 439
Location: Tehran

PostPosted: Sun Dec 19, 2004 7:40 pm    Post subject: Reply with quote

آرش جون شرمنده ولی برنامت x=2 رو غلط حساب میکنه.
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Sun Dec 19, 2004 10:19 pm    Post subject: Reply with quote

ممنونم امیر جان ...
مثال رو تصحیح کردم فقط به جای 2 باید 1 می نوشتم ...
جواب برات ارسال میشه فقط امید وارم ایمیلت رو چک کنی ...
یکی می گفت از هر دست بدی بیشترش رو از اون دست پس می گیری ....
بازم ممنوم امیر آقا ...
Back to top
lkjk123456
مهمون يكي دو روزه


Joined: 16 Aug 2005
Posts: 8

PostPosted: Thu Aug 18, 2005 12:13 am    Post subject: Reply with quote

دستت درد نکنه امیدوارم بازم از این کارا بکنی
Back to top
Display posts from previous:   
Post new topic   Reply to topic    ParsX.com Forum Index -> C/C++ Programming All times are GMT + 3.5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum