JavaScript is required to use Bungie.net

Service Alert
Destiny 2 will be temporarily offline tomorrow for scheduled maintenance. Please stay tuned to @BungieHelp for updates.

OffTopic

Surf a Flood of random discussion.
Edited by Good News: 9/14/2013 6:18:32 AM
7

Programmers of the Flood, I need some help (C++)

I just started learning how to program, using c++, 3 weeks ago, and I have one class a week, so forgive me if this is so basic that asking for help on it makes you want to kill me. I need to write a program using some nested loops to create this: * *** ***** ******* ***** *** * Two problems I have: 1. I don't know how to make the 1-3-5-7-5-3-1. My code goes 1-2-3-4-5-6-7 2. I have no idea how to make it descend once it reached the 7 asterisks. For my loops, I'm trying to use two while loops. One for the ascending part of the triangle, and one for the descending. Both of them have for loops nested inside. [spoiler]#include <iostream> using namespace std; int main() { int counter=1; int x = 0; while(counter <= 7) { for (x=0; x < counter; x++) { cout <<"*"; } cout << endl; counter++; } //descending part while (counter >= 7) { for (x=0; x > counter; x--) { cout<<"*"; } cout << endl; counter--; } return 0; } [/spoiler] So as I said, I don't know how to make the descending part of the triangle. My code is indicative enough of that. Anyone have any advice? For both or either problems?

Posting in language:

 

Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

  • [quote] #include <iostream> #include <iomanip> using namespace std; int main(){ cout.fill('*'); for(int i=1,j=2; i>0; i+=j,j*=((i%7)==0)?-1:1){ cout << setw(i + 1) << '\n'; } return 0; }[/quote]http://ideone.com/yrVrle You can replace the setw call with another loop to output character-by-character if you require the nesting.

    Posting in language:

     

    Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    1 Reply
    • Just set it to wumbo

      Posting in language:

       

      Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    • Edited by Seven: 9/14/2013 2:35:15 PM
      I would have approached the problem differently in the first place. There's whole new code below so don't look at it if you want to fix it yourself. [spoiler]#include <iostream> using namespace std; int main() { int counter=1; bool atSeven=false; while(counter > 0) { if(counter == 7) atSeven=true; for (int x=0; x < counter; x++) { cout <<"*"; } cout << endl; if(!atSeven) counter+=2; else counter-=2; } return 0; } [/spoiler]

      Posting in language:

       

      Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    • Add an if condition for the for loop and endl output that checks if counter is odd. If(counter%2) Do not include counter-- in if block As for the descending part, before running the while loop, decrement counter by 1 otherwise you will get two lines of seven *. In the while loop, use counter>0, then add the if statement to check for odd number. Then. In the for loop use x=counter-1;x>=0;x-- What you r currently doing in the for loop is assignong value 0 to x then decrementing it on every iteration and checking if it is greater than a positive num which will never happen, thus resulting in an infinite loop..

      Posting in language:

       

      Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

      1 Reply
      • Edited by Cast: 9/14/2013 6:54:46 AM
        After the first loop structure, counter is equal to 8. The second while loop loops for while counter is greater or equal to 7. After it gets decremented the loop terminates. Only producing two lines.

        Posting in language:

         

        Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

      • Have you tried Xbox.com?

        Posting in language:

         

        Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

      • bamp

        Posting in language:

         

        Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

      You are not allowed to view this content.
      ;
      preload icon
      preload icon
      preload icon