4

I have an array of 100 numbers and I only gave the array even values. How can I print out how many elements of the array I have to add to obtain a sum < than 1768 using a WHILE LOOP? The following is what I have so far and I am stuck... thanks in advance for the help

void setup() {

  int[] x = new int[100];
  int i=0;
  int sum=0;

  for(i=0; i<100; i++) {
    if (i%2==0) {
      x[i]=i;
      sum+=x[i];
    }
  }
}
4
  • how many elements -> You need to say more.. Minimum no or maximum no? Or it doesn't matter? Commented Oct 15, 2012 at 14:51
  • sorry! i need the max number of elements before the sum of even numbers which is what i assigned to the array is 1768! Commented Oct 15, 2012 at 14:53
  • If one of your requirements say to use a while loop, why does your current progress utilize a for loop? Commented Oct 15, 2012 at 15:03
  • do you need to sum up even elements until you get to 1768? Commented Oct 15, 2012 at 15:14

5 Answers 5

2
 void setup() {
     int i = 0;
     int sum = 0;
     int counter = 0;

     while (sum < 1768) {
         sum += i;
         i += 2;
         counter++;
     }

     System.out.println(counter);
 }

You start with the even index of 0. Then just skip odd numbers by using i += 2.
If the number of elements is limited with 100, add i < 200 to the while condition:

while (sum < 1768 && i < 200)

The array of 100 even numbers will contain numbers from 0 to 200.

The variable counter will contain the number of additions performed. Its value will be equal to i / 2, so you can remove that additional variable.

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

Comments

2

You can use this loop and element number would be i+1.

       for(int i=0,k=0; k<1768; i++,k+=x[i]) {
        System.out.println(x[i]+" - "+k);
       }

While loop -

   int i=0,k=0;
   while(k<1768; ) {
        System.out.println(x[i]+" - "+k);
        i++,k+=x[i];
       }

3 Comments

Any way to see this in a while loop?
@Quoi Based on the strange requirement and the odd restriction, it's probably homework.
it is a tutorial... Self improvement.
2

You are skipping indexes in your array.You are only filling every other 'slot' Also, it would probably be easier to use a while loop to check against your max value (1728)

        int[] x = new int[100];
        int i = 0;
        int sum = 0;
        int max = 1728;

        while (sum < max && i < 100)
        {
            x[i] = i*2;
            if ((x[i] + sum) < max)
            {
                sum += x[i];
            }
            i++;
        }

Comments

1
void setup() {

  int[] x = new int[100];
  int maxValue = 1768;
  int i;
  int sum=0;

  while(sum<maxValue) {
    if (i%2==0) {
      x[i]=i;
      sum+=x[i];
      i++;
    }
  }

   System.out.println(i+" Elements needed")

}

Comments

-1

following is code:

void setup() {

  int[] x = new int[100];
  int i=0;
  int sum=0;

  for(i=0; i<100; i++) {
    if (i%2==0) {
      sum += i;
            if(sum<1768){

                 num +=1; 
            }
    }
  }
}

4 Comments

We need a Java answer for this question.
that is similar to what I initially had, but it must be completed using a while loop!
"Must be completed using a while loop" > that's news to us. Is that a requirement?
yes it is, sorry i forgot to mention that in the question! sorry!

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.