0

I want to print Ready if CL (count lucky) is greater than CUL(count unlucky). My criteria is N is some number and array A is upto N. and the contents of array are checked whether individual content is even/odd. If even CL++, or else CUL++

static void Main(string[] args) 
{
    int N = int.Parse(Console.ReadLine());
    int[] A = new int[100];
    int cL=0; int CUL = 0;
    int j;

    for (j = 0; j < N; j++)
    {
        A[j] = int.Parse(Console.ReadLine());
        if (A[j] %2!=0))
        {

            CUL = CUL++;
        }
        else
        {
            cL = cL++;
        }
    }
    if (cL>CUL)
    {
        Console.WriteLine("Ready for battle");
    }
    else
    {
        Console.WriteLine("Not ready");
    }
}   
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • also using the `Debugger` you could have spotted the issue right away.. also be consistent with your variables naming convention make them more meaningful this way you yourself won't get so confused – MethodMan Mar 22 '16 at 19:01

2 Answers2

1

Ignoring minor errors, you should have a Console.ReadLine(); at the end, you are probably running it through visual studio and you can't see the output as the program closes too quickly on completion.

Later you can fix your code like:

  • By using N for array size as well, imagine if the user enter anything over 100, you will end up with an exception of "Index out of range".
  • No need to assign the result of increment operator, It will update the variable. Postfix operator increments the value but it is visible on next usage, in your case you will always end up with 0 since you are assigning the result back to it.

The code should be:

int N = int.Parse(Console.ReadLine());
int[] A = new int[N]; //Use the input limit here on array size
int cL = 0; int CUL = 0;
int j;
for (j = 0; j < N; j++)
{
    A[j] = int.Parse(Console.ReadLine());
    if (A[j] % 2 != 0)
    {

      CUL++;//no need to reassing result
    }
    else
    {
        cL++;
    }

}
if (cL > CUL)
{
    Console.WriteLine("Ready for battle");
}
else
{
    Console.WriteLine("Not ready");
}
Console.Readline(); // Just to halt the program to see the output. 
Habib
  • 219,104
  • 29
  • 407
  • 436
  • you mean I'm assigning count values to zero every time? thanks I got the output – Bhavani Chandra Mar 22 '16 at 19:07
  • @BhavaniChandra, yes, When you do `CUL++`, that increments the value, but the increment value is available on next usage, so when you start `CUL = 0` and then you do `CUL = CUL++;` that cause `CUL` to increment by `1` but since that increase value will be available on next usage you get `0` assigned back to `CUL`, overwriting the incremented value. Hence it always remains at `0`. For more info see http://stackoverflow.com/questions/7812178/how-do-prefix-x-and-postfix-x-operations-work – Habib Mar 22 '16 at 19:10
  • @ Habib Console.Readkey() should be at the end – Bhavani Chandra Mar 22 '16 at 19:10
0

The problem is in

CUL = CUL++;

And in

cL = cL++;

You assign CUL to CUL (and cL) when it is 0 and increase the value of the right CUL to 1 only after the assignment, so in the next itreation CUL and cL will still be 0. Change it to

if (A[j] %2!=0))
{
    CUL++;
}
else
{
    cL++;
}

You also have problem in the A array declaration. You get the size from the user but initialize it with size 100. I should be

int[] A = new int[N];
Guy
  • 46,488
  • 10
  • 44
  • 88