Repository navigation
Expand file tree
/
Copy pathDay17.java
More file actions
125 lines (87 loc) · 3.96 KB
/
Copy pathDay17.java
File metadata and controls
125 lines (87 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package JHomeworks;
import java.util.Arrays;
public class Day17 {
// walmart costco corrado 's AVG
// apple 3 4 5 4
// milk 3 3 3 3
// meat 4 5 4 4.33
// bread 1 1 2 1.33
// TOTAL 11 13 14
// given table,
// part1. calculate sum of price in each market(walmart,costco,corrado's)
// expected output is:
// Total at Walmart is: ???
// Total at costco is: ???
// Total at corrado's is: ???
// part2. calculate average price of each item(apple, milk, meat, bread)
// expected output is:
// average price of apple is: ???
// average price of milk is: ???
// average price of meat is: ???
// average price of bread is: ???
public static void main(String[] args) {
int[][] prices = {
{3, 4, 5, 5},//apple => (3+4+5+5) / 4 = 4.25 (AVG)
{3, 3, 3, 5},//milk => (3+3+3+5) / 4 = 3.5 (AVG)
{4, 5, 4, 3},//meat => (4+5+4+3) / 4 = 4.0 (AVG)
{4, 5, 4, 3},//bread => (4+5+4+3) / 4 = 4.0 (AVG)
{1, 1, 2, 34}};//pepsi => (1+1+2+34) / 4 = 9.5 (AVG)
//sum=15,18,18, 50
//wal,costco,corrado,amazon
double[] sumItemArr = new double[prices.length];
double[] avgArr = new double[prices.length];
double[] sumMarket = new double[4];//prices[0].length
for (int urun = 0; urun < prices.length; urun++) {
//System.out.println("prices.length = " + prices.length); //5
//System.out.println("urun = " + urun);
for (int mar = 0; mar < prices[urun].length; mar++) { //4
//System.out.println("mar = " + mar);
//System.out.println("prices["+i+"].length = " + prices[i].length);
sumItemArr[urun] += prices[urun][mar];
//System.out.println("sumItemArr[urun] = " + sumItemArr[urun]);
sumMarket[mar] += prices[urun][mar];
//System.out.println("sumMarket[mar] = " + sumMarket[mar]);
}
avgArr[urun] = sumItemArr[urun] / 4; //prices[0].length
}
System.out.println("sumMarket = " + Arrays.toString(sumMarket));
//System.out.println("sumArr = "+Arrays.toString(sumItemArr));
System.out.println("avgArr = " + Arrays.toString(avgArr));
String[] stores = {"Walmart", "Costco", "Corrados", "amazon"};
String[] items = {"apple", "milk", "meat", "bread", "pepsi"};
for (int i = 0; i < items.length; i++) {
System.out.println("AVG of " + items[i] + " = " + avgArr[i]);
}
for (int j = 0; j < stores.length; j++) {
System.out.println("SUM of " + stores[j] + " = " + sumMarket[j]);
}
int[][] prices2 = {
new int[]{3, 4, 5, 5},
new int[]{3, 3, 3, 5},
new int[]{4, 5, 4, 3},
};
//you have to put the size
int[][] price3 = new int[3][5];
int check = prices[4].length;
// int[] storeSum = new int[prices[0].length];
// int[] itemAvg = new int[prices.length];
System.out.println("AVG--------------------------------");
for (int row = 0; row < prices.length; row++) {
System.out.println("prices.length = " + prices.length);
double sum = 0;
for (int col = 0; col < prices[row].length; col++) {
sum += prices[row][col];
}
double avg = sum / prices[row].length;
System.out.println(items[row] + " avg: " + avg);
}
System.out.println("SUM--------------------------------");
for (int j = 0; j < prices[0].length; j++) { //marketlerdeki degerleri gezmis oluyorum 4 marketim var
double sum = 0;
for (int i = 0; i < prices.length; i++) {
sum += prices[i][j];
}
System.out.println(stores[j] + " sum: " + sum);
}
}
}