Jump to content

Need help with nested for/while loops

CreepyPro

I need to print this picture and the number of symbols per line in this picture is n.

I've been stuck in this for 2 hours now so any help is aprecciated.

/
\\
///
\\\\
/////
\\\\\\
///////

Link to comment
Share on other sites

Link to post
Share on other sites

is it required that this is done with a nested loop? It could be very easily achieved with a loop and an if statement inside to check even or odd.

I WILL find your ITX build thread, and I WILL recommend the SIlverstone Sugo SG13B

 

Primary PC:

i7 8086k - EVGA Z370 Classified K - G.Skill Trident Z RGB - WD SN750 - Jedi Order Titan Xp - Hyper 212 Black (with RGB Riing flair) - EVGA G3 650W - dual booting Windows 10 and Linux - Black and green theme, Razer brainwashed me.

Draws 400 watts under max load, for reference.

 

How many watts do I needATX 3.0 & PCIe 5.0 spec, PSU misconceptions, protections explainedgroup reg is bad

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Fasauceome said:

is it required that this is done with a nested loop? It could be very easily achieved with a loop and an if statement inside to check even or odd.

Yeah, it says that each of the methods will have nested loops so I think so

Link to comment
Share on other sites

Link to post
Share on other sites

What have you tried so far? We won't do your homework for you but we can help you with specific problems in the code you already have.

 

-edit-

also take a look here, you problem sounds very similar to this one

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

<?
$n=20;
$a=0;
$c=1;
for($t=1;$t<$n;$t++){
$a++;
if ($a>$c){
$a=0;
$c++;
}
}
$tr=1;
$b=1;
for($t=0;$t<$c;$t++){
if ($tr==1){
$zn=chr(47);
$tr=0;
}
else
{
$zn=chr(92);
$tr=1;
}
$ou="";
for($g=0;$g<($t+1);$g++){
$ou=$ou.$zn;	
}
echo($ou."<br>");
}
?>

If I good understand. Example is in php.

 

$n is number of "\" or "/" in total.

Only full lines are displayed (not when number of "\" or "/" in last line is under required).

 

I hope it's what you want.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sauron said:

What have you tried so far? We won't do your homework for you but we can help you with specific problems in the code you already have.

Further to this point, try explaining to someone what you've done and step through it.

You may find your answer :D


 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, homeap5 said:

<?
$n=20;
$a=0;
$c=1;
for($t=1;$t<$n;$t++){
$a++;
if ($a>$c){
$a=0;
$c++;
}
}
//if ($a==$c) $c++;
$tr=1;
$b=1;
for($t=0;$t<$c;$t++){
if ($tr==1){
$zn=chr(47);
$tr=0;
}
else
{
$zn=chr(92);
$tr=1;
}
$ou="";
for($g=0;$g<($t+1);$g++){
$ou=$ou.$zn;	
}
echo($ou."<br>");
}
?>

 

If I good understand. Example is in php.

 

$n is number of "\" or "/" in total.

Only full lines are displayed (not when number of "\" or "/" in last line is under required).

 

I hope it's what you want.

 

Did you run this through an obfuscator? lol


 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, camjocotem said:

Did you run this through an obfuscator? lol

It's just few lines. Do you need comments and french fries for that? :)

Link to comment
Share on other sites

Link to post
Share on other sites

what programming language?

Here's Javascript, with cleverly named variables:

 

function drawTextFor(n=1) {
    let character = "/";
    let line = "";
    let text = "";
    for (let lineCounter=1;lineCounter<=n;lineCounter++) {
        line = "";
        character = (character=="\\") ? "/" : "\\"; // flip character at every line jump
        for (let charCounter = 1;charCounter<=lineCounter;charCounter++) {
            line = line + character;
        }
        text = text + ( (text=="") ? "" : "\n") + line;
    }
    return text;
}

console.log(drawTextFor(4));

 

Link to comment
Share on other sites

Link to post
Share on other sites

<?
$n=16;
$a=0;
$c=0;

do{
  $a++;
  $c=$c+$a;
}while($c<$n);

$a=$a-1;
if ($c==$n) $a++;
$tr=1;
$b=1;

for($t=0;$t<$a;$t++){
  if ($tr==1){
    $zn=chr(47);
    $tr=0;
  }
  else
  {
    $zn=chr(92);
    $tr=1;
  }
  $ou="";

  for($g=0;$g<($t+1);$g++){
    $ou=$ou.$zn;	
  }

  echo($ou."<br>");

}
?>

Simplified php version with tabs for everyone who loves that. :)

And "do...while" loop is used (if this is required somehow).

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, mariushm said:

what programming language?

Here's Javascript, with cleverly named variables:

But your script just write number of lines you want, not counting characters. And if I good understand - number of characters is a problem here, not number of lines.

 

Or maybe I wrong understand - OP first post is not so good explained. But then problem will be very easy to solve and I don't believe OP can have problem with that. It must be number of characters, not lines.

 

In my example you set number of characters and only full lines are displayed (so last line, if has not enough characters, is not displayed).

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, homeap5 said:

But your script just write number of lines you want, not counting characters. And if I good understand - number of characters is a problem here, not number of lines.

Unless I just don't understand the text of the problem, the number of symbols on a line in my example code is the line number (lineCounter variable in the first for)...1 character in 1st line, 2 characters in 2nd line, 3 characters in 3rd line.

 

So the total number of characters in the final text is 1+2+...+n + (n-1) characters (one newline character for each line but the last)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, homeap5 said:

It's just few lines. Do you need comments and french fries for that? :)

I'm a hungry boi for dem variable french fry names XD


 

Link to comment
Share on other sites

Link to post
Share on other sites

41 minutes ago, mariushm said:

Unless I just don't understand the text of the problem, the number of symbols on a line in my example code is the line number (lineCounter variable in the first for)...1 character in 1st line, 2 characters in 2nd line, 3 characters in 3rd line.

 

So the total number of characters in the final text is 1+2+...+n + (n-1) characters (one newline character for each line but the last)

Ran your script and got this output

\
//
\\\
////

 

Seems legit to me xD

If I was to be pedantic I'd say it starts off with a forwardslash in the example but the logic is there :')


 

Link to comment
Share on other sites

Link to post
Share on other sites

<?
$numberOfCharacters = 20;
$a = 0;
$c = 1;
for ($i = 1; $i < $numberOfCharacters; $i++) {
    $a++;
    if ($a > $c) {
        $a = 0;
        $c++;
    }
}

$useForwardSlash = 1;
for ($i = 0; $i < $c; $i++) {
    if ($useForwardSlash == 1) {
        $characterToAppend = chr(47); //Forwardslash
        $useForwardSlash = 0;
    } else {
        $characterToAppend = chr(92); //Backslash
        $useForwardSlash = 1;
    }
    $outputString = "";
    for ($ib = 0; $ib < ($i + 1); $ib++) {
        $outputString = $outputString.$characterToAppend;
    }
    echo($outputString."<br>");
}?>
So I put in some variable names here, could probably skip that first loop and just replace $c with $numberOfCharacters xP


 

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of a 0 and 1 , you can use a boolean variable and simply say  variable = !variable  where ! is the "not" in your language - basically toggle the variable between true and false

 

If you go with 0 and 1, you could have a two element array, where array[0]  is /  and array[1] is \

and you can do a single outputstring += array[type_of_slash]  where type_of_slash is 0 or 1

 

you could also simplify the code by replacing that 0 and 1 with detection if the line is odd or not

 

In php (and other languages) you have modulo .. the remainder of a division https://www.php.net/manual/en/language.operators.arithmetic.php

 

So you could do $lineNumber % 2 which will return 0 or 1, if the number is even or odd.

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, camjocotem said:
(...) could probably skip that first loop and just replace $c with $numberOfCharacters xP

Not really. Look closer. It's not the same value.

Link to comment
Share on other sites

Link to post
Share on other sites

 

1 hour ago, homeap5 said:

Not really. Look closer. It's not the same value.

Ah my bad, so your method takes the total number of characters and will output the corresponding of lines for that many characters.

 

@mariushm's code will actually also output the correct result but instead of deriving the output from the total number of characters; it will ask how many lines you would like and derive the output from the result

 

buuuuuuut..

18 hours ago, CreepyPro said:

I need to print this picture and the number of symbols per line in this picture is n.

I've been stuck in this for 2 hours now so any help is aprecciated.

/
\\
///
\\\\
/////
\\\\\\
///////

 @homeap5 is right in saying that the variable n represents the number of symbols per line, but since it's per line and we don't know the exact context of the question

It might be more accurate to say this with @mariushm's example but slightly changed

function drawTextFor(numberOfLines=1) {
  let character = "/";
  let line = "";
  let text = "";
  for (let lineCounter=1;lineCounter<=numberOfLines;lineCounter++) {
      line = "";
      character = (character=="/") ? "\\" : "/"; // flip character at every line jump
      for (let n = 1;n<=lineCounter;n++) { //where n is the number of symbols per line
          line = line + character;
      }
      text = text + ( (text=="") ? "" : "\n") + line;
  }
  return text;
}

console.log(drawTextFor(4));


 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×