Image | Description |
---|---|
AD and CE are the shortest arcs, with length 5, and AD has been arbitrarily chosen, so it is highlighted. | |
CE is now the shortest arc that does not form a cycle, with length 5, so it is highlighted as the second arc. | |
The next arc, DF with length 6, is highlighted using much the same method. | |
The next-shortest arcs are AB and BE, both with length 7. AB is chosen arbitrarily, and is highlighted. The arc BD has been highlighted in red, because there already exists a path (in green) between B and D, so it would form a cycle (ABD) if it were chosen. | |
The process continues to highlight the next-smallest arc, BE with length 7. Many more arcs are highlighted in red at this stage: BC because it would form the loop BCE, DE because it would form the loop DEBA, and FE because it would form FEBAD. | |
Finally, the process finishes with the arc EG of length 9, and the minimum spanning tree is found. |
404
Page not found.
Kruskal’s algorithm : Reference
Bitwise Left Shift (<<) Operator in C Programming
Bitwise Left Shift Operator in C
- It is denoted by <<
- Bit Pattern of the data can be shifted by specified number of Positions to Left
- When Data is Shifted Left , trailing zero’s are filled with zero.
- Left shift Operator is Binary Operator [Bi - two]
- Binary means , Operator that require two arguments
Quick Overview of Left Shift Operator
Original Number A | 0000 0000 0011 1100 |
Left Shift | 0000 0000 1111 0000 |
Trailing Zero’s | Replaced by 0 (Shown in RED) |
Direction of Movement of Data | <<<<<=======Left |
Syntax : Bitwise Left Shift Operator
[variable]<<[number of places]
Live Example : Bitwise Operator [Left Shift Operator]
#include<stdio.h> int main() { int a = 60; printf("\nNumber is Shifted By 1 Bit : %d",a << 1); printf("\nNumber is Shifted By 2 Bits : %d",a << 2); printf("\nNumber is Shifted By 3 Bits : %d",a << 3); return(0); }
Output :
Number is Shifted By 1 Bit : 120 Number is Shifted By 2 Bits : 240 Number is Shifted By 3 Bits : 480
Explanation of Left Shift Binary Operator :
We know the binary representation of the 60 is as below -
0000 0000 0011 1100
Now after shifting all the bits to left towards MSB we will get following bit pattern -
0000 0000 0011 1100 = 60 << 1 ------------------- 0000 0000 0111 1000 = 120
Observations and Conclusion :
Shfting Binary 1 to Left By ‘X’ Bits | Decimal Value | Converted Value |
---|---|---|
0 Bit | 2^0 | 1 |
1 Bit | 2^1 | 2 |
2 Bits | 2^2 | 4 |
3 Bits | 2^3 | 8 |
4 Bits | 2^4 | 16 |
5 Bits | 2^5 | 32 |
6 Bits | 2^6 | 64 |
7 Bits | 2^7 | 128 |
8 Bits | 2^8 | 256 |
What is meaning of (++*ptr) in Pointer ?
In the previous topic we have studied Precedence of Value at and Address Operator of Pointer. In this chapter we will be looking more advance form of pointer. We are looking one step forward to learn how (++*ptr) works ?
Meaning of (++*ptr) in C Programming :
Consider the Following Example :
int num = 20 , *ptr ; ptr = # printf("%d",++*ptr);
Explanation :
- ‘++’ and ‘*’ both have Equal Precedence
- Associativity is from Right to Left ( Expression evaluated from R->L)
- Both are Unary (Operates on single operand )
- So ‘ * ‘ is Performed First then ‘ ++ ‘
Re-Commanded Reference Article : Operator Precedence Table
Visual Understanding :
Calculation of Answer :
++*ptr = ++ ( *ptr ) = ++ ( *3058 ) = ++ ( 20 ) = 21
- Suppose ptr variable stores address 3058.
- * Operator will de-reference pointer to get value stored at that address. (i.e 20)
- Pre-increment on 20 will perform increment first then assignment
Live Example : Meaning of ++*ptr Pointer
#include<stdio.h> int main() { int n = 20 , *ptr ; ptr = &n; printf("%d",++*ptr); return(0); }
Output :
21
In the next chapter we will be learning the meaning of (*++ptr) Pointer.
What is the size of Pointer variable in C ?
In the previous chapter, we have learnt about the memory organization of the pointer variable. In this chapter we are learning to compute or find the size of pointer variable.
What is the size of Pointer variable in C ?
How to calculate size of Pointer ?
Consider the following memory map before declaring the variable.In the memory comprise of blocks of 1 byte.
Whenever we declare any variable then random block of memory is chosen and value will be stored at that memory location.
Similarly whenever we declare any pointer variable then random block of memory will be used as pointer variable which can hold the address of another variable.
Calculating Size of Pointer : Tips
- Pointer stores the address of the Variable.
- Size of Pointer Variable can be evaluated by using sizeof operator
- Address of variable is considered as integer value.
- For Borland C/C++ Compiler, for storing integer value 2 bytes are required.
- Thus Pointer variable requires 2 bytes of memory.
Size of Integer Pointer Variable in Different IDE :
Compiler | Size of Integer | Size of Integer Pointer |
---|---|---|
Borland C/C++ | 2 Bytes | 2 Bytes |
Turbo C/C++ | 2 Bytes | 2 Bytes |
Visual C++ | 4 Bytes | 4 bytes |
Size of the Pointer :
#include<stdio.h> int main() { int num = 15; int *ptr = NULL; ptr = # printf("Size of Pointer : %d Bytes",sizeof(ptr)); return 0; }
Output :
Size of Pointer : 2 Bytes
Some of the Useful Programs :
Application of Array in C Programming
Application Of Array :
Array is used for different verities of applications. Array is used to store the data or values of same data type. Below are the some of the applications of array -
A. Stores Elements of Same Data Type
Array is used to store the number of elements belonging to same data type.
int arr[30];
Above array is used to store the integer numbers in an array.
arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;
Similarly if we declare the character array then it can hold only character. So in short character array can store character variables while floating array stores only floating numbers.
B. Array Used for Maintaining multiple variable names using single name
Suppose we need to store 5 roll numbers of students then without declaration of array we need to declare following -
int roll1,roll2,roll3,roll4,roll5;
- Now in order to get roll number of first student we need to access roll1.
- Guess if we need to store roll numbers of 100 students then what will be the procedure.
- Maintaining all the variables and remembering all these things is very difficult.
Consider the Array :
int roll[5];
So we are using array which can store multiple values and we have to remember just single variable name.
C. Array Can be Used for Sorting Elements
We can store elements to be sorted in an array and then by using different sorting technique we can sort the elements.
Different Sorting Techniques are :
- Bubble Sort
- Insertion Sort
- Selection Sort
- Bucket Sort
D. Array Can Perform Matrix Operation
Matrix operations can be performed using the array.We can use 2-D array to store the matrix.
E. Array Can be Used in CPU Scheduling
CPU Scheduling is generally managed by Queue. Queue can be managed and implemented using the array. Array may be allocated dynamically i.e at run time. [Animation will Explain more about Round Robin Scheduling Algorithm | Video Animation]
F. Array Can be Used in Recursive Function
When the function calls another function or the same function again then the current values are stores onto the stack and those values will be retrieve when control comes back. This is similar operation like stack.
if-else-if Statement OR else if Ladder : Tutorial on If
if-else-if Statement OR else if Ladder :
Suppose we need to specify multiple conditions then we need to use multiple if statements like this -
void main ( ) { int num = 10 ; if ( num > 0 ) printf ("\n Number is Positive"); if ( num < 0 ) printf ("\n Number is Negative"); if ( num == 0 ) printf ("\n Number is Zero"); }
which is not a right or feasible way to write program. Instead of this above syntax we use if-else-if statement.
Consider the Following Program -
void main ( ) { int num = 10 ; if ( num > 0 ) printf ("\n Number is Positive"); else if ( num < 0 ) printf ("\n Number is Negative"); else printf ("\n Number is Zero"); }
Explanation :
In the above program firstly condition is tested i.e number is checked with zero. If it is greater than zero then only first if statement will be executed and after the execution of if statement control will be outside the complete if-else statement.
else if ( num < 0 ) printf ("\n Number is Negative");
Suppose in first if condition is false then the second condition will be tested i.e if number is not positive then and then only it is tested for the negative.
else printf ("\n Number is Zero");
and if number is neither positive nor negative then it will be considered as zero.
Which is faster and feasible way for Writing If ?
Point | Example 1 | Example 2 |
---|---|---|
No of If Statements | 3 | 2 |
No of times Condition Tested (Positive No.) | 3 | 1 |
No of times Condition Tested (Negative No.) | 3 | 2 |
No of times Condition Tested (Zero No.) | 3 | 2 |
Flowchart for If-Else Ladder :
Difference between Declaration and Definition in C Programming
Some Examples of Declaration :
Declaration of Function :
Whenever we write function after the main function, compiler will through the error since it does not have any idea about the function at the time of calling function. If we provide prototype declaration of function then we compiler will not look for the definition.
int sum(int,int); main() { int res = sum(10,20); } int sum(int n1,int n2) { return(n1+n2); }
In the above example , first line is called as function declaration.
int sum(int,int);
Declaring Variable
Whenever we write declaration statement then memory will not be allocated for the variable. Variable declaration will randomly specify the memory location.
int ivar; float fvar;
Variable Declaration Vs Definition Differentiation Parameters
A. Space Reservation :
- Whenever we declare a variable then space will not be reserved for the variable.
- Whenever we declare a variable then compiler will not look for other details such as definition of the variable.
- Declaration is handy way to write code in which actual memory is not allocated.
struct book { int pages; float price; char *bname; };
In the above declaration memory is not allocated. Whenever we define a variable then memory will be allocated for the variable.
struct book b1;
B. What it does ?
- Declaration will identify the data type of the identifier.
- Definition of the variable will assign some value to it.
C. Re-Declaration Vs Re-Definition
In Both the cases we will get compile error. Re-declaration and Re-definition is illegal in C programming language.
Re-Declaring the Variable in Same Scope :
main() { int num1; int num1; }
&
struct book { int pages; float price; char *bname; }; struct book { int pages; float price; char *bname; };
above statement will cause error.
Re-Defining the Variable :
int sum(int n1,int n2) { return(n1+n2); } int sum(int n1,int n2) { return(n1+n2); }
above definition will cause compile error.
Difference between Declaration and Definition
No | Declaration | Definition |
---|---|---|
1 | Space is Not Reserved | In Definition Space is Reserved |
2 | Identifies Data Type | Some Initial Value is Assigned |
3 | Re-Declaration is Error | Re-Definition is Error |
What is Data Type in C Programming ?
What is Data Type in C Programming ?
- A Data Type is a Type of Data.
- Data Type is a Data Storage Format that can contain a Specific Type or Range of Values.
- When computer programs store data in variables, each variable must be assigned a specific data type.
Some of the Data Types Supported by C :
Data Type | keyword | Description |
---|---|---|
Integer Data Type | int | Stores the Integer Value |
Float Data Type | float | Stores the Floating Point Value |
Character Data Type | char | Stores the Single Character Value |
Long Data Type | long | Stores the Long range Integer Value |
Double Data Type | double | Stores the long range Floating Value |
Explanation :
- Whenever we declare variable in Computer’s memory, Computer must know the type of the data to be stored inside the memory.
- If we need to store the single character then the size of memory occupied will be different than storing the single integer number.
- The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C.
- A byte can store a relatively small amount of data one single character or a small integer (generally an integer between 0 and 255).
Re-commanded Readings : Data Type
Size Required to Store Variable of Different Data Types
Data Type | Borland C/C++ Compiler | Visual C++ |
---|---|---|
Integer | 2 Bytes | 4 bytes |
Float | 4 Bytes | 4 Bytes |
Character | 1 Byte | 1 Byte |
Long | 4 Byte | 8 Byte |
1-D Array - Compile Time Initializing in C Programming
Different Methods of 1-D Array - Compile Time Initializing
Whenever we declare an array we can initialize array directly at compile time. Initialization of an array is called as compiler time initialization if and only if we assign certain set of values to array element before executing program. i.e at Compilation Time.
Different Ways Of Array Initialization :
- Size is Specified Directly
- Size is Specified Indirectly
A. Method 1 : Array Size Specified Directly
In this method , we try to specify the Array Size directly.
int num[5] = {2,8,7,6,0};
In the above example we have specified the size of array as 5 directly in the initialization statement.Compiler will assign the set of values to particular element of the array.
num[0] = 2 num[1] = 8 num[2] = 7 num[3] = 6 num[4] = 0
As at the time of compilation all the elements are at Specified Position So This Initialization Scheme is Called as “Compile Time Initialization“.
Graphical Representation :
B. Method 2 : Size Specified Indirectly
In this scheme of compile time Initialization, We does not provide size to an array but instead we provide set of values to the array.
int num[] = {2,8,7,6,0};
Explanation :
- Compiler Counts the Number Of Elements Written Inside Pair of Braces and Determines the Size of An Array.
- After counting the number of elements inside the braces, The size of array is considered as 5 during complete execution.
- This type of Initialization Scheme is also Called as “Compile Time Initialization“
Live Example :
#include <stdio.h> int main() { int num[] = {2,8,7,6,0}; int i; for(i=0;i<5;i++) { printf("\nArray Element num[%d] : %d",i+1,num[i]); } return 0; }
Output :
Array Element num[1] : 2 Array Element num[2] : 8 Array Element num[3] : 7 Array Element num[4] : 6 Array Element num[5] : 0
Basic Stack Operation : Data Structure
Primitive Basic Stack Operation in C
We know that Stack can be represented using an array. Stack is open at one end and operations can be performed on single end. We can have different primitive operations on Stack Data Structure.
Creating Stack Data Structure :
typedef struct stack { int data[MAX]; int top; }stack;
Basic Operations Performed on Stack :
- Create
- Push
- Pop
- Empty
A. Creating Stack :
- Stack can be created by declaring the structure with two members.
- One Member can store the actual data in the form of array.
- Another Member can store the position of the topmost element.
typedef struct stack { int data[MAX]; int top; }stack;
B. Push Operation on Stack :
We have declared data array in the above declaration. Whenever we add any element in the ‘data’ array then it will be called as “Pushing Data on the Stack”.
Suppose “top” is a pointer to the top element in a stack. After every push operation, the value of “top” is incremented by one.
C. Pop Operation on Stack :
Whenever we try to remove element from the stack then the operation is called as POP Operation on Stack.
Some basic Terms :
Concept | Definition |
---|---|
Stack Push | The procedure of inserting a new element to the top of the stack is known as Push Operation |
Stack Overflow | Any attempt to insert a new element in already full stack is results into Stack Overflow. |
Stack Pop | The procedure of removing element from the top of the stack is called Pop Operation. |
Stack Underflow | Any attempt to delete an element from already empty stack results into Stack Underflow. |