Union

The Union is same like  Structure in which all members are stored at the same address. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size for certain data.

Just like with structures, the members of unions can be accessed with the. and -> operators. When the computer allocates memory for a program, it usually does so in one large block of bytes. Every variable allocated when the program runs occupies a segment of that block. When a variable is getting freed, it leaves a “hole” in the block allocated for the program. If this hole is of an unusual size, the computer may have difficulty allocating another variable to “fill” that hole, thus leading to inefficient memory usage.

Since unions have a standard data size, however, any “hole” left in memory by freeing a union can be filled by another instance of the same type of union. A union works because the space allocated for it is the space taken by its largest member; thus, the small-scale memory inefficiency of allocating space for the worst case leads to memory efficiency on a larger scale.

A union is declared in the same way as a structure. It has a list of members, as in the example below:
union customer
{
char *name;
int number;
};

Declaring union variables is similar to declaring structure variables, if you have not read structure before, please visit our simplified explanation for structure here
union customer customer1;

The difference between union and structure:

Structure Union
We can access all the members of a structure at any time. Only one member of a union can be accessed at any time.
Memory is allocated for all variables. Allocates memory for a variable which variable require more memory.
All members of the structure can be initialized Only the first member of a union can be initialized.

typedef:

The C programming language provides a keyword called typedef, which you can use to define your own data type. Following is an example to define a term myDatatype for int

typedef int myDatatype

After this definition myDatatype can be used for int.

Like this,

myDatatype a = 10;

 

 

Pointers

Pointer is an important concept in C programming Language, you should have a very good understanding of pointers. For most people, it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language.

So why use pointers? Why don’t we use arrays to create data structures?
With an array, you have to declare its maximum size at the beginning. But with a pointer memory allocated at runtime.There is a case, in which you have to use 10 megabytes initially and later 15 and after that 20, If you create an array for this with 20 megabytes size, remaining 10 Mb storage will be wasted. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated while the program is running. So the exact amount of memory is claimed and there is no waste.

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.

Declaration:

To declare a pointer you have to put an asterisk * in front of its name. A pointer can be typed or untyped. (A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type).

syntax

data_type *variable_name;

int *ptr_A;   // typed pointer

void *ptr_A; // untyped pointer

There are few important operations, which we will do with the help of pointers very frequently.

  •  we define a pointer variable
  • assign the address of a variable to a pointer
  •  finally, access the value at the address available in the pointer variable.

This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:
#include
int main ()
{
int  normal_var = 100;   /* actual variable declaration */
int  *ptr;        /* pointer variable declaration */
ptr= &normal_var;  /* store address of var in pointer variable*/
printf("Address of normal_var variable: %x\n", &normal_var);
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ptr);
/* access the value using the pointer */
printf("Value of *ptr variable: %d\n", *ptr);
return 0;
}
Address of normal_var variable: accfkk
Address stored in ptrvariable: accfkk
Value of *ptrvariable: 100

Pointer is an important concept, so you have to spend more time with it, and need to study clearly, in this we are giving you only basics in the pointer.we kindly advice you study pointers in detail with the help of the other reference If time and your positive reviews come to our hand, we will give separate tutorial for pointer.Best of Luck.

 

Recursion

Recursion Program in C

Factorial program in c using recursion:

Recursion is a technique in which a function calls itself, for example in above code factorial function is calling itself.

#include

long factorial(int);

int main()
{
int n;
long f;

printf(“Enter an integer to find factorial\n”);
scanf(“%d”, &n);

if (n < 0)
printf(“Negative integers are not allowed.\n”);
else
{
f = factorial(n);
printf(“%d! = %ld\n”, n, f);
}
return 0;
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

Pascal Triangle

Pascal Triangle Program in C

Pascal Triangle:

#include
long factorial(int);
int main()
{
int i, n, c;
printf(“Enter the number of rows you wish to see in pascal triangle\n”);
scanf(“%d”,&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n – i – 2 ) ; c++ )
printf(” “);
for( c = 0 ; c <= i ; c++ )
printf(“%ld “,factorial(i)/(factorial(c)*factorial(i-c)));
printf(“\n”);
}
return 0;
}

long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}

output:

Enter the number of rows you wish to see in pascal triangle
4
    1
1 1
1 2 1
1 3 3 1

Palindrome

Palindrome program in C

Palindrome :

 

Palindrome number in c

#include

main()
{
int n, reverse = 0, temp;

printf(“Enter a number to check if it is a palindrome or not\n”);
scanf(“%d”,&n);

temp = n;

while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}

if ( n == reverse )
printf(“%d is a palindrome number.\n”, n);
else
printf(“%d is not a palindrome number.\n”, n);

return 0;
}

C program for palindrome without using string functions

#include
#include

int main()
{
char text[100];
int begin, middle, end, length = 0;

gets(text);

while ( text[length] != ‘\0’ )
length++;

end = length – 1;
middle = length/2;

for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf(“Not a palindrome.\n”);
break;
}
end–;
}
if( begin == middle )
printf(“Palindrome.\n”);

return 0;
}

C program for palindrome
#include
#include

int main()
{
char a[100], b[100];

printf(“Enter the string to check if it is a palindrome\n”);
gets(a);

strcpy(b,a);
strrev(b);

if( strcmp(a,b) == 0 )
printf(“Entered string is a palindrome.\n”);
else
printf(“Entered string is not a palindrome.\n”);

return 0;
}

Introduction to Swift Programming

   Apple introduced a new programming language for IOS and OS X developers called Swift. Our Swift tutorial for iOS developers & beginners is a simplified best tutorial for learning swift from basics and gives the clear understanding of apple’s Swift language. Despite the new name, Swift is compatible with the root of its predecessors like C, Objective-C, and Cocoa Touch framework.If you have been programming with Objective-C, learning Swift would be a breeze as it has inherited a number of syntaxes that you may already have been familiar with. And if learning Swift is on your to-do list but you haven’t started yet, We are providing simplified tutorials for learning Swift

We will update this tutorial regularly to add/modify contents. Please visit regularly.

Setup:

There are few options to have hands on.

1. XCode:

We have to download and install Xcode 8. Once you have installed it, open and select File from the

menu -> New -> Select Source

on the left under either iOS or OS X -> Playground. Give your playground name. That’s it, you are ready to get started.

2. Terminal:

If we have two or more versions of Xcode installed then you will need to select Xcode 6 and above as our default version. If we are using Xcode 6 and above only, then skip this step and ahead to step 3, otherwise go ahead and run the following line:

sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer/

Please check your app name in the “Applications” folder to write out the appropriate path when using xcode-select.

3. To start the REPL type:

xcrun swift

3. Online compiler for learning Swift

 Go to the below site, it has an online compiler for swift.

http://www.runswiftlang.com/

Variables

Variables & constants in Swift is a basic and fundamental concept. This tutorial is focusing on explaining let(immutable) & var(mutable) declaration. Both variable and constant two terms are self-explanatory, Constant refers to the values which never gets changed during runtime. Whereas Variable refers to the values, which can be changed during runtime.

Declaring a Constant:

let is the keyword, used to declare the constants.

let myName = “ABCD” and let age = 20

var is the keyword , used to declare the variables.

var salary= 2000;

You can change the value of this salary variable since it is declared with var keyword. whereas you can’t change the age since it is declared with let keyword.

for ex:-

salary = 3000 // works fine

age = 22 // compile error

Note: Though it is not mandatory to terminate the line with a semicolon in swift. You can use semicolon to write multiple lines in single line . ex let a = 90; var b = 100 

We will see variables & constants in detail in data type & Tuples

 

Binary Search

Binary Search in C Programming Following program explains binary Search:
Binary Search finds the position of a given value from an array .
Algorithm:
It starts with the middle element of the list.
1. If the middle element of the list is equal to the ‘input key’ then we have found the position the specified value.
2. Else if the ‘input key’ is greater than the middle element then the ‘input key’ has to be present in the last half of the list.
3. Or if the ‘input key’ is lesser than the middle element then the ‘input key’ has to be present in the first half of the list.

#include
int main(){
int a[10],i,n,m,c,l,u;
printf(“Enter the size of an array: “);
scanf(“%d”,&n);
printf(“Enter the elements of the array: ” );
for(i=0;i<n;i++){
scanf(“%d”,&a[i]);
}
printf(“Enter the number to be search: “);
scanf(“%d”,&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf(“Number is not found.”);
else
printf(“Number is found.”);
return 0;
}
int binary(int a[],int n,int m,int l,int u){
int mid,c=0;
if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}

Bubble Sort

Bubble Sort in C Programming

#include
#include
int arr[20];
int n;
void get();
void BubbleSort();
void Show();

void main(){
clrscr();
printf(“- This Program Explains Sorting Using Bubble Sort Algorithm -\n\n”);
get();
BubbleSort();
Show();
getch();
}

void get(){
int i;
while(1){
printf(“Enter the size of the elements :\n”);
scanf(“%d”,&n);
if(n<=20)
break;
else
printf(“Sorry the maximum no of elements is 20\n\n”);
}
printf(“\n”);
printf(“———————-\n”);
printf(“Enter the values \n”);
printf(“———————-\n\n”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
}

void BubbleSort(){
int i,j;
for(i=1;i<n;i++){

for(j=0;j<n-i;j++){

if(arr[j]>arr[j+1]){      //for descending use “<”

int t;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;

}

}

}

}

void Show(){
int i;
printf(“\n”);
printf(“———————–\n”);
printf(“Sorted Array Elements\n”);
printf(“———————–\n”);
for(i=0;i<n;i++){
printf(“%d\n”,arr[i]);
}
}