How do I replace zeros in a matrix? - java

Replace the elements with a value of 0.0 with the average of the adjacent elements: previous and / or next in the same row.
Here's my code. Can u help me to solve it?
public void replaceZeros(double[][] array){
for (int i = 0; i < array.length; i++){
for(int j = 0; i < array[i].length; j++){
if(array[i][j] == 0){
if(array[i][0] == 0){
array[i][j] = array[i+1][j];
} else if(j == array[i].length-1){
array[i][j] = array[i-1][j];
} else {
array[i][j] = (array[i+1][j]+array[i-1][j])/2;
}
}
}
}
}

If I understand correctly, you want to replace elements of an array that are 0 with the average of a neighbour to the left and right. In the code you provided, you increase and decrease second array argument which is the column. Try this code:
public void replaceZeros(double[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; i < array[i].length; j++) {
if (array[i][j] == 0) {
//check if its first element in a row
if (j == 0) {
array[i][j] = array[i][j + 1];
}
//check if its last element in a row
else if (j == array[i].length - 1) {
array[i][j] = array[i][j - 1];
}
//proceed with calculating average
else {
array[i][j] = (array[i][j + 1] + array[i][j - 1]) / 2;
}
}
}
}
}

Your for loop had minor issues, below for loop should work :
public void replaceZeros(double[][] array){
for (int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
if(array[i][j] == 0) { // If element is 0
if (j == 0) { // If element is in 1st column, any row
array[i][j] = array[i][j+1];
} else if(j == array[i].length-1) { // If element is in last column, any row
array[i][j] = array[i][j-1];
} else { // If element is not in 1st or last column, any row
array[i][j] = (array[i][j-1]+array[i][j+1])/2;
}
}
}
}
}

Related

Check if elements in first row in 2d array are the same

I'm looping through a 2d array. To check if the first row elements are the same/equal. I'm having an issue pulling this off. Below is what I have so far.
public void checkMatch(Values[][] val){
//TODO check elements in 2d array for matches
for(int i = 0; i < val.length; i++){
for(int j = 1; j < val[i].length; j++){
if(val[i][0].equals(val[i][1]) && val[i][0].equals(val[i][2])){
System.out.println("Match");
}else {
System.out.println("No Match");
}
}
}
}
Figured it out.
public void checkMatchRows(Values[][] val){
for(int i = 0; i < val.length; i++){
for(int j = 1; j < val[j].length -1; j++){
if(val[i][0] == val[i][1] && val[i][1] == val[i][2]){
System.out.println("Match");
}else {
System.out.println("No Match");
}
}
}
}

Trying to print out a diamond made of stars, and it cannot print even numbers

for one of my assignments I am required to print out stars in the shape of a diamond. When trying to print odd numbers, everything works out fine. However, when trying to print even numbered diamonds, the spacing in the middle lines is off, and a diamond is missing. Could someone please point out what I am doing wrong, and provide a possible fix?
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
Desired result, if the user enters 6 (d = 6)
If you notice your code, the second for loop is starting from
i = d in
for (int i = d; i > 0; i = i - 2)
but your first loop is not ending at d, you are doing i < d there which will stop before d, so it will go from 1 to 5 if your d value is 6.
Simply change your second main for loop to start from d-1, here is the code
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d-1; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
The code below will print a diamond pattern using a single loop:
private static void PrintDiamondSingleLoop(int row) {
row++;
int mid = row/2;
int midLeft = mid;
int midRight = mid;
int currentRow = 1;
int currentColumn = 1;
for (int i = 1; i <= row * row; i++)
{
if(i % row == 0)
{
System.out.println("");
if(currentRow < mid)
{
midLeft--;
midRight++;
}else
{
midLeft++;
midRight--;
}
currentColumn = 1;
currentRow++;
}
if (currentColumn >= midLeft && currentColumn <= midRight)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
currentColumn++;
}
}
(This code is taken from an article published on my website: https://programtown.com/how-to-print-diamond-pattern-using-single-for-loop-in-java/.)

My code that determines if two arrays are permutations always returns false, why?

I've written this to sort two arrays and then compare the values to see if they're the same, but it always returns false, and I can't figure out why.
It is supposed to find if two arrays are permutations of each other.
public class Permutations {
public static void main(String[] args) {
int[] a = {1,4,6,7,8,34};
int[] b = {34,6,8,1,4,7};
System.out.println(arePermutations(a, b));
}
public static boolean arePermutations(int[] a, int[] b)
{
int count = 0;
int temp = 0;
if(a.length == b.length)
{
for(int i=0; i<a.length-1; i++)
for(int j=0; j<a.length-1; j++)
if(a[i] > a[j+1] && i>j+1)
{
temp = a[i];
a[i] = a[j+1];
a[j+1] = temp;
}
{
for(int i=0; i<b.length-1; i++)
for(int j=0; j<b.length-1; j++)
if(b[i] > b[j+1] && i>j+1)
{
temp = b[i];
b[i] = b[j+1];
b[j+1] = temp;
}
}
for(int i=0; i<a.length; i++)
if(a[i] == b[i])
{
count++;
}
if (count == a.length)
{
return true;
}
else return false;
}
else return false;
}
}
The problem is in the implementation of the bubble sort. Change your sorting loops to the following:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length - 1; j++) {
if (b[j] > b[j + 1]) {
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
This code works because a bubble sort just swaps adjacent elements if they need swapping, but needs to run through the entire array multiple times (up to the number of items in the array) to get all the items into the correct position.

How would you draw an arrow using a loop ? (triangle rotated 90 degrees to the right)

I can print a triangle but I'd like to know how to get it to print in the form of an arrow like below and yes it is homework.
for (int i = 1; i < 10; i += 2)
{
for (int k = 0; k < (4 - i / 2); k++)
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
You can use the following code:
public static void main (String[] args)
{
int mid = 10/2; //where 10 is number of lines
for (int i = 0; i < 10; i ++) {
if(i < mid){
for(int j = 0; j < i; j++){
if(j == 0 || j == i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
} else{
for(int j = 10 - i; j > 0; j--){
if(j == 10-i || j == 1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println("");
}
}

How to check array for possible divisors?

Creating the array, I am letting the user choose the length:
StartNum = scan.nextInt();
int[] NumBox = new int[StartNum];
for (int i = 1; i < NumBox.length+1; i++)
{NumBox[i - 1] = i;}
NumBox[0]=0;
Assuming there are other methods that can change cells in NumBox to 0, how would I use a for loop to check each cells in the array for any divisor? If there are no divisors for the cell in the array, it will then become a 0. For example, if the array is [0,2,0,4,0,6,7,8,9] 9,2 and 7 would become a 0.
The code below is what I tired but didn't get far.
boolean NoDiv=false;
for (int a=1; a < NumBox.length+1; a++)
{
a++
for (int check=1; a < NumBox.length+1; check++)
{
if (NumBox[a-1]% check == 0 && NumBox[a-1] !=0)
{
NumBox[a-1] = 0;
}
}
}
for (int i = 0; i < NumBox.length; i++) {
if (NumBox[i] == 0) continue;
boolean hasDivisor = false;
for (int j = 0; j < i; j++) {
if (NumBox[j] == 0) continue;
if (NumBox[i] % NumBox[j] == 0) {
hasDivisor = true;
break;
}
}
if (!hasDivisor) NumBox[i] = 0;
}

Categories