Sunday, September 30, 2012

Star Structure

Q. Write a c program to print the following star structure:

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

Ans.
/*c program for print the star structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 printf("\n");
 n=num;
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<=r; sp++)
    printf(" ");
  for(c=1; c<=n; c++)
    printf("*");
  for(c=num-r; c>=1; c--)
    printf("*");
  n--;
  printf("\n");
 }
 for(r=2; r<=num; r++)
 {
  for(sp=num-r+1; sp>=1; sp--)
    printf(" ");
  for(c=1; c<=r; c++)
    printf("*");
  for(c=r-1; c>=1; c--)
    printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of  specific Star Structure C program
Screen shot for specific star structure C program

Monday, September 24, 2012

Star triple pyramid

Q. Write a C program to print the following star triangle:

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

Ans.

Star pyramid program make 3 module program
Screen shot of solutions for star pyramid
breaks in module sub programs


/*c program for star pyramid or star triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,n=4;
 for(r=1;r<=2; r++)
 {
  for(sp=1; sp<=n; sp++)
    printf(" ");
  for(c=1; c<=r; c++)
  {
    printf("*");
    printf(" ");
  }
  printf("\n");
  n=n-1;
 }

 for(r=1;r<=3; r++)
 {
  for(sp=0; sp<=n+1; sp++)
    printf(" ");
  for(c=1; c<=r; c++)
  {
    printf("*");
    printf(" ");
  }
  printf("\n");
  n=n-1;
 }

 for(r=1;r<=4; r++)
 {
  for(sp=0; sp<=n+4; sp++)
    printf(" ");
  for(c=1; c<=r; c++)
  {
    printf("*");
    printf(" ");
  }
  printf("\n");
  n=n-1;
 }
 getch();
 return 0;
}

The output of above program would be:

Output of star pyramid C program
Screen shot for star pyramid C program


Monday, September 17, 2012

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 45
 345
 2345
 12345

Ans.

/*c program for number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,num,n,i;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 n = num;
 for(r=1; r<=num; r++,n--)
 {
  for(i=n; i<=num; i++)
     printf("%d", i);
  printf("\n");
 }
 getch();
 return 0;
}


The output of above program would be:


Output of number pyramid C program
Screen shot for number pyramid C program

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 454
 34543
 23454321
 123454321

Ans.

/*c program for number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,num,n,i,j;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 n = num;
 for(r=1; r<=num; r++,n--)
 {
  for(i=n; i<=num; i++)
    printf("%d", i);
  for(j=num-1; j>=n; j--)
    printf("%d", j);
  printf("\n");
 }
 getch();
 return 0;
}


The output of above program would be:


Output of number triangle C program
Screen shot for number triangle C program

Number Triangle


Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 9
 898
 78987
 6789876

Ans.

/*c program for number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,num=9,n,i,j;
 n = num;
 for(r=1; r<=4; r++,n--)
 {
  for(i=n; i<=num; i++)
    printf("%d", i);
  for(j=num-1; j>=n; j--)
    printf("%d", j);
  printf("\n");
 }
 getch();
 return 0;
}


The output of above program would be:

Output of specific number pyramid C program
Screen shot for specific number pyramid C program

Friday, September 14, 2012

Short-Circuit operators

C language has two types short-circuit operators:

  1. Logical AND operator  &&
  2. Logical OR operator  ||
What is meaning of short-circuit


In C language, the meaning of short-circuit is that if there are any expression, then output of expression must be determined by the left side operand, and right side operand never be executed.
Here two case occurred,:
what would be output if left side operand is true?
what would be output if left side operand is false?

keep in mind: 0 or any negative value is always false.
                    1 or any non-zero value is always true.


Let's understand short-circuit operators by example:

x && y

hear, if left side operand x is false then overall expression to be false, and the right side operand y never evaluated.
If left side operand x is true then result of expression depend upon right side  operand y and in this case right side  operand will be evaluated.

m || n

here, if left side operand m is true then overall expression to be true, and right side operand n not to be evaluated.

Read following programs carefully:

/*c program for logical OR short-circuit operator example*/
#include<stdio.h>
int main()
{
 int x, y , z;
 x = y = z = 1;

 ++x || ++y;

 printf("\n x=%d\t y=%d\t z=%d \n", x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=2  y=1  z=1
explanations: In above program logical OR short-circuit operator, in expression left hand side operand x is true so its increase ++x i.e. x=2. And right side operand y not to be evaluated.

-------------------------------------------------------------------

/*c program for exercise of logical OR short-circuit operator operators*/


#include<stdio.h>
int main()
{
 int x, y , z;

 x = y = z = 0;

 ++x || ++y;

 printf("\n x=%d\t y=%d\t z=%d \n", x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=1  y=0  z=0
explanations: In above program logical OR short-circuit operator, in expression left hand side operand x is false so right side operand y not to be evaluated.


-------------------------------------------------------------------

/*c program for exercise of logical AND short-circuit operators example*/


#include<stdio.h>
int main()
{
 int x, y , z;

 x = y = z = 1;

 ++x && ++y;

 printf("\n x=%d\t y=%d\t z=%d \n",x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=2  y=2  z=1
explanations: In above program logical AND short-circuit operator, in expression left hand side operand x is true(++x) so right side operand y also evaluated(++y).


-------------------------------------------------------------------

/*c program for logical AND, logical OR short-circuit operators example*/

#include<stdio.h>
int main()
{
 int x, y , z, r;


 x = y = z = 1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x && ++y || ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x || ++y || ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x && ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);



 x = y = z = -1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x && ++y ||  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x ||  ++y ||  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x &&  ++y &&  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x || ++y &&  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);

 getch();
 return 0;
}

The output of above program would be:


Output of short circuit operators C program
Screen shot for logical AND, logical OR
short circuit operator C program


Wednesday, September 12, 2012

Heap Sorting method and algorithm

In heap sorting process:

First of all organizing the whole data to be sorted as a binary tree i.e. heap. And after that impletment heap sorting.

Step 1: (How to data organizing as binary tree?)

Remember only 2 rule:

Rule:1.

The parent node must be greater then child node.
If parent node is not greater then to child node not replace it with parent node. And if binary tree is large then, if relaceing child node(now its parent node) is greater then to great-parent node then its also is replace the great-parent node.

Rule:2.

New element always insert in left side and after right side of parent node.
If left side of parent element has already element then new element would be insert in right side. And if right side of parent node has already element then new element will be insert in left side.

Step 2: (How to perform heap sorting:)

Remove the topmost element(largest element) and replace it with the rightmost element.

Step 3:

Repeat steps 1 and 2 untill all elements is not sorted.



Heap Sorting method/process by example:

max heap : 80 , 32 , 31 , 110 , 50 , 40 , 120 

Step1:
                  80

Step2:  
                 80
              /      \
           32        31

Step3:  
                 80                           80                         110
              /       \                      /     \                     /       \
           32         31    ==>     110       31     ==>    80         31
          /                             /                            /
     110                           32                          32


Step4:
                 110
               /      \
            80        31
          /    \        
      32       5


Step5:
                  110                               110
                /       \                          /      \
             80         31    ==>          80        40
           /    \       /                     /   \       / 
        32      50   40                32      50   31



        
Step6:
                 110                               110                                120
               /       \                          /        \                           /       \
           80         40       ==>        80          120      ==>        80         110
         /     \      /    \                /    \        /    \                 /    \      /    \
     32       50   31   120         32     50    31     40           32     50  31       40


-------------------------------------------------------------------------------------

(1)
                  120 [1]
                 
              /                 \

          80 [2]               110 [3]

      /         \              /           \

 32 [4]       50 [5]     31 [6]         40 [7]

// [1],[2],[3],[4],[5],[6],[7]: Index number 
// Now applying step 2(perform heap sorting)
// Replace the topmost element with the rightmost element.


(2)
             40                                     110      
           /      \             Heaping        /       \
        80       110        =====>     80          40
      /   \       /    \                     /    \       /    \
   32     50   31    120              32     50   31    120

(3)
//now replace 110 with rightmost element i.e. 31 because 120 had been sorted.

            31                                        80                                 80
         /       \           Heaping             /       \        Heaping         /      \
       80        40        ======>        31         40      ======>    50       40
     /    \     /    \                         /     \      /    \                   /    \     /   \
   32   50  110   120                  32     50   110    120           32    31  110  120

(4)
//now replace 80 with rightmost element i.e. 31 because 110, 120 had been sorted.

           31                                           50                                    50
         /      \             Heaping              /        \           Heaping         /     \
       50       40          ======>         31          40        ======>    32      40
      /    \      /   \                           /     \       /     \                   /    \     /     \
   32     80  110  120                    32     80     110    120           31     80  110   120

(5)
//now replace 50 with rightmost element i.e. 31 because 80, 110, 120 had been sorted. 

              31                                      40

           /        \            Heaping         /       \
       32           40        ======>    32         31
     /     \        /    \                     /    \     /     \
   50     80    110    120             50     80  110    120

(6)
//now replace 40 with rightmost element i.e. 31 because 50, 80, 110, 120 had been sorted. 


              31                                     32
            /     \           Heaping            /     \
         32      40        ======>        31       40
       /    \     /    \                       /    \     /    \
    50   80   110   120               50    80   110    120

(7)
//now replace 32 with rightmost element i.e. 31 because 40, 50, 80, 110, 120 had been sorted. 

                   31
                /       \
            32        40
         /     \       /      \
     50     80   110    120

Now all elements are sorted: 31 , 32 , 40 , 50 , 80 , 110 , 120


Related programs:

  1. Merge sorting
  2. Heap sorting
  3. Bubble sorting
  4. Selection Sorting
  5. Insertion sorting
  6. Insertion sorting using function
  7. Shell sorting
  8. Quick sorting
  9. Radix sorting
  10. Liner sorting

Tuesday, September 11, 2012

Pointer Extend Example

Can a pointer variable, itself might be another pointer?
Yes, this pointer would be contains another pointer's address.

The following example/program should should make this point clear:

/*c program for pointer extend example*/

#include<stdio.h>
int main()
{
 int x=5;
 int *y, **z;

 y = &x;
 z = &y;

 printf("\nAddress of x = %u", &x);
 printf("\nAddress of x = %u", y);
 printf("\nAddress of x = %u", *z);
 printf("\nAddress of y = %u", &y);
 printf("\nAddress of y = %u", z);
 printf("\nAddress of z = %u", &z);
 printf("\nValue of y = %u", y);
 printf("\nValue of z = %u", z);
 printf("\nValue of x = %u", x);
 printf("\nValue of x = %u", *(&x));
 printf("\nValue of x = %u", *y);
 printf("\nValue of x = %u", **z);

 getch();
 return 0;
}

The output of the above program would be:


Output of pointer extend example C program
Screen shot for pointer extend example C program


Read above program carefully, the addresses that get output might be something different.
The relationship between x, y and z can be easily summarized as:

Name ----------------       x                    y               z

Value -------------------    5               2293620       2293616

Address ---------------- 2293620        2293616       2293612


What is the meaning of int x, *y, **z;

Here, x is an ordinary int variable,
y is a pointer to an int i.e. integer pointer,
z is pointer to an integer pointer.

We can extend the above program still further by creating a pointer to a pointer to an integer pointer i.e.  ***n
There is no limit on how far can we go on extending this definition. Possibly, till the point we can comprehend it. And that point of comprehension is usually a pointer to a pointer.


Related programs:


  1. What is Pointer?
  2. Concept of Pointer
  3. Pointer basic example

Monday, September 10, 2012

Function Key Shortcuts

There are 12 function keys in the keyboard. These function keys meaning, combine with other key shortcuts are as following:

F1
Display the Help.

F2
Rename the selected item.

F3
Search for file or folder

F4
Display the address bar list in Windows Explorer

F5
Refresh the active window

F6
Go to the address bar in browser

F7
spell and grammar check in a document in MS programs.

F8
When system is going to start, and if you press F8 then, it will open in safe mode i.e. access Windows in safe mode

F9
Normally function key F9 not defined any work in Windows. But its can be perform task such as controlling BIOS setup programs, switching display unit etc.

F10
Activate the menu bar in the active program

F11
Go to full Screen in browser

F12
Dock/Undock into Separate Window

Alt + F4
Exit the active window

Ctrl + F4
Close the active window

Shift + F10
Display the right click menu for the selected menu



Related programs:

  1. Run command shortcuts
  2. General keyboard shortcuts
  3. Windows keyboard shortcuts
  4. Dialog box shortcuts
  5. Make your own Windows Run command

Dialog Box Shortcuts


The below are the shortcuts which are used geneally in as form, save, open etc.

Enter
Perform the equivalent to hitting the OK button

Space
Change the state of Check box or radio button.

Tab
Move forward through options

Shfit + Tab
Move back through options

Ctrl + Tab
Move forward through options

Ctrl + Shift + Tab
Move back through tabs

Arrow Keys
Select a button if the active option is a group of option buttons

Backspace
Open a folder one level up if a folder is selected in the Save As open dialog box



Related programs:

  1. Run command shortcuts
  2. General keyboard shortcuts
  3. Make your own Windows Run command
  4. Windows keyboard shortcuts
  5. Function key shortcuts