java - Summing the results of an array multiplication -
i need sum result of multiplication of these 2 arrays:
item store 0 1 2 3 4 0 25 64 23 45 14 1 12 82 19 34 63 2 54 22 17 32 35 item cost per item 0 $12.00 1 $17.95 2 $95.00 3 $86.50 4 $78.00
i have sum results of first row, second row, , third row , display them separately , add them up. code i'm using program following:
import java.util.*; public class asd { public static void main(string[] args) { double items[][]= new double[3][5]; double cost[]=new double[5]; loadarray(items, cost); system.out.println("total amount of sales each store : "); computecost(items, cost); printarray(items, cost); } public static void loadarray(double items[][], double cost[]) { scanner input = new scanner(system.in); string s1; int num, x, y; for(x=0; x<items.length;x++) { for(y=0; y<items[x].length; y++) { system.out.println("enter next item of data:"); items[x][y]=input.nextdouble(); } } //cost of items: cost[0]=12.00; cost[1]=17.95; cost[2]=95.00; cost[3]=86.50; cost[4]=78.00; } public static void printarray(double items[][], double cost[]) { system.out.println("number of items sold during day: "); int row, col; (row =0; row<items.length ; row++) { for(col=0; col<items[row].length; col++) { system.out.print( items[row][col]+" "); } system.out.println(); } system.out.println("cost per item: "); int i; (i =0; < 5; i++) { system.out.println(cost[i]); } } public static void computecost (double items[][], double cost[]) { int row, col; double productarray[]=new double[3]; (row =0; row<items.length ; row++) { for(col=0; col<items[row].length; col++) { if(col==0) { productarray[row]=items[row][col]*cost[0]; system.out.println(productarray[row]+" test1 "); } else if(col==1) { productarray[row]=items[row][col]*cost[1]; system.out.println(productarray[row]+" test2 "); } else if(col==2) { productarray[row]=items[row][col]*cost[2]; system.out.println(productarray[row]+" test3"); } else if(col==3) { productarray[row]=items[row][col]*cost[3]; system.out.println(productarray[row]+" test4 "); } else if(col==4) { productarray[row]=items[row][col]*cost[4]; system.out.println(productarray[row]+" test5 "); } } } } }
the computecost()
method multiplication takes place, i'm wondering how can store values of multiplication can later add them up.
(for example output of computecost()
method first run :
300.0 test1 1148.8 test2 2185.0 test3 3892.5 test4 1092.0 test5
i need way store values , add them up, since they're inside of loops , if statements, i'm not sure of how that.
any appreciated.
you there already. going wrong overwriting value @ index particular row @ each new column. should increment value instead. possible use same variable each row (instead of array), because don't reuse saved value in way.
also, not needed create if-else structure did checking column considering. can generalize part using value of col
variable instead.
the new computecost()
method this:
public static void computecost (double items[][], double cost[]) { (int row = 0; row < items.length; row++) { double rowsum = 0.0; for(int col = 0; col < items[row].length; col++) { double colproduct = productarray[row] = items[row][col] * cost[col]; rowsum += colproduct; system.out.println("row: " + row + ", col: " + col + ": " + colproduct); } system.out.println("total row " + row + ": " + rowsum); } }
Comments
Post a Comment