• 100% Satisfaction Guarantee
Leela-TheProgrammer, Computer Software Engineer
Category: Programming
Satisfied Customers: 474
Experience:  Post Grad in CS (Gold Medal)
21067471
Leela-TheProgrammer is online now

# Do you know how to how to check if elements in rows

Hello,

My code below is meant to only print "YES" when it finds a row that contains all ones in java but when it finds a column that contains all ones it still prints "YES"! How can I fix my code below so that it only prints "YES" when it only finds a row all equal to 1?

Many thanks,

Samia

import java.util.*;
public class fixst
{
public static void main(String [] args)
{ int total =0;
int [][] a = {{1,7,2},{7,1,8}}; //2D ARRAY
boolean yes=false;
//TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s
for(int i = 0;i<a.length; i ++) //loops through rows
{
for(int j = 0; j<a[i].length; j ++) //loops through columns
{
int num = a[i][j];

if(num == 1) //everytime number is ***** adds to total
{
total++;
}

if (total==a[0].length) //if total equals to rows length it means all elements in a row are equal to 1

{
yes = true;

}

}

}
if(!yes)
{
System.out.println("NO");
} else {
System.out.println("YES elements in a row are all equa to 1! ");
}

}
}

Hi, Thanks for using JustAnswer.This is Leela and I will help you with this question today. I will check the code and provide you a solution. Thanks,Leela
Customer: replied 2 years ago.

Thanks :) please as soon as possible as i've got a deadline at 11:45pm tonight.

Sure. I am working on it. Will provide you in max 30 minutes.
Customer: replied 2 years ago.

Also if possible and you have time, I have one more question which i'm really struggling with. I can pay you more if you can please write the following program in java and explain to me how it works...

Write a program that prints “YES” if in each row of A, all the elements are distinct and “NO” otherwise. (Hint: First write a function that takes as input a one-dimensional array and checks if all elements in this array are distinct. Then, apply this function to each row of a matrix (treating this row as a one dimensioal array). Print “YES” if and only if the function returns TRUE for each row.

Please find code below: import java.util.*;public class fixst { public static void main(String[] args) { int total = 0; int[][] a = { { 1, 1, 2 }, { 3, 1, 1 } }; // 2D ARRAY boolean yes = true; //variable to check if a specific row has all 1's int rowCount_1 = 0; // variable to track the number of rows which has all 1's // TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s for (int i = 0; i < a.length; i++) // loops through rows { yes = true; // by default set it to true for (int j = 0; j < a.length; j++) // loops through columns { int num = a[j]; //if any number other than 1 is found, then set yes to false and stop processing current row if (num != 1) { yes = false; break; } } // When a row is processed check if that row has all 1's, accordingly increment 1's rowcount if (yes) { rowCount_1++; } } // if 1s row count is > 0 that means there is atleast ONE row with all 1s if (rowCount_1 > 0) { System.out.println("YES elements in atleast one row are all equal to 1! "); } else { System.out.println("NO"); } }}Please let me know if you have any queries. I can help you with your other question also, please post a question for ME so that i can answer it for you.
I started working on the SECOND PROGRAM also.PLEASE ACCEPT THE PROPOSAL and SHARE YOUR DETAILS mail, skype or Google chat via PRIVATE CONTACT INFO so that I can contact you quickly and explain the program. I will provide a solution in FEW MINUTES, please post a message as soon as you see my message.
Please provide your email or skype id via PRIVATE CONTACT info securely. I shared my contact details in PRIVATE CONTACT INFO section, please check them and contact me. I started working on the 2nd question and I have a DOUBT.. Should i print YES when atleast 1 ROW has all distinct values OR only when ALL ROWS have distinct values? Thanks,Leela
Customer: replied 2 years ago.

Only when ALL ROWS have distinct values please. Sorry copied the code you sent me into Bluej and I got this error for a[j] which says int[] can not be converted to int! Also I cannot do skype tonight but I can on another day when your free. For the meantime can you please clearly comment on the code.

Many thanks,

Samia

Hi Samia,About question 2: OK. I am clear about 2nd question.About question 1: About the error you are seeing in Bluej, In my code there is no a[j], it should be a[j]. Please check it let's quickly check the same via remote session. Please click on the LINK BELOW and we can do a quick remote session.http://www.justanswer.com/remotedesktop/join.aspx?Session=96a22933-00fd-4fe8-ab79-567e50537f60 Thanks,Leela
It should be a [j] (remove space between and [j]) there is problem in the chat window it seems.
There is a problem in the CHAT WINDOW CODE IS NOT COMING properly, please provide your email address, I will email you the CODE. It should bea [ i ] [ j ]remove all the spaces.
Customer: replied 2 years ago.

I'm afraid i'm still having the same problem with my code. My email is***@******.***

There is problem with the CHAT window. Please check your email and acknowledge the receipt of it.
Did you get the CODE for the 1st program?
Customer: replied 2 years ago.

Yes I did. Thank you I will shortly provide you with a ratting. It finally fully works now :)

Thanks for acknowledging. I am working on the SECOND program Will provide it in 15 minutes.
Completed the LOGIC, adding comments and sending you the code in 5 minutes.
Sent the java code by email, use that code and let me know if there are any queries. Providing same code on chat also, but it may have some problems, so use it from email. import java.util.*;public class RowAllDistinct { public static void main(String[] args) { int total = 0; int[][] a = { { 1, 3, 2 }, { 3, 2, 1 } }; // 2D ARRAY boolean yes = true; // variable to check if a specific row has all 1's int rowCount_distinct = 0; // variable to track the number of rows which has // all different elements // TO CHECK IF THERE'S A ROW IN 2D ARRAY THAT HAS ALL 1s for (int i = 0; i < a.length; i++) // loops through rows { yes = areAllRowElementsSame(a); // When a row is processed check if that row has all disctint elements // accordingly increment distinct rowcount if (yes) { rowCount_distinct++; } } // if 1s row count is > 0 that means there is atleast ONE row with all // 1s if (rowCount_distinct == a.length) { System.out .println("YES elements in ALL rows are distinct! "); } else { System.out.println("NO"); } } public static boolean areAllRowElementsSame(int[] row) { if (row.length == 0) { return true; } else { //set cannot have duplicates Set mySet = new HashSet(); //add each element in array to set for(int elem:row) mySet.add(elem); //if size of set and row length are same means // the row has all different elements //System.out.println("row as list: " + Arrays.asList(row)); //System.out.println("set size:" + mySet.size() + " row length:" + row.length); //if set size is same as array row size that means set has all the elements in array row // which means array row has ALL distinct elements if (mySet.size() == row.length) { return true; }else{ return false; } } }}
Please ACKNOWLEDGE the receipt of the CODE and also let me know if you have any queries. If you need my help in the future just post a question starting with "FOR TOUGHGUY ONLY"and I will be notified of the question and I can quickly help you. Incase i am not available to help some other expert will help you, that way you get timely solution. All the best. Thanks,Leela
I clearly documented the logic. Please let me know if you have any queries. Submit the assignment soon and I can clarify all your doubts if any later.
Customer: replied 2 years ago.

Thank you it works. I understand everything but this:

//set cannot have duplicates
Set mySet = new HashSet();
//add each element in array to set
for(int elem:row)