Eddie Gerlach

Greenhorn
+ Follow
since Oct 23, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Eddie Gerlach

See what I mean? Even in my reply, I'm not coding correctly, (k2 instead of n2) but can see where the issue is...in my fat fingers!

Thanks again everyone for your help! Taking a break and will be back at it tomorrow..

Cheers!
10 years ago
Sorry for my confusion, ya'll....went out for a run to clear my head...I think it may have helped....

the original code used a while loop:

int gcd = 1; // initial gcd is 1
int k = 2; //possible gcd
while (k <= n1 %% k <= n2) {
if ( n1 % 2 == 0 && n2 % 2 == 0)
gcd = k;
k++;
}

I then replaced it with a for loop:

int gcd =1; //initial gcd is 1
for (int k = 2; k <= n1 && k <= k2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}

Finally, when asked to insert the "/2" into the loop-continuation-condition, I was instructed that this is wrong and was asked why..(in the book)

for (int k =2; k <= n1 / 2 && k <= n2 / 2 ; k++) {
if (n1 % k == 0 && n1 % k == 0)
gcd = k;

For all 3 conditions, when I used the example 125 and 2525, gcd = 25...so this obviously left me wondering why the 3rd set of code was incorrect...however, when I enter 3 and 3 for the 3rd set of code, gcd is 1....it should be 3, correct? Same goes for 2 and 2..gcd should be 2, not 1...Hence my inquiry here...

BTW, Emil, when I take your parameters (n1 = 16 and n2 = 24) into the 3rd set of code, my answer is indeed 8...so there is my nightmare, folks.... isn't this fun?!

Appreciate the assistance everyone.
.....swimming, swimming, swimming.....


10 years ago
Winston,

Thanks for catching that...I was simply reading from my textbook and I could've entered the code incorrectly....
10 years ago
Emil n Piet,

From what I can gather, 'while' these two conditions exist, AND if (n1 % k == 0 && n2 % k == 0), then 'k' is incremented by 1...since it started at 2, shouldn't it then be incremented by 1 and then gcd is 3? However, since this is a Boolean statement that requires the first statement to evaluate as true, and if it doesn't, it ignores the 2nd condition? Then the loop starts anew until a new value is stored for gcd (or 'k') or k becomes >= n1 or n2?

Sorry, but noobie brainfarts and perhaps a bit "loop"y...

Eddie
10 years ago
Gosh, I'm a boob....the gcd of 3 and 3 is 3, not 1! Went back and examined my code.....when I enter the '/2' divisor in the 'for' loop and re-evaluate, it's an incorrect process to determine the "%" remainder, yes? swimming, swimming, swimming.....
10 years ago
Winston,

thanks for the follow-up, but if I enter 3 and 6 (which means the answer should be 3 as the gcd), I still get 1.....however, when I enter 125 and 2525, I get 25....so it works part of the time....hmmmm, when I remove the divisor '/2' from both sides of the Boolean expression, I come up with 3.....I've done something foul and need to re-examine my code...God, I love and hate this all at the same time..
10 years ago
Winston,...he shows a link to that in the book.. however, I haven't looked at it yet...and will try not to worry too much.... ;)

here's the exercise...
import java.util.Scanner;

public class Listing4_9GreatestCommonDivisor {
//** Main method */
public static void main(String[] args) {
//Create a Scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int n1 = input.nextInt();
System.out.print("Enter second integer: ");
int n2 = input.nextInt();

int gcd = 1; //initial gcd is 1

for (int k = 2; k <= n1/2 && k <= n2/2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k; //update gcd

}

System.out.println("The greatest common divisor for " + n1 +
" and " + n2+ " is " + gcd);
}
}

So I ran the program in NetBeans, entered 3 for both integers and my gcd was 1, yet was told the current 'for' loop was wrong by illustration....if one takes out the "/2" in the for loop, then it's correct.....yet I come up with the same answer w/o '/2' and if I used a 'while' loop as follows;

while (k <= n1 && k <= n2) {
if (n1 % 2 == 0 && n2 % 2 == 0)
gcd = k;
k++;

Hope this helps and illustrates things a bit better for everyone...
10 years ago
Hi!

I'm teaching myself Java w/Dr. Liang's Intro to Java, 9th Ed., Comprehensive.....Chapter 4, Loops....in attempting to find the greatest common divisor (gcd) of two integers, n1 and n2, whereas k is a possible gcd and gcd is initialized to 1, the following code has been entered:



for (int k = 2; k <= n1 && k <= n2; k++) {
if ( n1 % 2 == 0 && n2 % 2 == 0)
gcd= k;
}

When asked to change the previous line of code to this:

for (int k = 2; k <= n1 / 2 && k < n2 / 2; k++){

the questions states this revision is wrong and to find the reason.....well, I've changed it, entered "3" (per the answer key) for n1 and n2....

now I can see logically where k (2 in this example) is not <= n1/2, which is 3/2 or 1, since we're dealing w/integers, yet when I compile and run, my answer is indeed, gcd = 1. However, since this is a Boolean expression where && is being used, since the first portion evaluates to "false", the 2nd portion isn't executed and thus my result of 1?......sorry, loops are throwing me for one, for sure....can someone clarify, please?

Thanks!
10 years ago
Campbell,

Thank you for the reply, but I don't think we've covered that yet....it says in the example that I needed to use "(that is, each possible combination is terminated with a newline character)"

I was under the impression that "\n" was the newline character, but felt it was unnecessary because of each new "System.out.print"....attempts to concatenate the result w/o using multiple println's gave me more errors....humph!

At any rate, MyProgrammingLab is now happy with the code and it also works in my NetBeans, too.....hoo-ray Beer!

Eddie Gerlach
Green Greenhorn

12 years ago
Henry,

Yes, exactly what i was thinking, what if there's 5, 8 or more names, the combinations would be ridiculous to figure out manually....but I haven't learned that yet....still in Chapter 2....I'm just happy as a clam to be coding without any prior programming experience....here's what it looks like now...

name1 = stdin.next();
name2 = stdin.next();
name3 = stdin.next();
System.out.println(name1 + ", " + name2 + " and " + name3 + "\n");
System.out.println(name2 + ", " + name3 + " and " + name1 + "\n");
System.out.println(name3 + ", " + name1 + " and " + name2 + "\n");
System.out.println(name1 + ", " + name3 + " and " + name2 + "\n");
System.out.println(name2 + ", " + name1 + " and " + name3 + "\n");
System.out.println(name3 + ", " + name2 + " and " + name1);

geez....I'm getting there...slowly! Thanks again!

Eddie Gerlach
Greenhorn
12 years ago
Sorry to bother ya'll! I just realized I needed another three lines of code to show all possible combinations of the three names, hence a total of 6 lines of code....talk about feeling sheepish!

Happy Thanksgiving!
12 years ago
Happy Thanksgiving, everyone!

I'm running into an issue with a homework problem and could use some guidance....I've included my code and the question for further review and assistance...

Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can't agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.

Here's my code I've written on NetBeans:

import java.util.Scanner;

public class ThreePartners {
public static void main(String [] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter three names:");
String name1 = stdin.next();
String name2 = stdin.next();
String name3 = stdin.next();
System.out.print(name1 + ", " + name2 + " and " + name3 + "\n");
System.out.print(name2 + ", " + name3 + " and " + name1 + "\n");
System.out.print(name3 + ", " + name1 + " and " + name2);

}
}

And here's my result....although it says it's successful on NetBeans, I'm not quite sure it's right even though it's "successful", plus MyProgrammingLab through Pearson and Dr. Liang's Intro to Java 9th Ed., Comprehensive isn't happy with the code, either... :/

run:
Enter three names:
Larry
Curly
Moe
Larry, Curly and Moe
Curly, Moe and Larry
Moe, Larry and CurlyBUILD SUCCESSFUL (total time: 13 seconds)

Thanks again for the assistance....this is much harder than I have anticipated, yet I'm having the time of my life and really enjoying this!

Eddie Gerlach
Greenhorn
12 years ago
Campbell,

Nice article....read the link of "they engaged in mortal combat" and it took me to an interesting site, which I bookmarked (Teach Yourself Programming in 10 Years!), and then I downloaded an edited reprint of an article called "What Every Computer Scientist Should Know About Floating-Point Arithmetic" by David Goldberg....

Appreciate the breadth and depth of knowledge being shared!

Eddie Gerlach
Greenhorn
12 years ago
Winston,

Thanks a bunch! What an awesome article! I did some brief programming, like 30 years ago in high school learning BASIC, which is procedural, if I remember correctly, but nothing like this...I did research on Java and found it fascinating, plus the wages weren't too shabby, either! I realize it will take some time to reach a level of competency and professionalism, but have never been more excited about my new career choice! I'll probably be the oldest entry-level programmer in the world, but hey, I can use that to my advantage, too!....cheers!

Eddie Gerlach
Greenhorn
12 years ago