Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am writing this function, however it gives me: The name 'binarySearchRecursive' does not exist in the current context. I dont understand why i am getting this message, the call is done in the scope of this method itself? Or do i misunderstand a fundamental part of c#?
public static int binarySearchRecusive<T>(T[] a, int low, int high, T v) where T : IComparable
{
if (low < high)
{
var middle = (low + high) / 2;
if (a[middle].CompareTo(v) == 0)
return middle;
if (a[middle].CompareTo(v) < 0)
return binarySearchRecursive(a, low, middle - 1, v);
else
return binarySearchRecursive(a, middle+1, high - 1, v);
}
return -1;
}
Your function goes by this name:
binarySearchRecusive
If you change it to:
binarySearchRecursive
it will work.
Typpo in binarySearchRecusive, instead, try binarySearchRecursive.
If it still don't work then try Program.binarySearchRecursive()
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?
Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all
The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.
Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I keep getting a CS1513 error on the following code in visual studios writing in C#
{
class NotChineseZodiac
{
static void Main(string[] args)
{
String YearBorn;
int YearBornInt;
Console.WriteLine("What was the year of your birth?");
YearBorn = Console.ReadLine();
YearBornInt = Convert.ToInt32(YearBorn);
Console.WriteLine("You are" + (DateTime.Now.Year - YearBornInt));
if ((DateTime.Now.Year - YearBornInt) < 18);
Console.WriteLine("You Shall Not PASS");
else
Console.WriteLine("Please Proceed");
Console.ReadLine(); // last enter key
Does anyone see my error??
you have terminated this line with a semicolon:
if ((DateTime.Now.Year - YearBornInt) < 18);
correctly it would be:
if ((DateTime.Now.Year - YearBornInt) < 18)
Console.WriteLine("You Shall Not PASS");
else
Console.WriteLine("Please Proceed");
or for better readability
if ((DateTime.Now.Year - YearBornInt) < 18)
{
Console.WriteLine("You Shall Not PASS");
}
else
{
Console.WriteLine("Please Proceed");
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to do a simple math equation, however, when I try to use "*" it throws the error
error CS0019: Operator *' cannot be applied to operands of typeSystem.Collections.Generic.List' and `double'
Code example:
public static List<string> items = new List<string> ();
public static List<double> itemsprice = new List<double>();
public static List<double> qu = new List<double>();
int i = 0;
double price = 0;
while(i != items.Count){
price = itemsprice[1];
ticksales = qu * pricepoint / 2; // Error on this line
income = income + ticksales * price;
}
That is occuring because qu is a List of type double and you are trying to multiply it. It seems like what you want to do is qu[i] * pricepoint/2;
Also you should increment i at the end of your loop or it will run forever.
qu is a List. What do you think list * pricepoint / 2 equals? (we don't know what pricepoint is BTW)
au is a List, you have to provide the index of the item you want to access. And also at the end you have to increment the index.
ticksales = qu[i] * (pricepoint / 2)
and i++; at the end.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}