Starting with C++

I learnt C language a while ago and few months back I wanted to master C++. The obvious question is “Why?”. I mean since C is already in the bucket I could’ve possibly spent my time on learning another modern high level language, framework and anything other than C++.

“Learning programming languages is NOT learning how to program. Focus on programming techniques, problem solving, and analytical skills, not on learning as many languages as you can.” — Richard Kenneth. While keeping that in mind, let’s dive in.

Let me start with a History lesson. C++ was developed in 1980 by Bjarne Stroustrup. C++ programming is relative of C. Being a direct decedent of C via way of “C with classes” but today both languages have diverged substantially and modern C++ and C codes look considerably different. The scenario I’d like to dive down to is, C tends to be used in very low level projects; embedded firmware or like OS. Where C++ is generally used at the application level on projects where performance is still desired but a higher level abstraction is appreciated (type-based abstraction, Simula-style objects, generic programming) like browsers, game engines, real time applications ( Ex – Tesla auto pilot), professional productivity tools and etc. I believe it’s safe to say that C++ is the middle ground between Performance and ease of programming.

If you’re familiar with C/C++, In this article I’ve listed code snippets of star patterns using C++. If you’re a novice programmer you’ll probably get to practice these patterns to get a good hands about logical thinking and program flow control. If you’re looking for more complex problems visit,

  1. Project Euler
  2. Hacker Rank

Notable mentions :

 


 

Lets start with Star Patterns,

Pattern – 1

*
**
***
****
*****

#include<iostream>
using namespace std;

int main()
{
    int i, j;
    for(i=1;i<=5;i++){
        for(j=1;j<=i;j++){          
           cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

 

Pattern – 2

        *
**
***
****
*****

#include<iostream>
using namespace std;

int main()
{
    int i, j, k;
    for(i=5;i>=1;i--){
        for(j=1;j<i;j++){          
           cout << " ";
        }
        for(k=5;k>=i;k--){
           cout << "*";
        }
        cout << endl;
    }
    return 0;
}

 

Pattern – 3

*
***
*****
*******
*********

#include<iostream>
using namespace std;

int main()
{
    int i, j, k;
    for(i=1;i<=5;i++){
        for(j=i;j<5;j++){
           cout << " ";
        }
        for(k=1;k<(i*2);k++){
           cout << "*";
        }
        cout <<endl;
     } 
     return 0; 
}

 

Pattern – 4

*
**
***
****
*****
****
***
**
*

 

#include<iostream>
using namespace std;

int main()
{
    int i, j;
    for(i=1; i<=5; i++){
        for(j=1; j<=i; j++){
          cout<< "*";            
        }
        cout<<endl;
    }
    for(i=5; i>=1; i--){
        for(j=1; j<i; j++){
          cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

 

I did leave out few patterns. I’m confident that if you manage to grab the concept of For loop through solving above or solving any complications you had, other patterns would be nothing but a piece of cake 🙂 If you want to master C++ here is a tip, there is no ending to master that, there is always something new; like any other good programming language I guess. Good Luck!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s