Monday, May 24, 2010

C Programming Language: Using Recursion to Print the Fibonacci Series?

The Fibonacci series





0, 1, 1, 2, 3, 5, 8, 13, 21, …..





begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.





For this problem, we are asked to write a recursive function fib(n) that calculates the nth Fibonacci number. Recursion MUST be used.





In an earlier problem, we where asked to do the exact same thing, except we where to NOT use recursion. For that problem, I used the following code (between the dotted lines):





--------------------------------------...





main()


{


int c=1,f=0,s=1;


int i;


printf("%d%20d\n",f,f);


printf("%d%20d\n",s,s);


for (i=2; i%26lt;5||f%26lt;s; i++)


{


c=f+s;


f=s;


s=c;


printf("%d%20d\n",i,c);


}


}





--------------------------------------...





Which gave the correct output:








0 0


1 1


2 1


3 2


4 3


5 5


6 8


7 13


8 21


9 34


10 55


11 89


12 144


13 233


14 377


15 610


16 987


17 1597


18 2584


19 4181


20 6765


21 10946


22 17711


23 28657


24 46368


25 75025


26 121393


27 196418


28 317811


29 514229


30 832040


31 1346269


32 2178309


33 3524578


34 5702887


35 9227465


36 14930352


37 24157817


38 39088169


39 63245986


40 102334155


41 165580141


42 267914296


43 433494437


44 701408733


45 1134903170


46 1836311903


47 -1323752223














While I was able to come up with a code that works nonrecursively, I am not sure how to write a program that will print out the Fibonacci series by using recursion. Help would be greatly appreciated.

C Programming Language: Using Recursion to Print the Fibonacci Series?
That's interesting. Usually they have students try these methods in the opposite order, to show that recursive algorithms can be transformed into non-recursive ones.





The way to solve this is by going back to the mathematical definition of the Fibonacci series:


f(1) = 1


f(2) = 1


f(n) = f(n-1) + f(n-2)





So, to find a particular number in the Fibonacci series, you just need to calculate the previous two numbers and add them together.





If you take that last formula and simply translate it into a C function, you'll be most of the way there. Don't forget to treat n=1 and n=2 as special cases.
Reply:Simple: this is in my book:





//Begin function Fibonacci





int Fibonacci(int n)


{


if(n==0||n==1)


return 1;


else


return Fibonacci(n-1)+Fibonacci(n-2);


}





//End function Fibonacci
Reply:#include %26lt;iostream%26gt;





int main()


{


//define first two fibonacci series numbers.


int fib1 = 0;


int fib2 = 1;





//declare the variable to store the next number of fibonacci series


int fib3;





//declare the variable to store how many numbers to be printed. Default is 2.


int numbers = 2;





//the counter to keep track how many numbers are printed.


int counter = 2;





//Ask user how many numbers of the fibonacci series need to be printed.


std::cout %26lt;%26lt; "How many Fibonacci number you need ? : " ;





//Store the number.


std::cin %26gt;%26gt; numbers;





//If number entered is less than 3, exit the program.


if (numbers %26lt; 3) return 0;





//Print the first two element.


std::cout %26lt;%26lt; fib1 %26lt;%26lt; "\t" %26lt;%26lt; fib2;





//do-while loop to calculate the new element of the series and printing the same.


do {


counter++;


fib3 = fib1 + fib2;


std::cout %26lt;%26lt; "\t" %26lt;%26lt; fib3;


fib1 = fib2;


fib2 = fib3;


} while (counter %26lt;= numbers);





std::cout %26lt;%26lt; std::endl;


system("pause");


return 0;


}


C:\ access denied?

i'm not really sure when this started because i don't often access C:\ from My Computer but just a few days ago, whenever i click C:\ in My Computer, i keep getting the "C:\ Access Denied" message. I have granted all my accounts access and I am also logged in as an administrator. The only thing I did drastically different is install my Sony Ericsson PC Suite (which I have already uninstalled to see if it makes a difference but it didn't).





I have tried system restore and logging in safe mode but i still keep getting the message. I can open C:\ though when I type it into the address bar and I can also explore it, I just can't click it from My Computer. Any help would be greatly appreciated.





I'm running Windows XP SP2 %26amp; an NTFS drive.

C:\ access denied?
the few times i have seen this it has ALWAYS been do the the installation of a firewall such as norton suite , since your saying in your post that you have installed and uninstalled Ericsson pc suite my educated guess would be that their are traces of the firewall still siting in the registry that are not allowing access tot he C drive do to a corrupt registry key , i would try using a good trusted registry cleaner such as CCleaner this should fix your issue
Reply:ANYTIME YOU DO ANYTHING IT ACCESSES (C;)


THIS IS YOUR HARD DRIVE,,,,,,,,,,,,,,,,,,,,,,,,,,,,





DELETE ALL USER ACCOUNTS BUT NOT THE ADMINISTRATOR,,,,,,,,,,,,,,,,,,,,





CHECK PROPERTIES ON C ,,,,,,, GO TO SHARING AND SEE IF THE FOLDER IS CHECKED PRIVATE ,,,,,,,,,,





RESTART PC,,,,,,,,,,,,,,,,
Reply:Boot to safe mode, Start %26gt; Control... %26gt; User... %26gt; see if your admin is under computer admin. Delete other unknown accounts.





Open my computer, right-click C drive %26gt; properties %26gt; in security tab, make sure you have all permissions checked for yourself.
Reply:go to search..


click on all files and folder.


LOOK IN click on my computer


in the name of the file write Program files


let it search.


there u go u r in C.


u can explore c from there.. there is a way to unlock it


but itz too long


.

pokemon cards

C++: Is any compile time memory allocated for this type of function return value?

class c


{


c func()


{


return c();


}


}





I was rather amused to discover that this kind of object creation is allowed: as in return c(). This is obviously not an object declaration so I was wondering if memory is allocated at all compile time? I'm guessing that at runtime a temporary object automatically gets created just while returning. And this "temporary" memory is something different from the well known heap and stack memory areas??

C++: Is any compile time memory allocated for this type of function return value?
The memory for the result is allocated on the stack. However, the memory may be located purely in the caller of "c::func" and not in "c::func", itself, if your compiler uses return-value-optimization (RVO).
Reply:'return c();' will call the same function again, it is recursive, it will never complete recursing, eventually it will cause a stack overflow. No memory would be allocated at compile time. Stack memory will be allocated for each function call once c() is called.


F(x) = 4x+c and f^-1(x) = mx - 5/4. Find the values of m and c.?

f(x) = 4x+c


Let y = 4x+c


y-c = 4x


y-c/4 = x





Therefore, f^-1(x) = x-c/4





What do I do next?

F(x) = 4x+c and f^-1(x) = mx - 5/4. Find the values of m and c.?
You have done it wrong


f(x) = 4x+c


Let y = 4x+c


y-c = 4x


y-c/4 = x


--------------------------------------...


how is this possible


y-c = 4x


then


x = y/4 - c/4


so,


f^-1(x) = x/4 - c/4-------------%26gt;%26gt;(1)





given that





f(x) = 4x+c


and


f^-1(x) = mx - 5/4---------%26gt;%26gt;(2)





Comparing (1) and (2) we get





m = 1/4 and c = 5


================





Hope this will help u...................
Reply:You know that both


:A: f^-1(x)=x-c/4 [what you derived from f(x)=4x+c]


and


:B: f^-1(x)=mx-5/4 [given]


are true. The only possible solution here is (m,c)=(1,5); substituting m=1 would give


:A: f^-1(x)=x-c/4


:B: f^-1(x)=x-5/4


and substituting c=5 would give


:A: f^-1(x)=x-5/4


:B: f^-1(x)=x-5/4


C# I need still help! 10 points for the best answer. C# C# C#?

Hello everybody.


I need a C# code for print an image, imported with a openFileDialog Component.


Can you help me?


Thanks

C# I need still help! 10 points for the best answer. C# C# C#?
First, you will need an OpenFileDialog, a PictureBox, and a method of starting the loading process (your choice, most likely a button's Click event).





Assuming the dialog is called openFileDialog and the picture box is called pictureBox, you can call this function (or copy and paste the code inside of it into the event handler.





void LoadImageFromDialog()


{


openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";


if (openFileDialog.ShowDialog() == DialogResult.OK)


{


pictureBox.Load(openFileDialog.FileNam...


}


}





Example Form:


Copy and paste this into a new file called ImageForm.cs and just create an instance of it and show it. Clicking on the form will open the file dialog and load the picture after selecting one.





using System;


using System.Windows.Forms;





public class ImageForm : Form


{


OpenFileDialog openFileDialog = new OpenFileDialog();


PictureBox pictureBox = new PictureBox();





public ImageForm()


{





this.pictureBox.Location = new System.Drawing.Point(0, 0);


this.pictureBox.Size = new System.Drawing.Size(300, 300);


this.pictureBox.Dock = DockStyle.Fill;


this.pictureBox.Click += new EventHandler( pictureBox_Click);





this.ClientSize = new System.Drawing.Size(300, 300);


this.Controls.Add(pictureBox);


this.Text = "Image Form";


}





void pictureBox_Click(object sender, EventArgs e)


{


openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";


if (openFileDialog.ShowDialog() == DialogResult.OK)


{


pictureBox.Load( openFileDialog.FileName);


}


}


}
Reply:Look in MSDN for Graphics.DrawImage, or just look at the Graphics class. You


draw an image in really the same way as you print anything.
Reply:Since we're talking windows forms, I think you want to create a form and add an OpenFileDialog to it, and add a PictureBox Control to it.





Add a button call it Load... in the OnClick you'll want to open the Dialog. When the user clicks Ok after selecting a picture. Set the image property of the PictureBox to the file in the Open dialog control.





Instant Gratification.


C progamming /clr?

I want to compile a program that I wrote in Microsoft Visual C++. It is a C program that looks like this:


#include %26lt;stdio.h%26gt;





int main()


{


if ( !remove("useless.txt"){


printf( "Remove Sucsess" );


}


return 0;


}





I just wanted to test the "remove" function that I recently learned.





When I try to complie it an error comes up:





1%26gt;cl : Command line error D8045 : cannot compile C file '.\remove.c' with the /clr option





Now I have no idea what /clr is can anyone help?


Is it jsut the compiler or my program?





Thanks,

C progamming /clr?
Your program is missing library...


set up libraries path...

plum

Friday, May 21, 2010

C-section question.?

okay. i wanted to know some things about the c-section. i'm in the south carolina area also. i was wondering can you get a planned c-section, or will the doctors make you have it natural if you can? .. %26amp;%26amp; if you can get a c-section do you have to get a epidural. or is there other things you can ask for and they give it to you? can you get a anestishia ( however you spell it ), and be put to sleep? .. what are some other things about the c-section, like your experience with it.

C-section question.?
It depends on your doctor but my experience they will only do a c-section if need be. I had one with my daughter b/c she was breech and they did a version to try and flip her by pushing on my belly to try to externally flip her, it didn't work, so when I had my son 2 years later I did a vbac, vaginal birth after cesarian, it was the best thing I could have done. It was such a fast recovery and with my c-section I had a spinal block, a one time shot where they do the epidural and it totally numbs you. If you have an option go for vaginal birth. So much better. Good luck
Reply:No physician should allow a woman to have c-section unless it is medically warranted. It is major surgery, with risks to you and the baby. Generally women have epis or spinal blocks for a c-section. On very, very rare occassions when there is a true emergency, the mother might be "put to sleep" for the duration of the surgery.





I have had vaginal births and a c-section and there is NO way I would elect to have c-section. ONLY is there were some emergency... it's way to risky otherwise.
Reply:Most of the time they will not do a c-section because you ask for it. Yes, most c-sections are done with epidural's. General anesthesia is bad for baby and mom so they only do it for emergency c-section's most of time. It is sometimes worse recovering from c-section than it is a normal vaginal birth. I would say you need to talk to your doctor and be honest with your fears of vaginal delivery. Good luck.. Hope this helps a little..


P.S. in vaginal delivery, the contractions and baby passing through birth canal gets the fluid out of babies lungs... in c-cection, this does not happen so higher risk for aspiration of fluids occurs