jafal
July 9, 2021, 4:34pm
1
hi all
I want to perform multiplication and addition for this array so that the result is as follows
thanks in advance
int [] intArray = new int[] {1, 1, 1,-1, 1};
println("Original Array:");
for (int i=0; i<intArray.length; i++)
print(intArray[i] + " ");
println();
println(" reverse order:");
for (int i=intArray.length-1; i>=0; i--)
print(intArray[i] + " ");
------------------------------------------------------
i want to reach this result
{1, 1, 1,-1, 1} orginal * reverse {1,-1, 1, 1, 1}
1 1 1 -1 1
-1 -1 -1 1 -1
1 1 1 -1 1
1 1 1 -1 1
1 1 1 -1 1
------------------------------------------------------
1 0 1 0 5 0 1 0 1
You can change your array to a IntList how have reverse method implement
2 Likes
jafal
July 11, 2021, 10:25pm
4
hi all
i made a code but its very long how i can use for loop to make it shorter
int[] numbers = new int[5];
void setup() {
size(200, 200);
numbers[0] = 1;
numbers[1] = 1;
numbers[2] = 1;
numbers[3] = -1;
numbers[4] = 1;
int a = numbers[4] *numbers[0];
int b = numbers[4] * numbers[1];
int c = numbers[4] * numbers[2];
int d = numbers[4] * numbers[3];
int e = numbers[4] * numbers[4];
println(a, b, c, d, e);
int a1 = numbers[3] *numbers[0];
int b1 = numbers[3] * numbers[1];
int c1 = numbers[3] * numbers[2];
int d1 = numbers[3] * numbers[3];
int e1= numbers[3] * numbers[4];
println(a1, b1, c1, d1, e1);
int a2 = numbers[2] *numbers[0];
int b2 = numbers[2] * numbers[1];
int c2 = numbers[2] * numbers[2];
int d2 = numbers[2] * numbers[3];
int e2= numbers[2] * numbers[4];
println(a2, b2, c2, d2, e2);
int a3 = numbers[1] *numbers[0];
int b3 = numbers[1] * numbers[1];
int c3 = numbers[1] * numbers[2];
int d3 = numbers[1] * numbers[3];
int e3= numbers[1] * numbers[4];
println(a3, b3, c3, d3, e3);
int a4 = numbers[0] *numbers[0];
int b4 = numbers[0] * numbers[1];
int c4 = numbers[0] * numbers[2];
int d4 = numbers[0] * numbers[3];
int e4= numbers[0] * numbers[4];
println(a4, b4, c4, d4, e4);
int x1 = a;
int x2 = b+a1;
int x3 = c+b1+a2;
int x4 = d+c1+b2+a3;
int x5 = e+d1+c2+e3+a4;
int x6 = e1+d2+c3+b4;
int x7 = e2+d3+c4;
int x8 = e3+d4;
int x9 = e4;
println(x1, x2, x3, x4, x5, x6, x7, x8, x9);
}
void draw() {
}
Flolo
July 12, 2021, 10:21am
5
Hey
you can do the upper half with a nested for loop but I don’t know if you can do the lower half with a for loop. Unfortunately, I didn’t manage to do it
I hope I could help you
Here is my code:
int[] numbers = new int[]{1, 1, 1, -1, 1};
ArrayList<Integer> abc;
void setup() {
size(200, 200);
surface.setVisible(false);//so that the canvas is not displayed
abc = new ArrayList<Integer>();
//index 0 = 4 * 0
//index 1 = 4 * 1
//index 2 = 4 * 2
//index 3 = 4 * 3
//index 4 = 4 * 4
//index 5 = 3 * 0
//index 6 = 3 * 1
//index 7 = 3 * 2
//index 8 = 3 * 3
//index 9 = 3 * 4
//index 10 = 2 * 0
//index 11 = 2 * 1
//index 12 = 2 * 2
//index 13 = 2 * 3
//index 14 = 2 * 4
//index 15 = 1 * 0
//index 16 = 1 * 1
//index 17 = 1 * 2
//index 18 = 1 * 3
//index 19 = 1 * 4
//index 20 = 0 * 0
//index 21 = 0 * 1
//index 22 = 0 * 2
//index 23 = 0 * 3
//index 24 = 0 * 4
//int a = 0;
//int b = 1;
//int c = 2;
//int d = 3;
//int e = 4;
//println(a, b, c, d, e);
//int a1 = 5;
//int b1 = 6;
//int c1 = 7;
//int d1 = 8;
//int e1 = 9;
//println(a1, b1, c1, d1, e1);
//int a2 = 10;
//int b2 = 11;
//int c2 = 12;
//int d2 = 13;
//int e2 = 14;
//println(a2, b2, c2, d2, e2);
//int a3 = 15;
//int b3 = 16;
//int c3 = 17;
//int d3 = 18;
//int e3 = 19;
//println(a3, b3, c3, d3, e3);
//int a4 = 20;
//int b4 = 21;
//int c4 = 22;
//int d4 = 23;
//int e4 = 24;
//println(a4, b4, c4, d4, e4);
for (int i = numbers.length-1; i >= 0; i--) {
for (int j = 0; j < numbers.length; j++) {
int a = numbers[i] * numbers[j];
print(a + " ");
abc.add(a);
}
println("");
}
int x1 = abc.get(0);
int x2 = abc.get(1) + abc.get(5);
int x3 = abc.get(2) + abc.get(6) + abc.get(10);
int x4 = abc.get(3) + abc.get(7) + abc.get(11) + abc.get(15);
int x5 = abc.get(4) + abc.get(8) + abc.get(12) + abc.get(19) + abc.get(20);
int x6 = abc.get(9) + abc.get(13) + abc.get(17) + abc.get(21);
int x7 = abc.get(14) + abc.get(18) + abc.get(22);
int x8 = abc.get(19) + abc.get(23);
int x9 = abc.get(24);
println(x1, x2, x3, x4, x5, x6, x7, x8, x9);
}
void draw() {
}
1 Like
jafal
July 12, 2021, 2:44pm
6
Flolo
hi
nice code for 5 elements but for 16 or 32 it is going to be much longer i have tried to use for loop but failed until now thanks for your time
What is the purpose of the arrays?
1 Like
jafal
July 12, 2021, 3:21pm
8
@Chrisir
thanks for response …actually it is Barker code 5 bits calculation and it might up to length codes of N < 10^22 and calculation 13 bit length as i did with the 5 bit length with out for loop is going to be very long code
this is the idea and you can see what i calculated i take length 5 which is {1, 1, 1,-1, 1};
jafal
July 12, 2021, 3:28pm
9
@Chrisir the idea of solving is simple
jafal
July 12, 2021, 3:42pm
10
@Chrisir
this is some information with c
c++, for-loop
Sorry, I am on a journey now and can’t really help you
1 Like
jafal
July 12, 2021, 6:03pm
12
it is fine when you can i am not in hurry have fun
1 Like
Okay, when you only have 16 or 32 items specifically then write a code that use println in a for loop
Let it println the lines of code you need for the main Sketch such as
1 Like
jafal
July 12, 2021, 7:26pm
14
@Chrisir you are true but if my array like this
int [] intArray = new int[] {1, 1, 1,-1, 1,+1 +1 +1 +1 +1 −1 −1 +1 +1 −1 +1 −1 +1};
solving it in this way going to take very long lines
int x1 = abc.get(0);
int x2 = abc.get(1) + abc.get(5);
int x3 = abc.get(2) + abc.get(6) + abc.get(10);
When you write the code lines automatically by another Sketch B it doesn’t matter
In fact you can let B insert “\n” after each „+“ automatically
Just use ctrl-t in your main Sketch A after pasting the result from B into A
Here is an example for a Sketch that writes code
It generates for loop, your Sketch B generates code with + etc.
Did you ever had to write 9 nested for-loops?
for (int i0 = 0; i0 < 2; i0++) {
for (int i1 = 0; i1 < 2; i1++) {
for (int i2 = 0; i2 < 2; i2++) {
....
......
I wrote another Sketch to write these 9 lines with for-loops for me, so I don’t have to do it.
You can copy paste the auto-generated code from the direct window in processing:
void setup() {
size(800, 600);
int numberOfDesiredForLoops = 9;
for (int i = 0; i < numberOfDesiredForLoops; i++) {
…
1 Like
int[] a = new int[] {1,2,3,4,5};
int[] ar = new int [a.length];
int[][] b = new int[a.length][a.length*2-1];
int counter = 0;
int[] c = new int[a.length*2-1];
int[][]d = new int [a.length][a.length];
for (int i=0; i<a.length; i++)ar[i] = a[a.length-1-i];
for (int i=0; i<a.length; i++) {
println("ar",ar[i],"a",a[i]);
for (int j=0; j<a.length; j++) {
b[i][j+i] = ar[i]*a[j];
println("output",b[i][j+i],ar[i],a[j]);
}
println("index", i, a[i]);
}
for (int i=0; i<b.length; i++) {
String s = "";
for (int j=0; j<b[i].length; j++) {
s+= b[i][j];
}
println(s);
}
for (int i=0; i<b[0].length; i++) {
counter = 0;
for (int j=0; j<b.length; j++) {
int pos = j;
counter+= b[j][i];
}
c[i] = counter;
counter++;
}
println("",counter);
for (int i=0; i<c.length; i++) {
print(c[i]);
}
2 Likes
jafal
July 14, 2021, 2:25am
18
@paulgoux
thank you it is creative idea
It probably can be simplified but for loop and array logic isn’t my forte
Good little exercise though
1 Like
jafal
July 14, 2021, 2:30am
20
@paulgoux
the way you used is very nice