-
Program with for loops help
Hey guys, I'm new to Java, and we're learning the For Loop in class, I don't understand it at all, and I need to create this object using for loops and nested for loops:
Code :
___*___
__***__
_*****_
*******
The object has 3 _'s then an asterix, then another 3. The next line has 2 _'s and 3 asterixs, and then 2 _'s, and so on till the last line which is 7 asterixs.
If I could have help with writing the code, it would be greatly appreciated, :D.
-
Re: Program with for loops help
[CODE]wahh i have school today if i had a long time for this right now i ill try to do this...
anyway try to study this first ..
same problem but slightly easy rather than that (helloworld922 gave this to me)
Code :
public class LoopSample {
public static void main(String[] args) {
int firstLoop,
secondLoop;
for (firstLoop = 3; firstLoop > 0; firstLoop--) {
for (secondLoop = firstLoop; secondLoop > 0; secondLoop--){
System.out.print(">");
}
System.out.println("");
}
}
}
you can see this
the firstloop will generate the count.(together with the next lines). the second loop will generate the character's display according to the count...
try to study its algorithm you'll find it out...
-
Re: Program with for loops help
The code's going to look somewhat different than the one chronoz gave, but it's the same general idea. It all comes down to what's the pattern? Once you can figure out a simple algorithm, it's not too hard to code.
It looks like with each new line the number of asterisks increase by 2 from the previous line, and the number of _'s decrease by 2, and stops when the line is all asterisks. I'm also assuming that the line width is always going to be odd. So, a simple little algorithm would be this:
Code :
1. Input how wide of a box you want = width (must be odd number)
2. Have numberAsterisks = 1
3. While numberAsterisks < width:
{
4. numberUnderscores = width - numberAsterisks
5. Draw out numberUnderscores/2 underscores
6. Draw out numberAsterisks asterisks
7. Increase numberAsterisks by 2
}
That's the whole algorithm, now it's your turn to turn this into code :)
-
Re: Program with for loops help
wait, I can see some spaces. is it also in the part of the loop?
-
Re: Program with for loops help
oh sorry.. there's no spaces.. i was confused if asterisk ,, its a charcter that will appear on a 'top'
i thought is was spaces.. sory,,
-
Re: Program with for loops help
im already here please help!
Code :
for (int x = 1; x <= 7; x = x + 2) {
for (int j = x; j > 0; j--) {
System.out.print("*");
}
System.out.println("");
}
}
}
output
im stuck with askterisks.. i dont know where should i start
-
Re: Program with for loops help
oh my God ! im here... cant believe it ..
Code :
for (int x = 1; x <= 7; x = x + 2) {
for (int p = x; p <= 5; p++) {
System.out.print("_");
}
for (int j = x; j > 0; j--) {
System.out.print("*");
}
for (int k = x; k <= 5; k++) {
System.out.print("_");
}
System.out.println("");
}
output:
Code :
_____*_____
___***___
_*****_
*******
-
Re: Program with for loops help
DONE DONE DONE!! i MADE it... but i wont deletete my first two post i want someone to check it... for me to know my first mistakes... (please dont think i'm spamming...)
yehey!! idid it!! what an ease!!
here's the final CODE!!
Code :
for (int x = 1; x <= 7; x = x + 2) {
for (int p = x; p <= 5; p = p + 2) {
System.out.print("_");
}
for (int j = x; j > 0; j--) {
System.out.print("*");
}
for (int k = x; k <= 5; k = k + 2) {
System.out.print("_");
}
System.out.println("");
}
}
OUTPUT:
Code :
___*___
__***__
_*****_
*******
-
Re: Program with for loops help
woo hoo thanks guys, i still have no idea what the hell i'm doing, but at least i got the answer to my challenge problem!
-
Re: Program with for loops help
:( That's not the purpose of us helping you. We're here to help you learn, not just blindly give you the answer.
I must say, though, nice work chronoz13.
-
Re: Program with for loops help
oh sori helloworld.. i understand what you mean.. its just that... i also want to figure it out..
anyway ixjaybeexee, I started also like that.. i want other people to make the program for me.(coz i dont know where to start).. but that's not where it ends.. i have to study it. and understand how it works.... so if i ever encounter similiar difficulties, I already have the guts and some knowledge to deal with it....
but if you can try to figure it out..try it first by your own.. but if theres no more way out... then ask for help...
anyway im happy that we statisfy you... hehe happy coding...! \m/
-
Re: Program with for loops help
ahmm helloworld. can you show the code for this.. i want to study its algorithm and to compare its flow with the one that i made
Code :
1. Input how wide of a box you want = width (must be odd number)
2. Have numberAsterisks = 1
3. While numberAsterisks < width:
{
4. numberUnderscores = width - numberAsterisks
5. Draw out numberUnderscores/2 underscores
6. Draw out numberAsterisks asterisks
7. Increase numberAsterisks by 2
}
-
Re: Program with for loops help
Haha, it's ok chronoz. You're turning out to be quite the programmer :)
Haha, I actually just found a slight problem in the algorithm i posted. I forgot to draw the end underscores :) Here's the revised algorithm:
Code :
1. Input how wide of a box you want = width (must be odd number)
2. Have numberAsterisks = 1
3. While numberAsterisks < width:
{
4. numberUnderscores = width - numberAsterisks
5. Draw out numberUnderscores/2 underscores
6. Draw out numberAsterisks asterisks
7. Draw out numberUnderscores/2 underscores
8. Increase numberAsterisks by 2
9. Draw out new line
}
I haven't actually checked if this code is correct, but here it is:
Code :
public static void drawPattern(int width)
{
if (width % 2 == 0)
{
System.out.println("width must be odd!");
return;
}
int numberAsterisks = 1;
while (numberAsterisks < width)
{
int numberUnderscores = width - numberAsterisks;
for (int i = 0; i < numberUnderscores/2; i++)
{
System.out.print("_");
}
for (int i = 0; i < numberAsterisks; i++)
{
System.out.print("*");
}
for(int i = 0; i < numberUnderscores/2; i++)
{
System.out.print("_");
}
numberAsterisks += 2;
System.out.println("\n");
}
}
-
Re: Program with for loops help
so thats it wahahahah I was having a very hard time "what will I do in numbers 5.) and 6.)"
"huh".. Im really extracting the step-by-step logic that you posted from 1 - 7 whahahah so thats it
5.) and 6.) whahahaha
anyway tnx for this and that helloworld...wahahahha
-
Re: Program with for loops help
Whoa that's a bit harder... pre-defined method haha., im not yet running that program but i notice that ...
it has a little bit logical similarities with mine...
the main loop will generate the next lines .("\n") and the rest of the loops that is NESTED with the main loop will generate the corresponding characters...(JSon taught and explained me this...shhhh) ^#(^
ahmm helloworld.. is it possible to make this program in a fully nested loop.. as in loop inside a loop inside a loop inside a loop and so on.. until it reaches the main loop. My eyes is starting to get white thinking of that
waheheheh ... :-?:confused:
-
Re: Program with for loops help
I'm only on my second chapter in Computer Science class, so I really don't know a whole lot
-
Re: Program with for loops help
Hehe, yeah, i don't know why i decided to put it inside a method, but I was just going through the algorithm for any general width.
I'm kind of confused by what you mean by fully nested loop. I have 3 for loops in series nested inside of a while loop. Did you mean like i did with the other question you had drawing "<"?
-
Re: Program with for loops help
exactly ... as in loop inside a loop inside a loop that will generate the same out put...
or just a single loop.... hahaa... i cant think of it... ei wait regardig with your method im not yet testing it... in the condition for the remainder what value it should return?
-
Re: Program with for loops help
I don't think this is possible in just 1 for loop. It's really not useful to have a loop inside a loop inside a loop here... remember, each inner loop contributes to the overall run-time. My code as-is is O(n^2), where as having what you're proposing would be O(n^3).
My method doesn't return any value because I declared it void. However, you can still use the return statement even if the method returns nothing (note: only works for the void return type)
-
Re: Program with for loops help
ahh so its ok if i remove the return statement... and helloworld regarding with the big 'O' notation ... i dont have any idea about ...
and regarding with the drawPattern that can generate by a single loop...
i would like to share my code..
this was done by my friend...
Code :
public static void main(String[] args) {
int c = 5;
for (int a = 1; a <= c; a++) {
System.out.print("*");
if (a == 5) {
System.out.println("");
a = 0;
c--;
}
}
we were coding this in 'C' syntax when we where in school a while ago and it generate this output
but when i apply the logic in 'java'' syntax, i dont know why it happened like this..
the the 3,2,1's part is not printing...
-
Re: Program with for loops help
Link: Big O notation
Basically, that's the time it takes your algorithm to run with respect to some value n (usually how many times through you have to go). Big O is very useful when n is huge, but not so useful when n is small. However, the difference in times when n is small is negligible.
umm, that code shouldn't work in either C or Java. It's because you're always comparing to 5 in the if statement instead of C.
Code :
public int main() {
int c = 5;
for (int a = 1; a <= c; a++) {
printf("*");
if (a == [b]c[/b]) {
printf("\n");
a = 0;
c--;
}
}
I really have the feeling that somewhere in this i've messed up my C syntax, but you get the idea :P
the converse code in Java would be this:
Code :
public static void main(String[] args) {
int c = 5;
for (int a = 1; a <= c; a++) {
System.out.print("*");
if (a == [b]c[/b]) {
System.out.println("");
a = 0;
c--;
}
}
-
Re: Program with for loops help
tnx helloworld... for correcting the code for me... anyway .. its a bit confusing for me trying to understand the logic of big 'O' .. :))
ahh , i want to compare the value of 'a' into 5 (that's the logic) but my mistake is.. i compared it in a numerical value(literally)... so instead the of comparing the value of 'a' to 'c' which is decrementing,
the value of condtion in 'if' stucks in 5, although the value of 'c' decrements.. :))
-
Re: Program with for loops help
It's ok if you don't understand big-O for now. I'm sure sometime in the future you will take a CS algorithm class, and they will go over all the gooey bits of big-O notation.
I think for now, it's sufficient to say this:
Big-O notation describes how fast a function's runtime increases. Obviously, we want runtime of an algorithm to be as short as possible, so the best Big-O performance is O(1). It's not always possible to get O(1) performance (actually, it's very rare), so we can shoot for O(n), O(log(n)), O(n*log(n)),O(n^2).... Any function of n that's limit at infinity is infinity ( O(1) is an exception here, it's limit is 1).
Mathematically speaking, here's a general breakdown of big-O terms I have seen and there runtime performance relative to each other:
O(1) < O(log(log(n))) < O(log(n)) < O(n) < O(n log(log(n)) < O(n log(n)) < O(n^2) < O(n^3) < O(n^4) < O(2^n) < O(Fib(n)) < O(n!) < O(2^(2^n))
The most common ones are O(1), O(log(n)), O(n), O(n log(n)), O(n^2), and O(n^3)
You'll also find some crazy ones, like the performance of shell sort: O(n^(5/4)), or even crazier O((log(n))^(6+ε)) for the AKS primality test.
-
Re: Program with for loops help
:confused::eek:
what the.! anyway... ill keep that.. !! heheheheh