[Solved] Printing pattern with only one inner loop [closed]


Inner loop ? Why would you need such a thing ? You can do it with a single for loop using std::string::string

void print_stuff(unsigned width){
  for(auto i = 0u ; i < width ; i+=1){
    auto starAmount = width - i;
    auto spaceAmount = i;
    std::cout << std::string{spaceAmount, ' '} << std::string{starAmount, '*'} << '\n';
  }
}

If you really need an inner loop then just replace the use of std::string‘s constructor with a loop :

void print_stuff(unsigned width){
  for(auto i = 0u ; i < width ; i+=1){
    //auto starAmount = width - i;
    auto spaceAmount = i;

    for(auto j = 0u ; j < width ; j+=1)
      std::cout << (j < spaceAmount ? ' ' : '*');
    std::cout << '\n';
  }
}

8

solved Printing pattern with only one inner loop [closed]