how to manage different vertices in triangle - java

in the inner for loop I have 0<p1,p2,p3<3 and they are integer. I want this for loop to assign value from 0 till 3 to each parameter in demoMethod.i.e., once the for loop executes it will send parameter like (1,2,3) to demomethod and for the second time it will send parameter (2,3,0) to the demoMethod. also the order of these three numbers are not important and they must be different .it means that after two times that for loop executes it doesn't send parameter like (1,2,3) and (2,3,1). thanks
public void Points(List<Point> pointList) {
int n = pointList.size();
if (n <= 2) {
System.out.println("null");
} else if (n == 3) {
drawingLine();
} else {
for(int i = 0;i<n;i++){
for(int j = 1;j<=(n-1)*(n-2)*(n-3)/6;j++){
demoMethod(p1,p2,p3);
}
}
}
}

I am not entirely sure what you are trying to do, but if I understand you correctly, you want to do something like this (?):
for(int i = 0;i<n;i++){
for(int j = 1;j<=(n-1)*(n-2)*(n-3)/6;j++){
int p1 = j % 4;
int p2 = (j + 1) % 4;
int p3 = (j + 2) % 4;
demoMethod(p1,p2,p3);
}
}

Related

Why is this not working? (Java for loop condition part)

int cnt = 0, sum = 0;
for(int i = 1; ((i <= 1000) && (i%3 == 0) && (i%5 == 0)); i++){
if(cnt >= 5)
break;
System.out.println(i);
sum += i;
cnt++;
}
System.out.println(sum);
I want to get the first 5 numbers that are divisible by 3 as well as 5 between the range of [1, 1000]. This code does not shows any error but it returns 'sum' and 'cnt' both to it's default values (i.e. 0) and seems to me that it's not entering the for loop.
P.S. I am not advance level programmer. I'm trying this stuff just for exploration, so if any one could explain the actual reason behind this.
It sounds like you do not understand how a for loop works. #Silvio Mayolo and #experiment unit 1998X commented some very helpful links. I would encourage you to read through those and get comfortable with them.
In your example, your for loop has a very complex header.
for(int i = 1; ((i <= 1000) && (i%3 == 0) && (i%5 == 0)); i++){
Let's break this apart into sections.
for -- this is the keyword that starts the for loop
int i = 1; -- now you have created a variable in your loop. It is an int called i, and it has a starting value of 1.
(the termination expression) -- If the termination expression returns false, then loop stops looping. Let's see how you used it
i <= 1000 -- now you have added a rule --> in order for the for loop to perform a loop, the number i must be less than or equal to 1000. Ok, that is simple enough, you are now going to have at least 1000 valid values for i thus far.
i%3 == 0 -- now you have added another rule --> in order for the loop to perform a loop, the number i must have no remainder when divided by 3. Unfortunately, that is a problem because your starting value for i is 1, as mentioned earlier, and 1/3 DOES have a remainder. Therefore, the loop stops before it even starts.
This is your problem. If you want to check and see if i%3 == 0, you should not put that check inside of your termination expression. It should go inside of the enclosing block within the for loop. And the same applies for i%5 == 0.
Here is a runnable example to help you understand.
public class SOQ_20220516
{
public static void main(String[] args)
{
int cnt = 0;
int sum = 0;
for(int i = 1; i <= 1000; i++)
{
if(cnt >= 5)
{
break;
}
if ((i%3 == 0) && (i%5 == 0))
{
System.out.println(i);
sum += i;
cnt++;
}
}
System.out.println(sum);
System.out.println(cnt);
}
}
public class SOQ_20220516
{
public static void main(String[] args)
{
int cnt = 0;
int sum = 0;
for(int i = 1; i <= 1000; i++)
{
if(cnt >= 5)
{
break;
}
if ((i%3 == 0) && (i%5 == 0))
{
System.out.println(i);
sum += i;
cnt++;
}
}
System.out.println(sum);
System.out.println(cnt);
}
}
Blockquote

How can i use a nested loops to help identify if i have 2 different matching pairs of dices

So I just had a lesson on loops and nested loops. My professor said that nested loops can help us do tasks such as knowing if we rolled 2 different matching pairs with 4 dices (4242) I'm a bit confused on how that would work.
So I started to work it out and this is what I was able to create.
public boolean 4matchDice(String dice){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
}
}
I used boolean as the return as it will tell us whether or not we have 2 different matching pairs.
Thing is, what do I put in the the loops? That's what's confusing me the most.
Here's a solution I came up with, seems to be returning the correct outcome for all the test cases I ran.
public static boolean matchDice(String dice) {
char[] diceArray = dice.toCharArray();
int pairs = 0;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < dice.length(); j++) {
if (diceArray[i] == diceArray[j]) {
diceArray[i] = 'X';
diceArray[j] = 'Y';
pairs++;
}
}
}
return (pairs > 1);
}
If you're only comparing two sets with two dice each, this is enough:
public boolean match4Dice(int first, int second, int third, int fourth) {
if ((first == third && second == fourth) || (first == fourth && second == third)) {
return true;
}
return false;
}
But if you're comparing 2 sets with any number of dice, the following algorithm would suit you better.
public boolean matchDice(String firstDiceSet, String secondDiceSet) {
// validate input, string must contain numbers from 1 - 6 only.
// lenghts of strings firstDiceSet & secondDiceSet must be equal
// works for any number of dice in each set.
// The dice only match if all numbers in the firstDiceSet all present in the secondDiceSet also.
// Let us count the matching numbers to check if this true.
int numberOfMatches = 0;
for (int i = 0; i < firstDiceSet.length(); i++) {
for (int j = 0; j < secondDiceSet.length(); j++) {
if (firstDiceSet[i] == secondDiceSet[j]) { // and not used
// increment number of matches
// mark secondDiceSet[j] as used, so that you do not count the same match twice.
// account for cases where firstDiceSet = "33" and the secondDiceSet = "35"
}
}
}
// your dice set match if the following condition is true
return (numberOfMatches == secondDiceSet.length());
}
Hi I just coded up a solution that will take "4242" as an input, although sindhu_sp's method is more practical i believe. I just wanted to show you another example to help your learning of java!
public static boolean fourMatchDice(String dice){
int match = 0;
for (int i = 0; i < dice.length(); i++){
for (int j = i+1; j < dice.length(); j++){
if (dice.toCharArray()[i] == dice.toCharArray()[j]){
System.out.println("does " + dice.toCharArray()[i] + " = " + dice.toCharArray()[j]);
match ++;
}
}
}
if(match == 2) *EDIT* //Change to (match >= 2) if 4 of the same pair is allowed.
return true;
return false;
}
public static void main(String[] args) {
System.out.println(fourMatchDice("4242"));
}
output:
does 4 = 4
does 2 = 2
true

Java, how to add 2 arrayLists of different sizes

So i am currently working on a project for a course that i am doing, and i am writing a method that needs to add two ArrayLists containing integers together, and sets the sum in a new ArrayList. currently i have this, which is working fine
public BigInt add(BigInt otherBigInt) {
BigInt result = new BigInt();
int length = digitList.size() - 1;
int lengthOther = otherBigInt.digitList.size() - 1;
int temp = 0;
int whole = 0;
int carry = 0;
for (int i = length; i >= 0; i--){
temp = (digitList.get(i) + otherBigInt.digitList.get(i));
temp += carry;
// temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
if (temp >= 10){
whole = temp - 10;
carry = 1;
result.digitList.add(whole);
}
else if (temp < 10){
carry = 0;
result.digitList.add(temp);
}
}
if (carry == 1){
result.digitList.add(carry);
}
//adds any remaining carried number after for loop
// Supply this code.
return result;
}
however, currently the method only works if the arrayLists are of the same size, how would i modify the method to accommodate lists of varying size? an example of this would be two lists containing 735934 and 68945 giving the result 804879.
p.s.
not sure if it's needed, (still new to posting here) but the two lists I'm currently adding are 7359 and 6894, giving the answer 14253.
If my assumption is correct, then you are trying to simulate a case where you add two numbers using two lists, where one digit of the number occupies one index of the list.
Simplest solution would be to assume that the shortest list has zero's instead of no value and add till the max of the two lists:
public BigInt add(BigInt otherBigInt) {
BigInt result = new BigInt();
int length = Math.max(digitList.size(), otherBigInt.digitList.size()) - 1;
int lengthOther = otherBigInt.digitList.size() - 1;
int temp = 0;
int whole = 0;
int carry = 0;
for (int i = length; i >= 0; i--){
temp = ( checkAndGet(digitList, i) + checkAndGet(otherBigInt.digitList, i) );
temp += carry;
// temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
if (temp >= 10) {
whole = temp - 10;
carry = 1;
result.digitList.add(whole);
}
else {
carry = 0;
result.digitList.add(temp);
}
}
if (carry == 1){
result.digitList.add(carry);
}
//adds any remaining carried number after for loop
// Supply this code.
return result;
}
// if the index position being retrieved is larger than the size, assume 0
private int checkAndGet(List<Integer> input, position) {
return (input.size() < position) ? input.get(position) : 0;
}
Let the two digit array be a1[0 ... n1] and a2[0 ... n2]. Now your algorithm would be something like :
add(a1, a2):
min_length = min{a1.length, a2.length}
result[0 ... max{a1.length, a2.length} + 1]
carry = 0
for(i in [0 ... min_length - 1])
result[i] = carry + a1[i] + a2[i]
carry = result[i] / 10
result[i] %= 10
while(i < a1.length)
result[i] = a1[i]
while(i < a2.length)
result[i] = a2[i]
result[i] = carry
Notice that you have to add the remaining digits in case they are not of same size. I assume that the digits are stored in order i.e. a[0] is the 1st digit.
I would iterate through the smaller array backwards, adding the indexes together as you go:
ArrayList<Integer> toIterate = (array1.size() > array2.size)? a1 : a2;
ArrayList<Integer> seperate = (array1.size() > array2.size)? a2 : a1;
for (int i = toIterate.size - 1; i >= 0; i --) {
if (seperate.get(i) != null) {
arrayResult.add(toIterate.get(i) + seperate.get(i));
}
else {
arrayResult.add(toIterate.get(i));
}
}

Best way to generate pascal's triangle (of two mentioned ways)

I have an programming assignment in Java.
I have implemented it by making an nCr ( http://en.wikipedia.org/wiki/Combination ) function then using a double for loop to make the triangle by printing it out.
However, the assignment calls for a uneven 2d array to be created and then populated by adding the two numbers from the previous lines and then printing the array out.
I know I am going to have to do the assignment the way it asks, however I have a small feeling that (at least for small triangles anyway) that the approach I have implemented is better.
Which is the better approach?
I'd think the method called for by the assignment would be better. You're method requires a number of multiplications to calculate each of the elements of the triangle. This number will increase for each row of the triangle you need to calculate.
The assignment's method, however, requires a single addition for each element of the triangle.
If I understand your question, you are trying to compare two approaches to generating Pascal's triangle:
By running an nCr function to populate each cell of the triangle.
By generating the triangle in one pass by filling in each cell by a simple addition.
The second approach seems hands-down better. Am I missing something? Even if you use memoization in your nCr function there is overhead in those calls.
By using recursion
/*By using recursion*/
class RecursivePascal {
public static void main(String args[]) {
int n = 100;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
//System.out.print(i+","+j+" ");
System.out.print(pascal(i, j) + " ");
}
System.out.println();
}
}
static int pascal(int i, int j) {
if (j == 0)
return 1;
else if (j == i)
return 1;
else {
return pascal(i - 1, j - 1) + pascal(i - 1, j);
}
}
}
By using simple logic
/*By using logic*/
class p {
public static void main(String args[]) {
int one[] = {1};
int n = 13;
System.out.println("1");
for (int j = 0; j < n; j++) {
int two[] = new int[one.length + 1];
int twoCounter = 0;
for (int i = 0; i < one.length; i++) {
if (i == 0) {
two[twoCounter++] = one[i];
System.out.print(one[i] + " ");
}
if (i != 0) {
two[twoCounter++] = one[i] + one[i - 1];
System.out.print((one[i] + one[i - 1]) + " ");
}
if (i == one.length - 1) {
two[twoCounter++] = one[i];
System.out.print(one[i] + " ");
}
}
System.out.println();
one = two;
}
}
}

storing those points that are not in triangles

I have written a code which first get some points(has x ,y) and then I will check all possible triangles with those points and I will check that the specific point is in a triangle or not(with determinate) but I have problem with this part of code which finds the external points and internal points.it doesn't work well.please help me thanks.
public void externalPoints(List<Point> pointList) {
// this method will check that a point is in a (n-1)(n-2)(n-3)/6 triangles or not
int n = pointList.size();
if (n <= 2) {
System.out.println("null");
} else if (n == 3) {
drawingLine();
} else {
for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
for (int k = 2; k < n; k++) {
for (int m = 3; m < n; m++) {
if (isPointInTriangle(pointList.get(i), pointList.get(j), pointList.get(k), pointList.get(m)) == true) {
System.out.println("is in the triangle");
break;
} else {
System.out.println("is not in a triangle");
newList.add(pointList.get(i));
}
}
}
}
}
}
}
Also isInTriangle method is like this site :link text
Can you explain why it isn't working well?
It seems that your method of iterating over points is a little dodgy. For example, if n == 5, your program will evaluate isPointInTriangle for i = j = k = m = 4, meaning that it's trying to figure out whether Point 4 is inside a triangle formed from vertices 4,4,4. If you're using the first method in your link, isPointInTriangle(4,4,4,4) will return true, even though the shape you gave it isn't actually a triangle... you probably want to assert that your vertices are distinct points.

Categories

Resources