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!

Recommended Answers

All 7 Replies

like, what do ye' need help with dude¿

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.

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?

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

OK then, what was it you needed?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.