hi people i am new to this site ... i needed a bit of help i have got an home work to create this
:the use will enter 6 random numbers and the computer will print the series
:input: 10,5,6,4,3,6

output:

10 5 6 4 3 6
5 1 2 1 3
4 1 1 2
3 0 1
3 1
2

i have tried to do it using arrays but i can do it but by a process which wastes momory ..i declare 6 arrays and then do calculation ...and print all the 6 arrays in correct format
is there any way to do it in one or 2 arrays ...im not asking for a full code ...instead just some suggestions

btw in the series above the number in each line after the first one is the difference between the two numbers above it (left and right)

Recommended Answers

All 7 Replies

Sure print the first array, create a second array, calculate into it, print that, redefine the first array as a the third array (or simply use a "last index" type variable) and calculate the second into that, rinse, lather, repeat.

Edit: Better of course, would be to simply recalculate the first value "in place" and use a "last index" type variable.

Sure print the first array, create a second array, calculate into it, print that, redefine the first array as a the third array (or simply use a "last index" type variable) and calculate the second into that, rinse, lather, repeat.

Edit: Better of course, would be to simply recalculate the first value "in place" and use a "last index" type variable.

a code snipette would be great cuz i have tried the above method ....i am not able to do the first method without having a very long code and as for the second one ...i an ending up deleting one esential number for calculation

I'm sure it would be. Post your code and we will help you correct it. The last method is the one you want to use.

I'm sure it would be. Post your code and we will help you correct it. The last method is the one you want to use.

instead i alreadh have one code with me il post that here

instead i alreadh have one code with me il post that here

here is the code ...it still needs some tweaking and correcting

import java.math.*;
import java.io.*;
class pattern
{
static void main()throws IOException
{
int arrcnt=0;int num1=0;int num2=0,chk=0,chk2=0;int nume=0;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("how many numbers you want to input");
int times=Integer.parseInt(br.readLine());
int arr[][]=new int[times][times*2];int numstr;
System.out.println("please input "+times+" numbers");
for(int i=0;i<times;i++)
{
for(int j=0;j<times*2;j++)
{
arr[i][j]=99;
}
}
for(int i=0;i<times;i++)
{
arr[0][arrcnt]=Integer.parseInt(br.readLine());
arrcnt=arrcnt+2;
}
for(int i=0;i<times-1;i++)
{
for(int j=0;j<times*2;j++)
{
if(arr[i][j]!=99)
{
if(chk==0)
{
num1=arr[i][j];
chk++;
}
else
{
num2=arr[i][j];
chk2++;
}
}
if(chk2!=0)
{
arr[i+1][j-1]=Math.abs(num1-num2);
}
}
}
for(int i=0;i<times;i++)
{
for(int j=0;j<times*2;j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}

it is giving me an index out of bounds at line 45

Like I said, go back and try the second method

print the arrays values
Create an int assign it the length of the array.

Then while that variable is greater than 1
loop through the array from 1 while the index is less than that variable
and assign to index - 1 the result of subtracting the values at index and index - 1
print that array's values up the element at that variables value minus 2
subtract one from that variable

Like I said, go back and try the second method

print the arrays values
Create an int assign it the length of the array.

Then while that variable is greater than 1
loop through the array from 1 while the index is less than that variable
and assign to index - 1 the result of subtracting the values at index and index - 1
print that array's values up the element at that variables value minus 2
subtract one from that variable

umm that method would be a bit complicated for me ...can u please help me with the code i have provided above :D

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.