#looking for values from arrays

61 messages · Page 1 of 1 (latest)

deft gyro
#

    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
glacial yarrowBOT
#

This post has been reserved for your question.

Hey @deft gyro! Please use /close or the Close Post button 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.

sand kiln
#

but the code gives

deft gyro
#

it gives 2189

#

this is what im trying to do btw

craggy tiger
# deft gyro this is what im trying to do btw

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

craggy tiger
deft gyro
#

if i put the count=0 doesnt that mean that itll be 8-0/8+0?

craggy tiger
#

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

errant bramble
#

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

deft gyro
#

im sorry im still quite lost

craggy tiger
#

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

deft gyro
#

okay wait

#

i just want to confirm

#

we need 8 and not 9 because the array starts with 0 right?

craggy tiger
#

(according to the picture)

deft gyro
#

so the issue is in row 1 and not row 0?

craggy tiger
#

I said in first row, so row with index 0

deft gyro
#

i see

#

so by changing my count to 0 and by adding <= it will be inclusive of column 8 only for first row?

craggy tiger
#

exactly

deft gyro
#

i seee

#

okay wait lemme change it

#

i got 2117

craggy tiger
#

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--

deft gyro
#

mhmm

craggy tiger
#

now convert my words into the java code

deft gyro
#

count++
if (count>=8){
count--
}

#

?

craggy tiger
#

nope

deft gyro
#

if (i>=8)?

craggy tiger
craggy tiger
deft gyro
#

if (i>=8)
count--;
else
count++;

#

?

craggy tiger
#

yeah

#

but use middle instead of 8. Don't hardcode values

deft gyro
#

ahh

craggy tiger
#
if (i >= middle) count--;
else count++;
deft gyro
#

ahhh right since i already have middle initialized

craggy tiger
#

now it should return 1662

deft gyro
#

yeap i got it now

#

thank you very much

glacial yarrowBOT
# deft gyro thank you very much

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.

deft gyro
#

i really appreciate it

craggy tiger
#

np 😉

craggy tiger
deft gyro
#

ouh