# Array help needed

• March 9th, 2010, 11:22 AM
The Lost Plot
Array help needed
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
Code :

```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
Code :

```class 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
• March 10th, 2010, 07:08 AM
The Lost Plot
Re: Array help needed
Shop 1 – O’Connell Street had the most customers on Tuesday at 499 people

ok ive got the program to find the most visits per shop per day but im having trouble outputting the part of the line highlighted in red

i think i could use an if/ else if statements but this will make my code very bluky if you know what i mean is there a simpler way to do this
• March 10th, 2010, 09:58 AM
Faz
Re: Array help needed
OK the just looking at the code I think you just need to add a few index variables. It's definatly not the most elegant solution as I would have the print statement in the loop(this would need an array with day strings though so up to you). SO declare index(as ints and initialise them in the loop)

Code :

``` for(int shop = 0; shop < shoptotal.length;shop++) { if(shoptotal[shop][0]>largest1) { largest1 = shoptotal[shop][0]; index1 = shop } }```

The rest should be easy

EDIT: On second thought you could drop the largest variables and just use the index.
Code :

` on Monday with " + shoptotal[index1][0] + " Customers`

Once again it's not the most elegant solution as I would use a few nested loops but I don't know if your interested in completely changing your code.
• March 10th, 2010, 09:59 AM
helloworld922
Re: Array help needed
If you're just printing out to the console screen, I would strongly recommend against trying to get different colors. The only other color other than the standard black is the red (and actually, this only hold some times) is to use the System.err stream. However, this stream should only be used for Exception handling, and not for standard output.

If you want different colors, create a Swing application and use a JTextPane to format you colors accordingly. Note that you will need some way to distinguish between what should be red and what should be black. The easiest way usually is to keep the two in separate strings. Using if/else statements is the second method.
• March 10th, 2010, 10:05 AM
Faz
Re: Array help needed
Quote:

Originally Posted by helloworld922
If you're just printing out to the console screen, I would strongly recommend against trying to get different colors. The only other color other than the standard black is the red (and actually, this only hold some times) is to use the System.err stream. However, this stream should only be used for Exception handling, and not for standard output.

If you want different colors, create a Swing application and use a JTextPane to format you colors accordingly. Note that you will need some way to distinguish between what should be red and what should be black. The easiest way usually is to keep the two in separate strings. Using if/else statements is the second method.

I don't think he wanted to print it in red, I think he just meant that he didn't know how to print it at all. The highlighting was done to show us what he couldn't do.

I think.
• March 12th, 2010, 11:22 AM
The Lost Plot
Re: Array help needed
yeah the part in red was to show the bit of the output i was stuck on,i ended up declaring a second arary for the days and finding the highest total of visits for the week and not per day.

Cheers