0

i cant seem to figure out the logic, here's my code

class stringrays {
public static void main (String[] args) {

    int[] numrays = {23, 6, 47, 35, 2, 14};
    int i;
    int z;
    int y;

    for (i=1; i < numrays.length; i++) {
        z = numrays[0] + numrays[i];
        System.out.println(z);
    }
}

the above results shows 29 70 58 25 37

which means that array 0 adds array 1, then array 0 adds array array 2 and so on.

what i want, is to add the first array 0 onto the next array and so on.. using a loop condition.

then get the average of the sum.

5
  • 2
    1) What is your question? 2) Is this homework? If so, it is advantageous to tag it as such. Commented Sep 6, 2012 at 8:01
  • 1
    "what i want, is to add the first array 0 onto the next array and so on" is unclear. could you please elaborate Commented Sep 6, 2012 at 8:02
  • Do you mean int z = numrays[0]; /* in loop*/ z += numrays[i]; ? Commented Sep 6, 2012 at 8:02
  • no this isnt homework, its just an exercise i found on the web. Commented Sep 6, 2012 at 8:03
  • "first" array and "next" array ? there is only one array... Commented Sep 6, 2012 at 8:08

7 Answers 7

2

Try this,

int[] numrays = {23, 6, 47, 35, 2, 14};
int z = 0;

for (int i=0; i < numrays.length; i++) {
    z = z + numrays[i];
    System.out.println(z);
}
    System.out.println("Average : "+(z/numrays.length) );
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you mean 23, 6 then 6 + 47 and so on you need to do:

for (i=0; i < numrays.length - 1; i++) 
{
   z = numrays[i] + numrays[i + 1];
   System.out.println(z);
}

1 Comment

and for finding average. z/numrays.length();
1

Or the LambdaJ way :

int sum = sum(asList(1, 2, 3, 4, 5));

Comments

0

Remove numrays[0] and replace it with z

     int z =0;
for (i = 0; i < numrays.length; i++) {
        z = z + numrays[i];
        System.out.println("Sum:"+z);

    }
    System.out.println("Average:"+z/numrays.length);

1 Comment

im getting variable z might not have been initialized z = z + numrays[i];
0

it is unclear what you need...

to add a value to the next position:

int[] numrays = {23, 6, 47, 35, 2, 14};

for(int i = 0; i < numrays.length - 1; i++) {
    numrays[i] += numrays[i + 1];
}
System.out.println(Arrays.toString(numrays));

to get the mean value:

int[] numrays = {23, 6, 47, 35, 2, 14};
double sum = 0;

for(int i = 0; i < numrays.length; i++) {
    sum += numrays[i];
}

double mean = sum / numrays.length;


System.out.println(mean);

Comments

0

You could use a "for each" loop, to sum the contents of the array and then find the average.

int sum = 0;

for(int each : numrays)
{
   sum = sum + each;
}

float average = each / numrays.length;

Comments

0

Check your logic. Now you are printing sum of first and n-th number in array; old z value is lost. For sum in loop, use like z = z + something

Comments

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.