Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to add 1 to all the odd numbers in the array, and add 1 to all the numbers in another array. I've tried my best to complete the code but it doesn't run, I've no idea why?

C#
public class MyProgram
{
    public void start()
    {
        int[] oddUp = {3, 8, 4, 9, 5, 5, 23, 14};
        int[] oneUp = {4, 8, 4, 10, 6, 6, 24, 24};
    }

    private void oddUp(int[] values)
    {
        for (int i = 0; i < oddUp.length; i++)
        {
            if (oddUp[i] % 3 == 0)
            {
                oddUp[i]++;
                System.out.println(oddUp[i]);
            }
            else
            {
                return;
            }
        }
    }

    private void oneUp(int[] values)
    {
        for (int i = 0; i < oneUp.length; i++)
        {
            oneUp[i]++;
            System.out.println(oneUp[i]);
        }
    }
}
Posted

Change the Code as below

C#
private void oddUpFunc(int[] values)
    {
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] % 3 == 0)
            {
                values[i]++;
                System.out.println(values[i]);
            }
        }
    }

    private void oneUpFunc(int[] values)
    {
        for (int i = 0; i < values.length; i++)
        {
            values[i]++;
            System.out.println(values[i]);
        }
    }


Call these functions by passing array in parameter.

C#
oddUpFunc(oddUp);
oneUpFunc(oneUp);
 
Share this answer
 
v2
Hi !...

Just I little bit of changes in your first function....! :-)

C#
private void oddUp(int[] values)
{
 for (int i = 0; i < values.length; i++)
  {
    //  You need to test Odd number's Not as previous division by 3
   if (values[i] % 2 != 0) 
    {
      values[i]++;
      System.out.println(values[i]);
      // use of print here shows only updated numbers not all numbers in array if want to print all then use it outside of if...
     }
    // No need to reuturn as you want to add 1 to all odd number's in array....
  }
}


Cheers....... :-)

Hemant Singh (My Asp.Net Blog[^])
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900