|
I would like to write a program of Fibonacci sequence.
F0=0;
F1=1;
F2=F0+F1;
F3=F1+F2;
.
.
Fn=Fn-1 + Fn-2
Like this.
And for sure, I know that I have to use recursive algorithm to do this.
But this is first time to make this kind of program.
So could someone help me out?
|
|
|
|
|
|
It's a homework assignment - that's why we're getting them now.
|
|
|
|
|
harold aptroot wrote: Why?
It's obvious: infinite recursion is more fun than anything with a wimpy limit on the number of iterations!
Factorials are supposed to end, which spoils the fun of blowing the top off the stack.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Blow the top off the stacked what?
|
|
|
|
|
OriginalGriff wrote: Factorials are supposed to end
Suffering from BDS again? (Bacon Deprivation Syndrom)
What am I missing here?
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Factorials are the "normal" example given to students to teach them recursion:
!n == n * !(n - 1) where n > 1 So factorials are naturally limiting - whatever finite number you start with, there is a termination to the sequence. (And never mind that recursion is a poor way to implement it anyway)
Fibonacci numbers on the other hand are different:
Fn = F(n-1) + F(n-2) Where F0 = 0, and F1 = 1 So instead of decreasing towards a limit, Fibonacci numbers increase to towards infinity, with no limiting condition at all to terminate them. That makes them a poor candidate for recursion, because unless an artificial outside limit such as "n < 1000" is applied as well, the recursion continues indefinitely, and the stack is compromised.
Another very poor implementation!
Thinking on this before I posted, I realised that I was wrong: the only practical way to write Fibonacci sequences as recursive functions means providing the limit first, and then calculating all the values before it, in much the same way as a factorial would be - I was thinking of it in terms of calculating the sequence from the start, without providing a limit: the sensible way since it doesn't duplicate work!
As a result, I was wrong, the stack won't self destruct.
It's still a silly recursion example though!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
OriginalGriff wrote: Thinking on this before I posted, I realised that I was wrong
Are you properly baconized? I hope there isn't another shortage on the horizon.
We all know that'll wreak havoc on your otherwise exceptional abilities.
(Which would indeed be expected from every bacon afficinado under such dire circumstances.)
OriginalGriff wrote: It's still a silly recursion example though!
Agreed!
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Sottyoru wrote: So could someone help me out?
Jack Daniels, Captain Morgan, Jose Cuervo... Choose your poison.
|
|
|
|
|
The only reason to use recursion is if it's a homework assignment; and if it's a homework assignment, you must do it yourself.
You learn more by getting it wrong and struggling and trying many ideas.
|
|
|
|
|
Picking up a good book on C might be good for a start.
|
|
|
|
|
A recursive function is simply one that calls itself. In the wrong hands, a recursive function can blow your application up. Try the following function to see what I mean:
public void MultiplyRecursively(int value, int multiplier)
{
int output = value * multiplier;
Console.WriteLine("Multiplied by ={0}", output);
MultiplyRecursively(output, multiplier);
} As this function has no terminating condition, all sorts of nasties can occur. I suggest that you run the code to see what happens.
|
|
|
|
|
|
public static Int64 Fibonacci(Int64 value)
{
Int64 oldnum = 1;
Int64 currnum = 1;
for (var i = 0; i < value; i++)
{
var next = currnum + oldnum;
oldnum = currnum;
currnum = next;
}
return currnum;
}
|
|
|
|
|
Hi, I have encounted a NullReferenceException problem while using SendKeys.
'TestWinAPI.vshost.exe' (Managed): Loaded 'C:\TestWinAPI\bin\Debug\TestWinAPI.exe', Symbols loaded.
'TestWinAPI.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
When I enable the exception capture, it shows the exception is being raised in SendKeys
private void btnAllinOne_Click(object sender, EventArgs e)
{
int hwnd = 0;
IntPtr hwndChild = IntPtr.Zero;
if (tbxProc.Text.Length == 0)
{
MessageBox.Show("empty process string"); return;
}
hwnd = FindWindow(null, tbxProc.Text);
if (hwnd == 0)
{
if (MessageBox.Show("Couldn't find the " + tbxProc.Text + " application. Do you want to start it?", "TestWinAPI", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
}
}
else
{
SetForegroundWindow(hwnd);
SendKeys.Send(tbxEditText.Text + "{ENTER}");
}
}
Anyone knows why is that?
|
|
|
|
|
tikcireviva wrote: Anyone knows why is that?
Either tbxEditText is null or it's .Text property is null. Assign the contents of the textbox to a string. On the next line, create a statement to concatenate the constant "{ENTER}".
Those three separate things "can" be done in a single line of code, but don't "have to". Dividing them up makes debugging a lot easier.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I think this is an exception which will be caught and handled internally within the SendKeys class. Debugging with "break when an exception is thrown" enabled is letting you see this exception which you don't really need to know about.
If you set the Visual Studio debugging option "Just my code" it should stop this nonsense!
Alan.
|
|
|
|
|
Hi !
Pardon for my bad english.
Ive been working this code for weeks,
foreach (Control Ctrl in this.Controls)
{
switch (Ctrl.GetType().ToString())
{
case "clarinrt.form.other.sdate":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("Text", formdataset, Ctrl.Tag.ToString()));
break;
case "clarinrt.form.other.slookup":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("Text", formdataset, Ctrl.Tag.ToString()));
break;
case "clarinrt.form.other.scheck":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("Checked", formdataset, Ctrl.Tag.ToString()));
break;
case "clarinrt.form.other.snumeric":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("Text", formdataset, Ctrl.Tag.ToString()));
break;
case "clarinrt.form.other.stext":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("Text", formdataset, Ctrl.Tag.ToString()));
break;
case "clarinrt.form.other.sdropdown":
if (Ctrl.Tag.ToString() != "") Ctrl.DataBindings.Add(new Binding("SelectedIndex", formdataset, Ctrl.Tag.ToString()));
break;
default:
break;
}
}
For each control, TAG property will be filled such as "account.detail" refering to a column in table name, while formdataset is Dataset for this form.
This code in Load method on base form. the problem with sdropdown and scheck which inherited from combobox and Checkbox. If I comment out, those two part, Databind work fine.
Can someone told me, what I should do ? sdropdown will be databind with Integer column while scheck will be databind with Boolean column
Thank you for your help...
|
|
|
|
|
You're abusing the "Tag" property. Databinding works best for properties, not for function-calls like "ToString".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi There !
I've using some properties set in my class to replace Tag
public string cfield { get; set; }
but it is doesnt work for checkbox and combobox.
Thank you for your help.
|
|
|
|
|
A dataset doesn't implement INotifyPropertyChanged ; you might need to create a wrapper for it.
public partial class Form1 : Form, INotifyPropertyChanged
{
bool _test;
public bool Test
{
get { return _test; }
set
{
_test = value;
PropertyChangedEventHandler handler = this.PropertyChanged;
if (null != handler)
handler(this, new PropertyChangedEventArgs("Test"));
}
}
public Form1()
{
Test = true;
InitializeComponent();
checkBox1.DataBindings.Add(new Binding("Checked", this, "Test"));
}
private void button1_Click(object sender, EventArgs e)
{
Test = !Test;
}
public event PropertyChangedEventHandler PropertyChanged;
}
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi!
I need to remove Application launch and "Pin this application to taskbar" from the taskbar context menu for an application. Reason is that the application cannot start standalone, it must be feed with information from another application.
Does anyone know how?
Andreas Johansson
Senior software developer at Tieto Sweden
|
|
|
|
|
I'd recommend simply adding a messagebox if started without parameters; that way one is also warned when launching from another (unexpected) location.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, i will do that as well.
But it would be nice to have both solutions
Andreas Johansson
Senior software developer at Tieto Sweden
|
|
|
|
|
Make sure the executable or shortcut name includes one of the restricted words:
- Documentation
- Help
- Install
- More Info
- Read me
- Read First
- Readme
- Remove
- Setup
- Support
- What's New
If it contains one of those words, you won't be able to pin it to the taskbar or start menu.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|