0

i want to asking this problem. this output is the expected output

*
*#
*#%
*#%*
*#%*#
*#%*#%

and this is my solution

#include <iostream>

using namespace std;


int main(){

  int a,b,n;

  cout << "Input the row";
  cin >> n;


  for (a = 1; a <= n; a++){
    for(b = 1; b <= a; b++){
        if (b == 1 || b == 1 + 3){
            cout << "*";
        }
        if (b ==2 || b == 2 + 3){
            cout << "#";
        }
        if (b ==3 || b == 3 + 3){
            cout << "%";
        }
    }
    cout << endl;
  }
}

this solution is only work if the n = 6. what should i do if i want this work in every row when user input the row to the n thank you in advance.

  • 1
    If you divide `b` by three and get the *remainder* (which is easily done with the remainder operator `%`) you will get a value between `0` and `2` (inclusive). You could the result of `b % 3` to decide which characters to print. It becomes more natural if you start the loops at *zero* instead. – Some programmer dude Dec 03 '22 at 08:46
  • i have try to use modulo operator but when i use % i try to print # when the number is odd and when the number is even i try to cout * but i dont know how to print % – Arrrriiiii Dec 03 '22 at 08:46
  • sorry sir if you dont mind, can you remake it with my code above, thank you. – Arrrriiiii Dec 03 '22 at 08:49
  • Side note: About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Dec 03 '22 at 09:22

3 Answers3

0

To make your solution work for any value of n, you can use the modulo operator % to check whether a given value of b is the first, second, or third element of each row.

Here is one way you could modify your code to do this:

#include <iostream>

using namespace std;

int main() {
  int a, b, n;

  cout << "Input the row: ";
  cin >> n;

  for (a = 1; a <= n; a++) {
    for (b = 1; b <= a; b++) {
      // Use the modulo operator to check whether b is the first, second, or third element of each row
      if (b % 3 == 1) {
        cout << "*";
      } else {
        if (b % 3 == 2) {
          cout << "#";
        } else {
          cout << "%";
        }
      }
    }
    cout << endl;
  }

  return 0;
}

With this change, the code will output the correct pattern for any value of n.

Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
0

Here, I tried using the modulo "%" on your if's

#include <iostream>

using namespace std;


int main(){

  int a,b,n;

  cout << "Input the row";
  cin >> n;


  for (a = 1; a <= n; a++){
    for(b = 1; b <= a; b++){
        // After every first digits will cout #
        if (b % 3 == 2){
            cout << "#";
        }
        // The first after the third digit will cout *
        if (b % 3 == 1){
            cout << "*";
        }
        // The third digit after the second digit will cout % 
        if (b % 3 == 0){
            cout << "%";
        }
    }
    cout << endl;
  }
}
DenjanD
  • 54
  • 5
  • 1. if one of these if's catches, the others can't any more, so you should introduce an `else` for every succeeding case. Even simpler: use `switch/case` instead. My personal recommendation, though: `std::cout << "%*#"[b%3];` – much simpler and even more efficient ;) – Aconcagua Dec 03 '22 at 09:11
0

Just adding a nice optimisation (note: C++ loops naturally go up from 0 to not including n, i.e. for(int i = 0; i < n; ++i) – this is especially relevant if you are indexing arrays which have a first index of 0 and last of n - 1, while n already is invalid!).

While you do use b % 3 to decide which character and you indeed can use this by chaining if(){} else if(){} else{} (where a switch() { case: case: default: } actually would have been preferrable) you can have a much more compact version as follows (and even more efficient as it avoids conditional branching):

for(int b = 0; b < a; ++b)
{
    std::cout << "*#%"[b % 3];
}

The C-string literal "*#%" actually represents an array of char with length four (including the terminating null character) – and you can index it just like any other array you have explicitly defined (like int n[SOME_LIMIT]; n[7] = 1210;)...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59