Saturday 8 March 2014

C++ program samples


download all and run all samples from here ......!!




C++ hollow diamond program


#include<iostream>
#include<conio.h>
using namespace std;

void main()
{
int Pheigt;
cout<<"Enter the height of paradigm:";
cin>>Pheigt;

int const margin_view = 20; //no need for constant it
int count;
int spaces_print;
int *ptr;

//**********************ROWS PRINT***************************************//
for(int i=0;i<=Pheigt;i++)
{

int spaces_to_print = margin_view +Pheigt - i; //*****************************//SETTING HOW MUCH SPACES TO PRINT

//cout<<spaces_to_print;
for(count = 0 ; count <= spaces_to_print; count++)
{
cout<<" ";
}

for(count = 0; count<=i*2; count++ ) //**********************PRINT STARS**************************//
{

if(count==0 || count==i*2)
{
cout<<"*";}
else{cout<<" ";}

}
ptr = &spaces_to_print;
//cout<<*ptr;
// cout<<spaces_to_print;
cout<<endl;
}

// cout<<*ptr;
int  margins_view = *ptr; //current spaces store to new variable

//*************************Row Loop***********************************************//
for(int i=Pheigt;i>=0;i--)
{
spaces_print = margins_view + Pheigt - i;
// cout<<spaces_print;

for(count = spaces_print ; count >=0; count --)
{

cout<<" ";
}

for(count=i*2;count>=0;count--)
{


if(count==0 || count==i*2)
{
cout<<"*";}
else{cout<<" ";}

//cout<<"*";
}

//spaces
cout<<endl;
}


_getch();
}


Enter the height of paradigm:7
                            *
                           * *
                          *   *
                         *     *
                        *       *
                       *         *
                      *           *
                     *             *
                     *             *
                      *           *
                       *         *
                        *       *
                         *     *
                          *   *
                           * *
                            *

C++ DIAMOND PROGRAM EASY APPROACH WITH FULL DESCRIPTION

QUESTIONS IN MIND

How much height you want to draw diamond?

Want to draw in Center of screen to show beauty?
if you dont pass spaces loop what happens?
why Nested Loop is important in this program?


Many questions are produced in mind of new programmers I have solution

height is set because program does not know what to draw you have to pass the height of above and below pyramid that program can knew that height is etc height = 10; then it has to use logical height 10 times.

why nested loop ?
Always Remember C++ compiler only draw in form of rows and column a loop

nested means loop under a loop
ROW(PROGRAMMING){
_________COL(PROGRAMMING)
}

first loop is for rows (------------------------------)to print

 2nd loop is for the column to print
(
|
|
|
)


now how to draw above and below pyramid??
Usually most of the time teachers and colleagues don't tell you what is logic to print it in easy way, i am experienced  person so i will help you guys

1)ROWS
first of all you have to print pass the loop to compiler C++ which starts from 0 and ends to height you know
in which it now needs to know in a line single or line by line
you have to use \n or endl to print row by row a (loop prints row)


2) COLUMNS

As you see pyramid
                            *
                          ***
                         *****

you notices their are spaces and margin in center and printing '*' in both sides

how
you have to define integer variable to set margin = for example 10
know

you have to store spaces for rows you see spaces are row by row reduced to help static print at left

int spaces = margin_you have given + height you have give -  rows loop current value
your spaces for current loop row will be set

you have to print spaces for this row
until this row spaces value reached
_______________________________

you above see line it shows space of current row


Now you have to print *
use a loop goes to current row*2 value of loop and print *
initally 0 then one star will be print
now
you se
_______________________________*

loop continues
row incremented
colomn space decremented
stars in row incremented

___________________________*
__________________________***
_________________________*****
________________________*******
_______________________*********
______________________***********
till height is given


now reverse do process for print in reverse order how?

______________________***********
_______________________*********
________________________*******
_________________________*****
__________________________***
___________________________*



first of all samely you have to pass row for loop then space for loop in between row loop same as stars loop
how you pass the space margin to other row for loop you have to use pointer for which you can get the margin space total gone and last time used for upper paramid now you can by using pointer save in it last value of margin space of pointer and pass to lower margin space pointer so which from you for loop row starts and ends to 0 to print in reverse order

in upper row for loop pass this code
ptr = &spaces_to_print;


it can saves last pointer then

after end of loop declare another space margin variable and pass this pointer to it and start the loop from it and till ends to 0

int  margins_view = *ptr;

for(int i=Pheigt;i>=0;i--)
{
// your code
}

same above procedure but in reverse

spaces_print = margins_view + Pheigt - i;


and you have got upper and lower paramid form diamond i hope you will understand my code


  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

  4. void main()
  5. {
  6. int Pheigt;
  7. cout<<"Enter the height of paradigm:";
  8. cin>>Pheigt;

  9. int const margin_view = 20; //no need for constant it
  10. int count;
  11. int spaces_print;
  12. int *ptr;

  13. //**********************ROWS PRINT***************************************//
  14. for(int i=0;i<=Pheigt;i++)
  15. {

  16. int spaces_to_print = margin_view +Pheigt - i; //*****************************//SETTING HOW MUCH SPACES TO PRINT

  17. //cout<<spaces_to_print;
  18. for(count = 0 ; count <= spaces_to_print; count++)
  19. {
  20. cout<<" ";
  21. }

  22. for(count = 0; count<=i*2; count++ ) //**********************PRINT STARS**************************//
  23. {
  24. cout<<"*";
  25. }
  26. ptr = &spaces_to_print;
  27. //cout<<*ptr;
  28. // cout<<spaces_to_print;
  29. cout<<endl;
  30. }

  31. // cout<<*ptr;
  32. int  margins_view = *ptr;
  33. for(int i=Pheigt;i>=0;i--)
  34. {
  35. spaces_print = margins_view + Pheigt - i;
  36. // cout<<spaces_print;
  37. for(count = spaces_print ; count >=0; count --)
  38. {
  39. cout<<" ";
  40. }

  41. for(count=i*2;count>=0;count--)
  42. {
  43. cout<<"*";
  44. }
  45. //spaces
  46. cout<<endl;
  47. }


  48. _getch();
  49. }



Thursday 6 March 2014

C++ FULL PARAMID PROGRAM EASIEST APPROACH

QUESTIONS IN MIND

How much height you want to draw paramid?

Want to draw in Center of screen to show beauty?
if you dont pass spaces loop what happens?
why Nested Loop is important in this program?


Many questions are produced in mind of new programmers I have solution

height is set because program does not know what to draw you have to pass the height of pyramid that program can knew that height is etc height = 10; then it has to use logical height 10 times.

why nested loop ?
Always Remember C++ compiler only draw in form of rows and column a loop

nested means loop under a loop
ROW(PROGRAMMING){
_________COL(PROGRAMMING)
}

first loop is for rows (------------------------------)to print

 2nd loop is for the column to print
(
|
|
|
)


now how to draw pyramid??
Usually most of the time teachers and colleagues don't tell you what is logic to print it in easy way, i am experienced  person so i will help you guys

1)ROWS
first of all you have to print pass the loop to compiler C++ which starts from 0 and ends to height you know
in which it now needs to know in a line single or line by line
you have to use \n or endl to print row by row a (loop prints row)


2) COLUMNS

As you see pyramid
                            *
                          ***
                         *****

you notices their are spaces and margin in center and printing '*' in both sides

how
you have to define integer variable to set margin = for example 10
know

you have to store spaces for rows you see spaces are row by row reduced to help static print at left

int spaces = margin_you have given + height you have give -  rows loop current value
your spaces for current loop row will be set

you have to print spaces for this row
until this row spaces value reached
_______________________________

you above see line it shows space of current row


Now you have to print *
use a loop goes to current row*2 value of loop and print *
initally 0 then one star will be print
now
you se
_______________________________*

loop continues
row incremented
colomn space decremented
stars in row incremented

___________________________*
__________________________***
_________________________*****
________________________*******
_______________________*********
______________________***********
till height is given


//CODE

#include<iostream>
#include<conio.h>
using namespace std;

void main()
{
int Pheigt;
cout<<"Enter the height of paradigm:";
cin>>Pheigt;

int margin_view = 20; //no need for constant it
int count;


//**********************ROWS PRINT***************************************//
for(int i=0;i<=Pheigt;i++)
{

int spaces_to_print = margin_view +Pheigt - i; //*****************************//SETTING HOW MUCH SPACES TO PRINT


for(count = 0 ; count <= spaces_to_print; count++)
{
cout<<" ";
}

for(count = 0; count<=i*2; count++ ) //**********************PRINT STARS**************************//
{
cout<<"*";
}
// cout<<spaces_to_print;
cout<<endl;
}

_getch();
}