i have to print

1 3 2 4 5 i the input

output should be

1 3 2 4 5
4 5 6 9
9 11 15
20 26

i am getting a weird output
my code is

import java.io.*;
class nik1234
{
public static void main(String args[])throws IOException
{
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[][]=new int [20][20];
for( j=5;j>0;j--)
{
i=0;
arr[i][j]=Integer.parseInt(br.readLine());
}
for(j=5;j>0;j--)
{
i=0;
System.out.print("\t"+arr[i][j]);
}
for(j=5;j>0;j--)
{
i=0;
while(i<5)
{
int c=arr[i][j]+arr[i][j-1];
System.out.print(c);
i++;
}
System.out.println();
}
}
}

please help me modify program accordingly thank you in advance

Recommended Answers

All 19 Replies

akulkarni,
Java Jagged array : array of array.

int a=4;
         int m[][]=new int[a][];
         for(int i=0;i<a;i++)
           {
           m[i]=new int[a-i]; 
           for(int j=0;j<a-i;j++)
             {
              System.out.print(" " + m[i][j]);
              }
             System.out.println();
         }

i am getting output all zeros.i have to put the input as 1 3 2 4 5 and then next line sum of 1+3 3+2 4+5 and so on
do i have to input values myself in the above code given by adapost
if yes ..how? i didnt get the desired output as a tried the above code as it is

Hey, akulkarni.
There're some logic errors:
1.

for(j=5;j>0;j--)
{
i=0;
while(i<5)
{
int c=arr[i][j]+arr[i][j-1];
System.out.print(c);
i++;
}
System.out.println();
}

Just look what happening e.g. on the first step of your for-cycle:
1. j=5; i=0; you're taking elements with coordinates (row=0; column=5) and (row=0; column=4) .. and returning sum of them to the c variable. Print it and increase i.
j=5; i=1; you're taking elements with coordinates (row=1; column=5) and (row=1; column=4) ..
.. and so on..
What we have - you're moving through column .. but you supposed to go through rows.

Also there's one more trouble: when you count and output your sum - you don't memorize that number (for use in the next iteration of a cycle) .. so you have no work to do for the next step of your for-cycle.

can u please please modify and send the code to me

i tried this

import java.io.*;
class nik1235
{
public static void main(String args[])throws IOException
{
int i,j,m,k;
int a[][]=new int[20][20];
int a1[][]=new int[20][20];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(i=1;i<=5;i++)
{
j=0;
a[i][j]=Integer.parseInt(br.readLine());
}
for(i=1;i<=5;i++)
{
j=0;
System.out.print(" "+a[i][j]);
}
System.out.println();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
for(m=5;m>=1;m--)
{
for(k=1;k<=m;k++)
{
a1[m][k]=a[i][j]+a[i][j+1];
}
}
}
}
for(m=5;m>=1;m--)
{
for(k=1;k<=m;k++)
{
System.out.print(" "+a1[m][k]);
}
System.out.println();
}
}
}

please help

i tried this but got all values zer0

import java.io.*;
class nik1235
{
public static void main(String args[])throws IOException
{
int i,j,m,k;
int a[][]=new int[20][20];
int a1[][]=new int[20][20];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(i=1;i<=5;i++)
{
j=0;
a[i][j]=Integer.parseInt(br.readLine());
}
for(i=1;i<=5;i++)
{
j=0;
System.out.print(" "+a[i][j]);
}
System.out.println();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
for(m=5;m>=1;m--)
{
for(k=1;k<=m;k++)
{
a1[m][k]=a[i][j]+a[i][j+1];
}
}
}
}
for(m=5;m>=1;m--)
{
for(k=1;k<=m;k++)
{
System.out.print(" "+a1[m][k]);
}
System.out.println();
}
}
}

please help

You won't program it correctly till you get the details of how it works. Let's unwrap the cycles. No indexes and such .. just the way, how it should go:
there's 2 things I've changed to make it more understandable:
- I took only the summing part
- And I divided it on 2 parts: summing and memorizing

//processing first line:
        int sum = a[0][0] + a[0][1];
        a[1][0] = sum;

        sum = a[0][1] + a[0][2];
        a[1][1] = sum;

        sum = a[0][2] + a[0][3];
        a[1][2] = sum;
        
        sum = a[0][3] + a[0][4];
        a[1][3] = sum;
//-------------------------------------------
//processing second line:
        sum = a[1][0] + a[1][1];
        a[2][0] = sum;

        sum = a[1][1] + a[1][2];
        a[2][1] = sum;

        sum = a[1][2] + a[1][3];
        a[2][2] = sum;
//-------------------------------------------
//processing third line:
        sum = a[2][0] + a[2][1];
        a[3][0] = sum;

        sum = a[2][1] + a[2][2];
        a[3][1] = sum;
//-------------------------------------------
//processing fourth line:
        sum = a[3][0] + a[3][1];
        a[4][0] = sum;

That's what it actually supposed to do. Now all you have to to is wrap it back to short form with cycles. To do that you should mark out the dependencies in index changing. Here's some points of attention to you:
1. As adatapost suggested - you can use Jagged array. Because each next row in your array is less that previous by 1. Look closer on a example given by adatapost .. and more closer to the place, where each row is created.
2. Since that - there's no need to create 2 arrays. Also as there's no need in creating four time nested cycles .. you can easily reuse indexes.
3. Also to give more flexibility .. also and for ease of programming, you can use Array length. That's gonna release from keeping unnecessary information in your mind.

I think that's all for the first time :) .. try to apply that in your prog.

Dear Atenka,

i did not understand jagged array however i followed the "sum" code but not able to put it in a code i am not an experienced coder.As u can see i have put efforts at least now u can help me code the "sum" code part i am falling short of visualising the code and putting it like it should be in the code window.

if u could give me the code it will help me in future. thanks

I can help (that's what I'm actually do) .. but the ready-made code won't make from you

an experienced coder

.. till you won't fully understand it.

If you interested - I can explain the jagged array for you. If you not interested in that - you still can use the regular array, like you did before.

ok thanks

dear atenka
i know this is irritating
but through your "sum" logic i could get the first line but others are zeros
my code is

import java.io.*;
class atenka1
{
public static void main(String args[]) throws IOException
{
int arr[][]=new int[20][20];
int arr2[][]=new int[20][20];
int i,j,m,n;int sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(j=0;j<5;j++)
{
i=0;
arr[i][j]=Integer.parseInt(br.readLine());
}
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
sum=arr[i][j]+arr[i][j+1];
int x=j+1;
arr2[i+1][x-1]=sum;
System.out.print(arr2[i+1][x-1]);
}
System.out.println();
}
}
}

any suggestions?

Ok, good job.
But your code still remain tricky:

int arr[][]=new int[20][20];
int arr2[][]=new int[20][20];

Look, you missed the main thing of using 2 dimensional array. The point is: while you are moving through the first line of it - you're filling the 2nd line (of the same array .. so we can access it on the next loop iteration) to process on the next step.

Let's unwrap the code, you posted (in same manner):

//processing first line:
        int sum = a[0][0] + a[0][1];
        a2[1][0] = sum;

        sum = a[0][1] + a[0][2];
        a2[1][1] = sum;

        sum = a[0][2] + a[0][3];
        a2[1][2] = sum;
        
        sum = a[0][3] + a[0][4];
        a2[1][3] = sum;
//-------------------------------------------
//processing second line:
        sum = a[1][0] + a[1][1]; // point of attention
        a2[2][0] = sum;
//etc.

Here's what's in that point of attention: while we were processing the first line - we were adding results to the a2 array. And now, when we try to get the value of e.g. "a[1][0]" - we got zero, because in the a array only first row is filled with proper data (from users input).

Also I'm curious what are you trying to achieve here?

int x=j+1; // I mean, what for this "x" variable
    arr2[i+1][x-1]=sum;

P.S. That's not irritating at all. Everybody were starting from the beginning having same or similar troubles :P

i got your point, will think over it

your question about x

int sum = a[0][0] + a[0][1];  //j=1      
a2[1][0] = sum;    // j= j-1(0)  
 sum = a[0][1] + a[0][2];    //j=2   
 a2[1][1] = sum;       // j=j-1(1)
 sum = a[0][2] + a[0][3];        
a2[1][2] = sum;         
sum = a[0][3] + a[0][4];        
a2[1][3] = sum

however i have resolved it

initialing i and j in for loop with 1 as a2[j-1] gives 0-1=-1 giving arrayoutofbounds exception

uhu .. what if you would use just "j" (considering that you're extracting 1 from "x" on your next steps):

arr2[i+1][x-1]=sum;
System.out.print(arr2[i+1][x-1]);

can u please tell me how i do this

import java.io.*;
class atenka4
{
public static void main(String args[]) throws IOException
{
int arr[][]=new int[20][20];
int arr2[][]=new int[20][20];
int i,j,m,n;int sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(j=1;j<=5;j++)
{
i=1;
arr[i][j]=Integer.parseInt(br.readLine());
}
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
{
sum=arr[i][j]+arr[i][j+1];
arr2[i+1][j-1]=sum;
System.out.print(arr2[i+1][j-1]);//how to make this my new a[i][j]??
			//please tell me
}
System.out.println();
}
}
}

How about use this:

arr[i+1][j]=sum;

instead of:

arr2[i+1][j-1]=sum;

thanks a lot how can i thank you it worked
i need to practise a lot
u are too good

thanks again

:) you're welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.