Initialize array in one line / in a single statement
-
Hi, I want to initialize a string array with "*". I want to know if I can avoid using a for loop to do the same. In the below example I have used a for loop. Can the same done in one line??? The following code will help me generate fibonacci series
public static int Fibonacci(int n)
{
int previous = -1;
int result = 1;
for (int i = 0; i <= n; ++i)
{
int sum = result + previous;
previous = result;
result = sum;
}// // declare an array of size 'result' // use a for loop and initialize each entry of array with a "\*" //
}
there isn't a straightforward formula that yields Fibonacci numbers without iterations; the closest you can get AFAICT is by using Binet's formula (see here[^]) but for large numbers that will be an approximation as it relies on floating-point arithmetic. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
Close but no cigar. That results in a char[], not a string[].
string[] sa=new string('*', n).Replace("*", ".*").Split(".".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
is one way of getting it done.
string[] sa=new string('*', n).Replace("*", ".*").Substring(1).Split('.');
is an alternative. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
Happy :p
string[] arr = new string[0];
for(int i = 0; i < 10; i++) {
string[] temp = new string[arr.Length+1];
Array.Copy(arr, temp, arr.Length);
temp[arr.Length] = "*";
arr = temp;
}Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
new String('*', 10).ToCharArray();
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
That was awesome Stephen. Thanks I never knew that an array can be instantiated with a new kewword. I have incorporated your suggestion in my application. I am now using "char[] str7 = new String('*', result).ToCharArray()" This should handle one part of my application. I have one more question. Is it possible to convert this array of characters to a string? something like reverse of what you showed me. regards, Netquestions
-
Hi, I want to initialize a string array with "*". I want to know if I can avoid using a for loop to do the same. In the below example I have used a for loop. Can the same done in one line??? The following code will help me generate fibonacci series
public static int Fibonacci(int n)
{
int previous = -1;
int result = 1;
for (int i = 0; i <= n; ++i)
{
int sum = result + previous;
previous = result;
result = sum;
}// // declare an array of size 'result' // use a for loop and initialize each entry of array with a "\*" //
}
string[] sa=new string('*', (int)Math.Round((Math.Pow(0.5+Math.Sqrt(1.25), n)-Math.Pow(0.5-Math.Sqrt(1.25), n))/Math.Sqrt(5))).Replace("*", ".*").Substring(1).Split('.');
works quite well. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
That was awesome Stephen. Thanks I never knew that an array can be instantiated with a new kewword. I have incorporated your suggestion in my application. I am now using "char[] str7 = new String('*', result).ToCharArray()" This should handle one part of my application. I have one more question. Is it possible to convert this array of characters to a string? something like reverse of what you showed me. regards, Netquestions
His name's not Stephen. I can understand the confusion, but the Stephen referred to in his signature relates to the author of the quote.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
His name's not Stephen. I can understand the confusion, but the Stephen referred to in his signature relates to the author of the quote.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
I prefer Denny Crane anyway. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
Hi, I want to initialize a string array with "*". I want to know if I can avoid using a for loop to do the same. In the below example I have used a for loop. Can the same done in one line??? The following code will help me generate fibonacci series
public static int Fibonacci(int n)
{
int previous = -1;
int result = 1;
for (int i = 0; i <= n; ++i)
{
int sum = result + previous;
previous = result;
result = sum;
}// // declare an array of size 'result' // use a for loop and initialize each entry of array with a "\*" //
}
You know, your teacher is likely just wanting a recursive method[^].
-
You know, your teacher is likely just wanting a recursive method[^].
I suggest you try Fibonacci(100) in a recursive way; you can walk around the world while it computes. Unless you go for a more advanced equation, one that calculates Fib(2x) from Fib(x). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
I suggest you try Fibonacci(100) in a recursive way; you can walk around the world while it computes. Unless you go for a more advanced equation, one that calculates Fib(2x) from Fib(x). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
Oh, I'm not suggesting that it is a better solution what so ever. I'm just stating it sounds like a homework assignment for creating a recursive Fibonacci implementation. ;P
-
I suggest you try Fibonacci(100) in a recursive way; you can walk around the world while it computes. Unless you go for a more advanced equation, one that calculates Fib(2x) from Fib(x). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
I am not an algorithms expert so mine may be wrong and I did cheat for base case but my recursive Fib algorithm is instant. I won't post because it may be the OP's homework but Fib is not hard and does not require any deep recursion. My algorithm needs 98 levels to calculate the Fib numbers from 2 to 100. (Again, I cheated on the base)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
I am not an algorithms expert so mine may be wrong and I did cheat for base case but my recursive Fib algorithm is instant. I won't post because it may be the OP's homework but Fib is not hard and does not require any deep recursion. My algorithm needs 98 levels to calculate the Fib numbers from 2 to 100. (Again, I cheated on the base)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
yes, you can easily avoid the exponential behavior, however I have seen quite a few straightforward recursive implementations that don't. They are extremely good examples against the use of recursion for anything that is not recursive by its very nature (such as a hierarchical file system). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
Hi, I want to initialize a string array with "*". I want to know if I can avoid using a for loop to do the same. In the below example I have used a for loop. Can the same done in one line??? The following code will help me generate fibonacci series
public static int Fibonacci(int n)
{
int previous = -1;
int result = 1;
for (int i = 0; i <= n; ++i)
{
int sum = result + previous;
previous = result;
result = sum;
}// // declare an array of size 'result' // use a for loop and initialize each entry of array with a "\*" //
}
NetQuestions wrote:
I want to initialize a string array with "*".
string[] a = new string[] { "*" } ;
-
That was awesome Stephen. Thanks I never knew that an array can be instantiated with a new kewword. I have incorporated your suggestion in my application. I am now using "char[] str7 = new String('*', result).ToCharArray()" This should handle one part of my application. I have one more question. Is it possible to convert this array of characters to a string? something like reverse of what you showed me. regards, Netquestions
-
You know, your teacher is likely just wanting a recursive method[^].
the number of times the method keeps getting called increases exponentially
-
NetQuestions wrote:
I want to initialize a string array with "*".
string[] a = new string[] { "*" } ;
I actually used the following statement
string str = new string('*', 10)
-
Oh, I'm not suggesting that it is a better solution what so ever. I'm just stating it sounds like a homework assignment for creating a recursive Fibonacci implementation. ;P
Hi, recursion is not always the way.
*
*
*
**
***
*****
********If I were to generate and display the following, you will observe that when n= 50, program hangs. the number of times recursive method gets called grows exponentially. In such a case a non recursive method is required. Regards, NetQuestions
-
I actually used the following statement
string str = new string('*', 10)
That doesn't meet the stated criteria.
-
Hi, recursion is not always the way.
*
*
*
**
***
*****
********If I were to generate and display the following, you will observe that when n= 50, program hangs. the number of times recursive method gets called grows exponentially. In such a case a non recursive method is required. Regards, NetQuestions
Indeed, I'm well aware of such. I was only basing my suggestion on the fact it seemed like a homework assignment to create a recursive method. :)