c++ - How Do I do a nested for loop for up and down range? -


how make program lets user input data , have program count 1 number input , number have input 1 , has , down this:

it starts off @ 1, goes 1, , 2 (without spaces) on different line, after line goes 1, 2, , 3 without spaces , continues add numbers on each line after until gets number put code, , goes number put in , goes 1 same way counting going down using nested loop.

   #include <iostream> #include <iomanip> using namespace std;    int main() {     int  row, col;     char mych;      cout << "please enter number wish count to: ";     cin >> mych;      for(row = 1; row <= mych; row ++)     {         cout << "\n";          for(col = 1; col <= row; col ++)         {              cout << mych;          }     }      for(row = 1; row >= mych; row --)     {         cout << "\n";          for(col = 1; col >= row; col --)         {              cout << mych;          }       }     cout << "\n\n\n";     system("pause");     return 0; } 

this becomes triangles of fives , want them count whatever user inputs , decrease user's input 1.

here's example output n==12:

1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112 

do notice patterns?

one pattern see there 12 lines or iterations. hmmm, looks first or outer loop goes 1 12 (inclusive).

the other pattern not obvious, let's annotate. number in [] line number, or index in outer for loop:

   [ 1]1     [ 2]12     [ 3]123     [ 4]1234     [ 5]12345     [ 6]123456     [ 7]1234567     [ 8]12345678     [ 9]123456789     [10]12345678910     [11]1234567891011     [12]123456789101112 

hmmm, each index, numbers in each line go 1 index, inclusive.

the first analysis says need for loop. because don't know how prompt user, i'll set fixed number limit. i'm looking for loop ...

ok, let's use variable names row , value:

const unsigned int limit = 12u; unsigned int row; (row = 1; row <= limit; ++row) {   std::cout << "[" << row << "]" << endl; } 

this loop print "line number" within [], in second analysis above.

note { above indicates there 1 or more statements executed each loop in for statement. } indicates end of statements execute in loop.

so instead of printing line number, need print each value. sounds for loop me. comment asks, how make go one. well, that's job of initialization in inner for loop.

const unsigned int limit = 12u; unsigned int row; (row = 1; row <= limit; ++limit) {   // now, brand new loop print values.   (unsigned int value = ???; value <= ???; ++value)   {     std::cout << value;   } } 

so, can initialize value make triangle print correctly?

what value compared signify end of row?

what needs happen after inner for loop return carriage new line , feed lines one?

try using debugger , see happens. you'll surprised!


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -