Printing a diamond pattern – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#3124 siteicon   siteicon   siteicon  

Printing a diamond pattern

A program that prints a diamond pattern of stars where a size unit is given in form of half of the diamond height.

Learning Objectives

  • Printing a filled diamond pattern using adjustment of pre print spaces first in decreasing order and then in increasing order.

Source Code

TC++ #3124

 

Source Code

Run Output

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

Code Understanding

int n=5; //This would diamond half height upto centre of diamond.
int i,j;    //These are loop index variables
int sp=n-1; //Since first row will have 1 star so spaces would be 1 less.
for(int i=1;i<=n;++i) //Row counting index for upper portion of the diamond
{
for (j = 1; j<=sp; j++) cout<<” “; //Prints spaces before the first star of each row.
sp–;                                                  //Space will have to be reduced for next row.
for (j=1;j<=2*i-1;j++) cout<<“*”; //This prints given number of *
cout<<endl;         //For new line for next row iteration
}
sp=1; //This will have to be set before printing reverse triangle the lower portion of diamond
for(int i=1;i<=n-1;++i)  //Outer loop for lower portion. This time it will be 1 less row as center row is already printed.
{
for (j=1;j<=sp;j++) cout<<” “;
Loop for printing of increasing number of spaces with each iteration
sp++; //Increasing it for next iteration
for (j=1;j<=2*(n-i)-1;j++) cout<<“*”;
Inner loop to print number of * upto half size and less than 1
cout<<endl; //New line for next row
}


Suggested Filename(s): diamond.cpp



Share

sunmitra| Created: 28-Jan-2018 | Updated: 28-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back