Hello!

Im writing a program that outputs some triangles and such. And I want to count the written *s. If anyone would help i would really appreciate it.

Code:

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

int main()
{
    char znak;
    znak = '*';
    int st_vrst = 0;
    int ploscina_trikotnika, ploscina_kvadrata, obseg_kvadrata, h;
    
    std::cout << "Vnesite število vrstic: ";
    std::cin >> st_vrst;
    std::cin.ignore();
    
    // Trikotnik
    
    for(int i=1; i<=st_vrst; i++)
    {
        int j=0;
        int v=1;
        j=v++;
        h=(j+j)+1;
        std::string str(i, znak);
        std::cout << str << "\n";
    }
    
    cout << h << endl;
    
    std::cout << "\n";
    
    // Kvadrat
    
    for(int i=1; i<=st_vrst; i++) 
    {
        std::string str(st_vrst, znak);
        std::cout << str << "\n";
        ploscina_kvadrata=st_vrst*st_vrst;
    }
    std::cout << ploscina_kvadrata << endl;
    
    std::cout << "\n";
    
    // Trikotnika
    
    for(int i=0;i<st_vrst;i++) {
        for(int j=i;j<st_vrst;j++) 
            printf("*");
        printf("\n");
    }
    for(int i=1; i<=st_vrst; i++) 
    {
        std::string str(i, znak);
        std::cout << str << "\n";
    }
    
    cout << endl;
    
    // Kvadrat
    
    for (int i=1; i<=st_vrst; i++)
        cout << "*";
    cout << endl;
    for (int i=1; i<st_vrst-1; i++)
    {
        cout << "*";
        for (int j=1; j<st_vrst-1; j++)
            cout << ".";
        cout << "*" << endl;
    }
    for (int i=1; i<=st_vrst; i++)
        cout << "*";
    cout << endl;
    
    obseg_kvadrata=st_vrst+(2*st_vrst-2)+(st_vrst-2);
    cout << obseg_kvadrata << endl;
    
    std::cin.get();
    return 0;
}

Output would be:

* 
 ** 
 *** 
 **** 
 ***** 
 ****** 
 21 
 
 ****** 
 ****** 
 ****** 
 ****** 
 ****** 
 ****** 
 36
 
 ****** 
 ***** 
 **** 
 *** 
 ** 
 * 
 * 
 ** 
 *** 
 **** 
 ***** 
 ******
 42
 
 ******
 *....*
 *....*
 *....*
 *....*
 ******
 20

Thanks in advance!

Ive like to count the surface area of the items... like I showed in the output sample.

Thanks

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int counter = 1;
    int left_space = 20;
    int offset = 0;

    cout << setw(20) << '*' << endl;

    for(int i=0; i<10; i++)
    {
         cout << setw(--left_space) << '*' << setw(offset+=2) << '*' << endl;
         counter += 2;
    }

    cout << setw(9);

    for(int i=0; i<23; i++)
    {
         cout << '*';
         counter++;
    }

    cout << "\n\nThis triangle is made of " << counter << " stars. " << endl;

    return 0;
}

This isn't what I need, I need to count stars that are written by function and count them (a.k.a. Surface Area). Thanks anyway.

Edited 5 Years Ago by minghags: n/a

This isn't what I need, I need to count stars that are written by function and count them (a.k.a. Surface Area). Thanks anyway.

It's exactly what you need. As Clinton shows, increment a counter every time you output a *. I leave you to work out the rest of the details.

Or did you want us to finish your code for you?

Edited 5 Years Ago by WaltP: n/a

Sorry if i wasted your time, but it wasn't what i needed, but I figure it out... Thanks anyways.

This article has been dead for over six months. Start a new discussion instead.