0

Okay, lets assume we have a variable with an array like this

int[] digit;
int x = 5; 
for(int i=0;i < = 5; i++)
{ digit[i] = 0;}

All value array on var Digit have 0. So what i want to do its i want to increment the value on that digit from right using Button Control that adds increment (that means digit[4] to digit[3] and so on) and when the value hits certain number example 5 in digit[4], it would come back to value 0 and the next var digit incremented (digit[3]). And the incremented start again and so on.

I already try using if and switch to make this happen like this

private btnClick_Click(Object Sender, Event Args)
{
     digit[4] +=1;
     if(digit[4] > 5) { digit[3] += 1;}
     if(digit[3] > 5) { digit[2] += 1;}
     //and so on
     switch(digit[4])
     {
         case 5: digit[4]=0;
     }
     //and so on
}

But its only for logic If We Know The Array Number Location. Say if i retrieve that number for somewhere like 15 digit. If we set array number so little on that command Button, it cannot fill the array right?

Imma already confused thinking this, any suggestion, help, discussion ill appreciate it. Thanks.

9
  • What do you mean by "if we know the array number location"? Why can't you just keep that as a private field in the class? Commented Feb 23, 2018 at 9:29
  • 1
    This looks like a homework trying teach you some programming logic... Why should we obstruct your learning giving an answer? Commented Feb 23, 2018 at 9:32
  • 3
    Your question is really difficult to undestand. Apart from that, i <= 5 will make the for loop fail with an IndexOutOfRangeException. It would be helpful to include executable code and a clear description of your problem. Commented Feb 23, 2018 at 9:32
  • @RMH yes thats the logic. Why i need the backwards array bcus my programming project retrieve the string and converted to int. String like number 15152 for example. And after the operation it would become 15283. So that means 2 its digit[4] Commented Feb 23, 2018 at 9:36
  • 4
    Also note that new int[5] creates an array with every element set to 0 to start with, so your initial loop is pointless. Commented Feb 23, 2018 at 9:40

1 Answer 1

2

If you just want to increment by one, and not any substraction or increment by let's say 5, I'd use a simple solution like this:

private void btnClick_Click(Object Sender, Event Args) {
    int maxValue = 5;
    int carry = 1; // This is our increment

    // iterate through your digits back to front
    for(int i = digit.Length - 1; i >= 0; i--) {
        digit[i] += carry;  // increase the value by the carry. This will at least increment the last digit by one
        carry = 0;

        // if we reach our max value, we set the carry to one to add it to the next digit, and reset our current digit to 0.
        // If you wanted to increase by more than 1 a time, we would have to add some more calculations here as it would
        // be incorrect to just reset digit[i] to 0.
        if(digit[i] >= maxValue) {
            carry = 1; // the next digit will now be told to increase by one - and so forth
            digit[i] = 0;
        } else {
            break; // This will break out of the for - loop and stop processing digits as everything just fit nicely and we don't have to update more previous digits
        }
    }
}

Not that once you reach 44444 and increment, you will end up with 00000.

Sign up to request clarification or add additional context in comments.

3 Comments

Why keep iterating when carry is 0? Just break out of the loop at that point.
Ah, sure! @JonSkeet
i was also thinking like that too, using subtraction on loop. i will trying that. Thanks for your idea @habegage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.