Search This Blog

Tuesday, April 21, 2020

Pointer full expalination C-Programming.

Mr. abhijeet dwivedi

        C-Programming:-

In this tutorial you'll learn :-

Pointer:-

 

 

Pointer is a variable that stores the address of another variable . The pointer is represented by sign (  *  ) .

There are some features of pointer in C programming language are  :-

1. Pointer saves memory space .
2. Execution time with pointer is faster because data is manipulate with the address is direct access to memory location .
3. Pointer reduce length and complexity of the program .
4. 2D and 3D array and many more dimension array representation is easy in pointer .

Pointer declaration :-

Pointer declaration names a pointer variable and specifies the type of the object to which the variable point.

syntax :-              Data type               * pointer name  ;

for e,g:-

int x=15; // declaration of pointer
int *p ;
*p=&x;
so, here we accessing the pointer value.

Initializing the pointer variable :-

The process of assigning the add variable in a pointer variable is known as initialization of  a pointer variable.

  for e.g -  

int q=100 , *p ;
p=&q ;
so this is the initializing pointer.

Let see one example :-

#include<stdio.h>
int main()
{
int x=10 , *a ;
float y=20 , * b ;
a=&x;
b= &y;
printf(" a = %d \ n   b = %f",(*a),(*b));
return( 0 ) ;
}

OUTPUT

a=10
b=20

Pointer to pointer:-

Pointer to pointer, the first pointer contain the address of second pointer. let see below.


               Pointer                      pointer                             variable
       Value
Address
   Address



 

for e.g:-

#include<stdio.h>
int main()
{
int v=300 ;
int *ptr , *p ;
 p=&v ;
ptr = & p ;
printf(" value of v is = %d",v);
printf(" value of p is = %d", p);
printf(" value of ptr is = %d", ptr);
return( 0 ) ;
}

OUTPUT:-

value of v is= 300 
value of p is = 300
value of ptr is 300

Pointer and function :-

The pointer can be used as arguments in the function . The argument or parameters to the function are passed in two ways:-

* Call by value :-

The process of passing of actual value of variables is known as "call by value " . When a function is called in program , the values to the arguments in the function are taken by the calling program, the values taken  can be used inside the function. Alteration to the value isn't accepted inside the function in calling program but change is locally available in the function .

For example:-

// Write a program to swap the values stored in two variable using function with pointer.


#include<stdio.h>
void swap ( int  , int  );
int main( )
{
int a =10 , b =20 ;
printf("\n Before swapping a= %d  ,  b + %d", a , b );
swap ( a , b) ;
printf(" After swapping :  a =%d   b  = %d\n",a,b);
}
void swap ( int  a  , b )
{
int temp;
temp =  a;
a = b;
b = temp;
}

OUTPUT:-

Before swapping   a = 10  , b = 20
After swapping     a = 10  ,  b =20

* Call by reference:-

The process of calling a function using pointer to pass the address of variable is known as call by reference . A function can be declared with pointer as it's arguments.
The address of the variable are altered to the pointer and any changes made to the value inside the function is automatically carried out in the location. This change is accepted by the calling function.

For example:-

// Write a program to swap the values stored in two variable using function with pointer.

#include<stdio.h>
void swap ( int * , int * );
int main( )
{
int a =10 , b =20 ;
printf("\n Before swapping a= %d  ,  b + %d", a , b );
swap ( &a , &b) ;
printf(" After swapping :  a =%d   b  = %d\n",a,b);
}
void swap ( int * a  , *b )
{
int temp;
temp =  *a;
*a = *b;
*b = temp;
}

OUTPUT:-

Before swapping   a = 10  , b = 20
After swapping     a = 20  ,  b = 10

  Pointer and array :-

* Array is a collection of similar data type element stored under common name .
* The element of an array can be efficently accessed by using pointer.
* The base address of array  start with zero element of the array.

for e.g:-

int   a[5] = { 10 , 20 , 30 , 40 , 50 };
 element     =       a[0]        a[1]       a[2]           a[3]            a[4]
value          =       10           20           30             40              50  
address      =      1000        1002      1004       1006          1008

Relation between pointer and array:-

An array is a block of  sequential data , where as  pointer ois a variable which holds the base address of an array so there are a relation between an array and pointer are :-

let see one example:-

//Write a program to print a address of array element.

#include<stdio.h>
int main()
{
int x[5] , i ;
for(i=0 ; i<5 ; i++)
 {
printf("address of an array x is =%p", x);
return (0);
}

Another example:-

#include<stdio.h>
int main ()
{
int x[5] = { 1 , 2 , 3 , 4 , 5 } ;
int *p ;
p=&a[1] ;
printf("%d", *p);
printf("%d",*(p+1)) ;
printf("%d", *( p+2-1) ;
return(0) ;
}

OUTPUT

3
4
4

Formula of calculating address of an array using pointer :-

Address of an array element is a calculating using it's index and the scale factor of data type.

scale factor= size of data type in bit.

 int    arr [3] = ( base address ) + ( 3*scale factor ) .

Array of pointer:-

The array element can be accessed by two method :-
1. Standard array notation.
2. Pointer arithmetic .
A collection od address or collection of pointers .

Advantages of an array of pointer :-

1. Accessing arrays through pointer is marginally faster than using array notation 
2. Pointer gives better control over array processing

For e.g:-

include<stdio.h>
int main ()
{
int *p[5] ;
int a=10 , b=20 , c=30 , d= 40 , e= 50 , i ;
p[0] = &a ;
p[1] = &b ;
p[2] = &c ;
p[3] = &d ;
p[4] = &e;
 for( i=0 ; i<5 ; i++)
{
printf(" address  =%d \ t value = % \ n ", p[i] , * p[i] );
}
return ( 0 ) ;
}

Pointer and structure :-

This topic carry a new topic STRUCTURE, that we are not know how to use this so we learn this topic in our upcoming post structure and union .

Pointer and string :-

A character pointer is a pointer to the character . It can be declared as :-
char   * pointer character ;
A character pointer  can also be used to access character array and string constant in  the same way as accessing numeric arrays using their respective pointer variable .

Let see one example :-

'// Write a program to input any string using pointer .

#include<stdio.h>
int main()
{
char name [ ] = " codeswithabhi " ;
char *p ;
p=name ;
while ( *p != '\0' )
{
printf("%c",*p);
p++;
}
return(0);
}

OUTPUT 

codeswithabhi 

Thanks for reading ...........

Monday, April 20, 2020

Additon of Matrix in C Programming

Mr. abhijeet dwivedi

             C-Programming:-

In this tutorial you will learn :-

 

// Write a program to perform addition of two matrix 3 x 3 and greater a result matrix

#include<stdio.h>
int main()
{
int a[3][3] ,  b[3][3] , c[3][3] ;
int i , j ;
printf("enter the value of matrix a");
for(i=0 ; i<3 ; i++ )
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",& a[i][j]);
}
}
printf("enter the value of second matrix ");
for("i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
c[i][j] = a[i][j] + b[i][j] ;
} 
}
printf("addition of matrix are ");
for(i=0 ; i<3 ; i++)
{
for(i=0 ; i<3 ; i++)\
{
printf("%d", c[i][j]);
}
printf(" \ n ");
}
return ( 0 );
}

 OUTPUT:-

enter the value of first matrix 1 2 3 4 5 6 7 8 9
enter the value of second matrix 1 2 3 4 5 6 7 8 9
addition of matrix is   2    4    6  
                                     8    10   12
                                    14   16   18 

String explaination in C-Programming

Mr. abhijeet dwivedi

                                       C-Programming

In this tutorial yon will learn:

String:- 

 

 

 A group of character is known as string .  C doesn't support string as a data type. We can create a string using character array.

Declaration of string:-

Declaring a string is a simple as declaring a one dimensional array. Below the basic syntax for declaring a string 
Data type             Array name [ size ] ;

for e.g:-         char     arr [ 30 ];  \\ declaration of string.

Initializing a string:-

A string can be initialized in different ways. We will explain this with the help of an example:-

1. char  str [ ]  =  "codeswithabhi" ;
2. char str [ 30 ] = " codeswithabhi" ;
3. char str [ ] = {'a' , 'b' , 'h' , 'i'} ;

Let see one example :-

// Write a program to accept values in a variable character array and print values from an array.

# include<stdio.h>
#include<conio.h>
void main()
{
char  str [ ] = "codeswithabhi" ;   // declare an initialize string
printf("%s", str);                        // print string
getch();
}

Output

codeswithabhi

String.h  as a header file .

The string.h header file have a some pre defined function\ library function which is very helpful in string's program  and make a code short.

There are some library function / predefined function of string in C language are:-

 1.   Strlen (  ) ; 

 Strlen stand for string lenght this function is used to count a character in a string it return a number as resultant . In this function length doesn't include null character. 

syntax:-

Variable = strlen ( string ) ;

for e.g :-

char na[ 20 ] = "abhi" ;
int l;
l=strlen(na);
printf("%d",l);

2.  Strrev (  ) ; 

  Strrev ( ) stand for string reverse this function is used to reverse a given string.

syntax       strrev ( string ) ;

for e.g :-

int main
char na [10] ={ 'a','b','h','i'};
printf("%s",na );
printf("%d",strrev(na));
 return(0);
}

Output

ihba

3.Strcpy (  ) ;  

 Strcpy stands for string copy this function is used to copy string into another string.

syntax:-

strcpy ( destination string , source string);

for e.g:

char str[10] = "abhi";
char str1 [10] ;
printf("%s",strcpy(str1,str));
return(0);
}

Output

abhi

4. Strcat (  ) ;  

 Strcat stand for string concatenation . When we combine two string add the character from one string to end of the another string this process is known as concatenation.

syntax:-           strcat( str , str1 ) ;

for e.g:--            

     char str [10]="codeswith";
                               char str1 [10]= "abhi" ;
                              strcat( str , str1 ) ;
                             printf("%s",str);       
                             printf("%s"str1);
                              return(0);

            Output

codeswithabhi

5. Strlwr ( ) ;   

 Strlwr (  )  stand for string lower case this function is used to convert a upper case string to the lower case.

6. Strcmp (  ) ;  

Strcpm  stand for compare string . It is used for to compare string this function compare two string on the basic of difference between ASCII value of the first string is greater than the ASCII value of second string it return negative value and if both string are equal then it return 0.

syntax :-         num = strcmp ( str1, str2) ;

for e.g:-

// write a program to check given string is identical or not 

#include<string.h>
#include<string.h>
int main () 
{
char str1 [10] ;
char str2 [10] ;
int cmp;
printf("enter first string ");
gets ( str1);
printf("enter second string");
  gets(str2) ;
cmp=strcmp(str1 , str2);
if(cmp==0)
{
printf("identical");
}
else 
{
printf("not identical");
}
return (0);
}

Output

enter first string      codeswithabhi
enetr second string    codeswithabhi
identical

7. Strupr ( ) ; 

 Strupr () stand for string upper case .This function used to convert given string into upper case.

syntax :-     strupr ( "string") ;

for e.g:

# include<stdio.h>
#include<string.h>
int main()
{
char str[100] ;
gets ( str) ;
printf("%s",strupr(str));
return(0);
}

// Write a program to find out the length of string with using library function

#include<stdio.h>
#include<conio.>
void main()
{
char  name [30];
int  i , l , count=0;
printf("enter your name ");
scanf("%s ", na);
for(i=0 ; name != '/0' ; i++)
{
count ++;
}
l=count ;
printf(" length of name is  =%d", l);
getch();
}

OUTPUT

enter your name  codeswithabhi
lenghth of name is = 13

Thank  you for reading my post ...

                                                   "codeswithabhi"                  


Sunday, April 19, 2020

Searching on array in C-Programming

Mr. abhijeet dwivedi

                                      C-Programming:-

In this tutorial yon will learn: 

* Searching  :- 

 

 The process of finding on element in an array is known as searching.

There are two types of searching are:-

1 Linear searching :-  Linear searching is also known as sequential search in linear search process we will find out an element from lower index one by one in an array.\

let we take one example :-

Write a program to find out the existence and position of a given element is an array.

#includestdio.h>
#include<conio.h>
void main()
{
int  a [5] = { 10 , 20 , 30 , 40 , 50 };
int n;
printf("enter no you want to search " );
scanf("%d",&n);
for( i=0 ; i<5  ; i++)
{
if (a [i]=n);
{
printf("%d is present location is ", n , i++) ;
}
else
{
printf("not present" ) ;
 }
}
getch() ;
}

 * Existence and sequence :-

Take one example  :-

#include<stdio.h>
#include<conio.h>
void mian()
{
int a [5] ={ 10 , 20 , 30 , 40 , 50 };
int n, i count= 0;
printf("Enter number you want to search");
scanf("%d",&n);
for(i=0 ; i<5 ; i++)
{
if ( a[5]==n)
{
count ++;
}
if ( count>0)
{
printf("element found % d", count);
}
else
{
printf("element not found");
}
getch();
}

2 Binary searching :- 

                                      Binary search algorithm applies to a sorted array for searching an element 
* sort ( ascending)
* Find the middle value / element
compare mid value to given number :-
match                                               [ Greater ------------- Right]
no match                                          [ Lesser  -------------- Left]

                                  Steps of binary searching again:-   

Binary search is the fastest search we can perform binary process in fallowing:-

* Sort the given array in ascending order.
* Find the mid element of an array .  
       Lowe index  +  Higher index /  2 
Compare mid element with given element want to be search.
Part ( a ) -  If no matched with the mid element  .   Print "element found " .
Part ( b ) - If mid element not matched then compare element is greater  or  lesser.
Part ( c ) - If value is greater start search in right list of an array .  If value is lesser start in left list of array.
                                                         ( Or in simple search) 
Perform sorting ( ASC )
Find mid element 
Compare mid value to given  number.
Value matched                  ( Element found)
If value is not matched --
( a ) Value is greater   ( search in right )
( b ) Value is lesser    ( search in left ) 

Array in C-Programming | Program to perform operation on array in C language | codeswithabhi

Mr. abhijeet dwivedi

                                     C-Programming:-

In this tutorial yon will learn:

                                                    Array :-

 

 

Array is the collection of homogeneous data items that share a common name ,  Array contain similar kind  of values on a same name it allocate contiguous or sequential memory. 

*  Declaration of an array:-

 data type          array name [ array size ]  ;

for example,
float   bookprice [ 5 ] ;

*Initialization of an array:-

You can initialize an array in both condition 

 1 Compilation time :-  
  
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]  =  { 1 , 2 , 3 , 4 , 5 };

Memory background like this :- 

  ___________________arr______________

           0
            1
            2
           3
              4

        arr[0]                          arr[1]                         arr[2]                        arr[3]                        arr[4]


2 On run time :-  

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5];
                                                And after use scanf function  to store all values :------

 There are two main types of array are:-

1.  Single dimension :-  Single dimension array  also known as 1D array which contain either row or column of a given size of memory.

syntax :-

Data type       array name      [  size  ];

for example :-

int   arr [5]  ;

Memory background like this :- 

       _______________arr_________________

           0
            1
            2
           3
              4

        arr[0]                          arr[1]                         arr[2]                        arr[3]                        arr[4]

 for example:-

* Write a program  to accepting value in array and print :-

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5];
int i,j;
printf("enter  5 any values");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("values od an array \ n");
for(j=0;j<5;j++)
{
printf("%d\n",a[i]);
}
getch();
}

                                                                    OUTPUT

                                    enter 5 any values  1
                                                                   2
                                                                   3                  
                                                                   4
                                                                   5
                                           values  of array 1
                                                                     2
                                                                     3
                                                                     4
                                                                     5 
                           

                           In an array there are two types condition are:-

* Under flow condition :- In this condition , the no. of values is less than the size of an array.
* Over flow condition :-   In this condition , the no of values is more than the size of an array.

                                                Note point :-
* On compilation time , by default value become  0  if cells are empty.
* On run time , by default value become garbage values if cells are empty.

2 double dimension  array:-  2D  array is a double dimension array having row and  column 
Syntax :-
 Data type                  array name           [row] [column] ;
 int  arr[3][3] ;
        __________rows_____________
                                                   
      1
      2
     3
      4
      5
     6
      7
      8
     9
 
  column



Let see one example to more clear this topic:-

*  write a program to create 3 x 3 matrix accept the values in matrix and print the values in matrix format:-

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][3] ;

printf("enter the value of 3 x 3 matrixs");
for(i=0  ;   i<3   ;  i++)\
{
for(j=0  ;  j<3  ;  j++)
{
scanf("%d",& arr[i][j] ) ;
}
}
printf("Array values are");
for(i=0  ;  i<3  ;  i++)
{
for(j=0  ;  j<3  ;  j++)
{
printf("%d", arr[i][j] ) ;
}
printf(" \ n");
}
getch();
}

 OUTPUT:-

 enter the values of 3 x 3 matrix 1 2 3 4 5 6 7 8 9

 1   2   3  
 4   5   6
 7   8   9

Array and function:-

Array function in C is a data structure that hold multiple elements of the same data type. The size of an array is fixed and the elements are collected in a sequential manner .

let see one example to more clear this topic :-


#include<stdio.h>
#include<conio.h>
int  sum  (int [5] ) ;
void main()
{
int  arr[5] ;
int  i  ,  s ;
printf("Enter any 5 values ");
for(i=0  ;  i<5  ;  i++)
{
scanf("%d",&arr[i] );
}
s= sum ( a ) ;             OR             s=sum( a [ ] ) ;
printf("%d",s);
getch();
}
int sum( int b [5] )
{
int i,s=0;
for(i=0  ;  i<5  ;  i++)
{
s=s+b [i] ;
}
return(s);
}


Thanks for reading this post and if you wanna know more to related this topic 
read m next post ,......thankyou for your support...........