java - How can I sum the product of two two-dimensional arrays? -
so got code 2 arrays: 1 array contains tickets sold 3 cinemas , other 1 contains adult , kid prices. code outputs total every cinema separately (3 lines of output) need total number of 3. instead of printing 828 cinema1, 644 cinema2, 1220 cinema3 , need print 2692 (total of 3 cinemas). how can sum 3 products loop? here's code:
public class arrays { public arrays() {} public static void main(string[] args) { float[][] = new float[][] {{29, 149}, {59, 43}, {147, 11}}; float[] b = new float[] {8, 4}; string[] s = new string[] {"cinema 1", "cinema 2", "cinema 3"}; string[] t = new string[] {"adults", "children"}; int i,j; system.out.println("cinema complex revenue\n\n"); ( = 0 ; <= 2 ; i++ ) { ( j = 0 ; j < 1 ; j++ ) { system.out.println(s[i] + "\t$" + (a[i][j] * b[j] + a[i][j + 1] * b[j + 1])); } } } }
and output: 1
just code want.
int i, j; float sum = 0; (i = 0; < a.length; i++) { (j = 0; j < a[i].length && j < b.length; j++) { sum += a[i][j] * b[j]; } } system.out.println(sum);
or if want use 1 for
loop, may be
int i; float sum = 0; (i = 0; < a.length * b.length; i++) { sum += a[i / b.length][i % b.length] * b[i % b.length]; } system.out.println(sum);
Comments
Post a Comment