Q.You should use a two dimensional array of 5 columns to represent each day and 4 rows to represent each shop.
Fill in the number of customers every shop had over the week using random numbers between 100 and 500.
The totals should be calculated and displayed beside each row when you output the array.
Ive no problem doing this part of the code but i cant figure out part b
class WS5Q1 { public static void main(String[] args) { int shoptotal [] [] = new int [4] [5]; String[] shopname = new String[4]; int total = 0; shopname[0] = "Grafton street "; shopname[1] = "O Connel street"; shopname[2] = "Blanchardstown "; shopname[3] = "Liffey Valley "; for(int shop=0; shop<4; shop++)//controls the rows { for(int col=0; col<5; col++)//controls the colum's { shoptotal[shop] [col] = (int)(Math.random()*10000000%500+1); //random number generator } } System.out.println(" Mon Tue Wed Thu Fri Total"); for(int shop=0;shop<shoptotal.length;shop++) { System.out.print("Shop:" + shop +":"+ shopname[shop]+" "); for(int col=0; col<shoptotal[shop].length; col++)//loop to output results and keep track of total for each week { total = total + shoptotal[shop][col]; System.out.print(shoptotal[shop][col] + " "); } System.out.println(+total); total = 0; } System.out.println(); }//end main }//end class
Problem (b)
Amend the above program such that the shop with the largest number of customers on a particular day is noted. For example, from the above sample data, the output would be:
Shop 1 – O’Connell Street had the most customers on Tuesday at 499 people
Here's my attempt
im not sure if im even going about this the right wayclass WS5Q2 { public static void main(String[] args) { int shoptotal [] [] = new int [4] [5]; String[] shopname = new String[4]; int total = 0, largest1 = 0, largest2 = 0, largest3 = 0, largest4 = 0, largest5 = 0; shopname[0] = "Grafton street "; shopname[1] = "O Connel street"; shopname[2] = "Blanchardstown "; shopname[3] = "Liffey Valley "; for(int shop=0; shop<4; shop++)//controls the rows { for(int col=0; col<5; col++)//controls the colum's { shoptotal[shop] [col] = (int)(Math.random()*10000000%500+1); //random number generator } } System.out.println(" Mon Tue Wed Thu Fri Total"); for(int shop=0;shop<shoptotal.length;shop++) { System.out.print("Shop:" + shop +":"+ shopname[shop]+" "); for(int col=0; col<shoptotal[shop].length; col++)//loop to output results and keep track of total for each week { total = total + shoptotal[shop][col]; System.out.print(shoptotal[shop][col] + " "); } System.out.println(+total); total = 0; } System.out.println(); for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][0]>largest1) { largest1 = shoptotal[shop][0]; } } for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][1]>largest2) { largest2 = shoptotal[shop][1]; } } for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][2]>largest3) { largest3 = shoptotal[shop][2]; } } for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][3]>largest4) { largest4 = shoptotal[shop][3]; } } for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][4]>largest5) { largest5 = shoptotal[shop][4]; } } System.out.println("had the most visits on Monday with " +largest1+ " customers"); System.out.println("had the most visits on Tuesday with " +largest2+ " customers"); System.out.println("had the most visits on Wednesday with " +largest3+ " customers"); System.out.println("had the most visits on Thursday with " +largest4+ " customers"); System.out.println("had the most visits on Friday with " +largest5+ " customers"); }//end main }//end class
any help would be greately appricated