public static void main(String[] args) {
int[][] array ={
{ 5, 5, 6, 67, 9, 9, 9, 9, 4, 4, 4, 4, 5, 0, 9, 9, 4},
{ 5, 66, 8, 6, 7, 43, 7, 43, 5, 4, 5, 4, 89, 4, 7, 43, 5},
{ 4, 6, 5, 3, 8, 4, 8, 4, 6, 9, 6, 9, 2, 5, 8, 4, 6},
{ 6, 4, 4, 3, 0, 6, 0, 6, 7, 5, 7, 5, 76, 6, 0, 6, 7},
{ 9, 6, 8, 6, 6, 3, 6, 3, 7, 34, 7, 34, 89, 3, 6, 3, 7},
{ 65, 4, 77, 4, 2, 8, 2, 8, 5, 5, 5, 5, 4, 6, 2, 8, 5},
{ 2, 3, 4, 8, 43, 43, 43, 43, 4, 3, 4, 3, 6, 3, 43, 43, 4},
{ 9, 4, 0, 38, 8, 3, 8, 3, 8, 6, 8, 6, 3, 7, 8, 3, 8},
{ 5, 3, 5, 6, 0, 3, 0, 3, 8, 3, 8, 3, 63, 2, 0, 3, 8},
{ 3, 4, 4, 64, 23, 4, 23, 4, 78, 5, 78, 5, 4, 6, 23, 4, 78},
{ 7, 89, 34, 5, 0, 3, 0, 3, 7, 78, 7, 78, 4, 3, 0, 3, 7},
{ 6, 3, 6, 6, 4, 3, 4, 3, 6, 42, 6, 42, 8, 4, 4, 3, 6},
{ 4, 4, 0, 53, 4, 3, 4, 3, 1, 12, 1, 12, 4, 5, 4, 3, 1},
{ 5, 9, 2, 6, 8, 5, 8, 5, 9, 3, 9, 3, 65, 4, 8, 5, 9},
{ 4, 7, 8, 3, 4, 4, 4, 4, 7, 4, 3, 4, 3, 2, 11, 4, 89},
{ 4, 7, 8, 8, 4, 9, 4, 4, 8, 9, 3, 9, 9, 2, 4, 2, 3},
{ 4, 7, 8, 6, 2, 4, 4, 4, 0, 4, 3, 4, 2, 9, 4, 4, 3},
};
int count=1;
int z=0;
int sum=0;
for (int i=0; i<array.length;i++){
int middle= array.length/2;
for (int j=(middle-count);j<middle+count;j++)
sum += array[i][j];
count=count+z;
count++;
if (count>=8){
z++;
count=count-z;
}
}
System.out.println("Sum=" + sum );
}
}```
i think the code is fine but whenever i run it, the answer is wrong the answer should be 1662
#looking for values from arrays
61 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @deft gyro! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
but the code gives
the error is here: for (int j=(middle-count);j<middle+count;j++)
middle is int middle= array.length/2; and == 8;
so for first iteration of inner loop you have for j=(8-1); j < (8+1); j++). So for first row inner loop will be working from 7 (inclusive) to 9 (exclusive), so it sum up indexes 7 and 8. But it's supposed to involve only one element
so i should put my count = 0?
and change condition of inner loop
what do i change it to though
if i put the count=0 doesnt that mean that itll be 8-0/8+0?
yes, so you need to fix it. That's why I said you also need to change the condition to make it work for 8 too
I think you should try outputting the pattern first. Have you ever come across such a pattern but using "*". Then when you get that it will be easy
im sorry im still quite lost
you need it to work for 8 in first row
if you leave the inner loop as it is for (int j=(middle-count);j<middle+count;j++) and having count = 0; you will be having
for (int j=8;j<8;j++). So loop wont work for first row (for j==8) but you need it to work for j ==8
so you need to change the condition of that loop
you need not <, but <=
now, 8 will be inclusive
I've implemented it already and getting 1662 as answer of sum. So I will help you if you put enough effort too
okay wait
i just want to confirm
we need 8 and not 9 because the array starts with 0 right?
on 0 row we need sum up only one element with index 8
(according to the picture)
so the issue is in row 1 and not row 0?
I said in first row, so row with index 0
i see
so by changing my count to 0 and by adding <= it will be inclusive of column 8 only for first row?
exactly
because you have mess with counter and z (dunno why you put one more variable)
let me explain
you need to do count++ to the middle row
as you reached the middle row, you need do count--
mhmm
now convert my words into the java code
nope
if (i>=8)?
count--
else
count++
yes, but write the rest of code
ahh
if (i >= middle) count--;
else count++;
ahhh right since i already have middle initialized
now it should return 1662
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
i really appreciate it
np 😉
you should click Im done here. Close this post
ouh
