lundi 23 mars 2020

How can I generate a snake shape with texts in c++?

Let's say, I want to print total m no of lines and the turning point is after every n no. of lines.

w
 w
  w
   w
    w
   w
  w
 w
w
 w
  w
   w
    w
   w
  w
 w
w

The value of m, n, and the text(w) will be taken from the user.

This is what I am done so far:

#include<iostream>
#include<iomanip>
#include <string>
using namespace std;

int main(){

    int m, n;  // total number of lines: m, turning points at interval: n, text: a 
    string a;
    cin>>m>>n>>a;

        int i=1;
    for(int k=1; k<=n/2; k++){
        cout<<setw(i)<<a<<endl;
        i++;
    }
    int j=(n/2)+1;
    for(int k=(n/2)+1; k<=n; k++){
        cout<<setw(j)<<a<<endl;
        j--;
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire