I was coding a Java program for AP CSA that took the max value from the columns of a two dimensional array, and it was used to determine and print out which area of a brand was the most popular/or had the highest amount of sales. It also took the min value, and it determined and which area of brand had the lowest amount of sales.
I had created two one dimensional arrays, one is for the 5 sneaker brands which were sold, and the other is for the four district/locations where they were sold.
String[] brands = { "ACTION", "DEFY", "GEL", "JETPRO", "MAGNUM"
};
String[] districts =
{
"North", "West", "South", "East" };
I then made a two dimensional array that contained the following numbers in correspondence to:
int[][] sales = {
{ 17, 57, 35, 48, 31 },
{ 35, 36, 53, 78, 76 },
{ 55, 56, 96, 44, 56 },
{ 35, 44, 34, 43, 44 }
};
In order to find the max and the min as I mentioned above, I had to make a nested for loop to run through the 2d array, row by row rather than column by column, to check the values depending on the location/area of the sneaker sale. And I had two variables that saved the max value and the min value as well as the indexes for both of them, and that's where things started to go wrong.
In the beginning, I set max and min to start at the first element of the 2d array, and change based on the if statement. Indexes were started at 0.
The if statement checked if there were any elements per column less than the min or greater than the max. If it did, then it would set min/max to that particular element in the list, and the maxIndex and the minIndex (the position at where the min/max was found) will be updated accordingly.
It works fine for the max value, however with over thousands of failed attempts, the if statement to check the min never worked.
Here's the entire code, with the starting variables and the for loop:
int max = sales[0][0];
int min = sales[0][0];
int minIndex = 0;
int maxIndex = 0;
for (int col = 0; col < sales[0].length; col++) {
for (int dist = 0; dist < sales.length-1; dist++) {
min = sales[dist][col];
// if this element is larger than any other element
if(sales[dist+1][col]<min) {
min = sales[dist][col];
minIndex = dist;
System.out.print("Min value: " + sales[minIndex][col] + "\n");
}
if(max<sales[dist][col]) {
maxIndex = dist;
max = sales[dist][col];
System.out.print("Max value: " + max + "\n");
}
}
System.out.println();
System.out.println(brands[col]);
System.out.println("Most popular: " + districts[maxIndex]);
System.out.println("Least popular: " + districts[minIndex]);
minIndex = 0;
max = sales[0][0];
}
Thousands of failed attempts never did anything to fix my problem with the if statement, that was used to check for the min value in the column. Since I can't give you thousand examples, I will show you my first attempt.
if(sales[dist][col]<min) {
minIndex = dist;
min = sales[dist][col];
}
I expected it to change the min accordingly, everytime the inner for loop ran. However, I tried a bunch of different things and that didn't help either.
Here's my second attempt to fix, but ended up failing: (To see the for loop and other code, check the problem details section where I give a big overview of what's happening)
min = sales[dist][col];
// if this element is larger than any other element
if(sales[dist+1][col]<min) {
min = sales[dist][col];
minIndex = dist;
System.out.print("Min value: " + sales[minIndex][col] + "\n");
}
I thought that this would work, but didn't return anything. During all of my attempts, the value of min stayed at [0][0] before the loop and after the loops finished iterating completely.
I really need help to find the root cause of my problem, so any help would be appreciated.
Since you want a min and a max value per column, you should move the variables inside your outer (first) for-loop and do the max/min check in the inner loop:
public static void main(String[] args) {
String[] brands = { "ACTION", "DEFY", "GEL", "JETPRO", "MAGNUM"};
String[] districts = { "North", "West", "South", "East" };
int[][] sales = {
{ 17, 57, 35, 48, 31 },
{ 35, 36, 53, 78, 76 },
{ 55, 56, 96, 44, 56 },
{ 35, 44, 34, 43, 44 }
};
for (int col = 0; col < sales[0].length; col++) {
int max = sales[0][col];
int min = sales[0][col];
int minIndex = 0;
int maxIndex = 0;
for (int dist = 0; dist < sales.length; dist++) {
if(sales[dist][col] < min) {
min = sales[dist][col];
minIndex = dist;
}
if(sales[dist][col] > max) {
maxIndex = dist;
max = sales[dist][col];
}
}
System.out.println();
System.out.println(brands[col]);
System.out.println("Most popular: " + districts[maxIndex]);
System.out.println("Least popular: " + districts[minIndex]);
}
}