Home > Blockchain >  nested looping C
nested looping C

Time:12-03

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.

CodePudding user response:

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;
  }
}

CodePudding user response:

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.

CodePudding user response:

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;)...

  • Related