Home > Enterprise >  How would i make a pascal triangle that keeps adding another star until it hits the end limit
How would i make a pascal triangle that keeps adding another star until it hits the end limit

Time:02-25

so I have 3 inputs a,b,c

ex 4, 7, 1

    
     
      
       

its supposed to look like this, it starts at 4 stars and keeps adding another star c times until it reaches 7 how would I code this Im very confused this is what I have so far

#include <iostream>
#include <iomanip>

int main(){
    int r,a,b,c;
    std::cin>> a;
    std::cin>>b;
    std::cin>>c;
    for(int i = a; i <= b; i  ) {
        for(int j = 1; j <=i; j  ){
            std::cout << "*";
        }
        std::cout << "\n" ;
    }

}
    

CodePudding user response:

While a is smaller than b, print "*" a times, then increment by c (step) for next iteration.

int main(){
    int a,b,c;
    std::cin>> a;
    std::cin>>b;
    std::cin>>c;
 while(a < b){
    for (int i =0; i < a;i  ){
        cout << "*";
    }
    cout << endl;
    a =c;
    }
    return 0;
}

CodePudding user response:

Last time I remembered doing something like this, I used a vector. It helps later in many ways, because it stores the output. So, here's my code:

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

void print(const std::vector<char>& vecToPrint) {
    std::copy(vecToPrint.begin(), vecToPrint.end(), std::ostream_iterator<char>(std::cout, " "));
}

int main() {
    int num1 = 4, num2 = 7, num3 = 1;
    std::vector<char> vecOfStars(num1, '*');
    for(int i = num1; i <= num2; i  = num3) {
        print(vecOfStars);
        std::cout << "\n";
        vecOfStars.push_back('*');
    }
    return 0;
}

Check it out here

  • Related