Monday, June 8, 2020

ARRAY IN C [PART 2]

You tube link for insert new element in an array

Delete an element from an array 
you tube link to delete an element from an array
https://www.youtube.com/watch?v=2U8-9wvuhT4
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
clrscr();
int ar[10];
int n,i,x,flag=0,pos;
printf("Enter the size of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter value in an array:");
scanf("%d",&ar[i]);
}
printf("\nEnter the element to be deleted : ");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(ar[i]==x)
{
flag=1;
pos=i;
break;
}
}
if(flag==1)
{
for(i=pos;i<n-1;i++)
{
ar[i]=ar[i+1];
}
printf("\nArray elements after deleting an element:\n");
for(i=0;i<n-1;i++)
{
printf("%d\n",ar[i]);
}
}
else
printf("\nElement not found in the list");
getch();

}


output--
delete an element from an array

Merge two array of same size
//merge two array of same size in c
#include<stdio.h>
#include<conio.h>
void main()
{
int ar1[5],ar2[5],ar3[10],i,j,num;
clrscr();

for(i=0;i<5;i++)
{
printf("\nEnter values for array1:\n");
scanf("%d",&ar1[i]);
}
for(i=0;i<5;i++)
{
printf("\nEnter values for array2:\n");
scanf("%d",&ar2[i]);
}
for(i=0;i<5;i++)
{
ar3[i]=ar1[i];
ar3[i+5]=ar2[i];
}
printf("\nvalues in array 3:\n");
for(i=0;i<10;i++)
{
printf("%d",ar3[i]);
}
getch();
}

output --
Merge two array of same size

Sum of two 2-D array :-

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][2],b[2][2],c[2][2];
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter value in first array:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter value in second array:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
printf("\nsum of two 2-D array:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}

Product of two 2-D array :

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][2],b[2][2],c[2][2];
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter value in first array:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter value in second array:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nproduct  of two 2-D array:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}


Recursion  -
Recursion is a process in which function call itself
conditon for recursion -
1. A recursive function must have a base case
2. A recursive function must change its state and moves toward base case
Fibonacci series by Recursion


#include<stdio.h>
#include<conio.h>
int fib(int);
void main()
{
int i,n;
printf("\nEnter no. of terms:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d\t",fib(i));
}
getch();
}
int fib(int n)
{
if(n==0)
return 0;
else if( n==1)
return 1;
else
return(fib(n-1)+ fib(n- 2));
}

gcd of two numbers
//gcd of two numbers
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int m,n,r;
clrscr();
printf("\nEnter first number:");
scanf("%d",&m);
printf("\nEnter second number:");
scanf("%d",&n);
r=gcd(m,n);
printf("\nGCD of two numbers :%d",r);
getch();
}
int gcd(int a,int b)
{
if(b==0)
return(a);
else
if(a<b)
return(gcd(b,a));
else
return(gcd(b,a%b));
}


output - 
gcd of two numbers


Address calculation in 1D-Array
                                   A[i]=B + W* (I - LB)
                
                                        B=base address
                                        W=storage size of one element stored in array
                                        I = Subscript of  element whose address to be found
                                        LB =lower limit of subscsript( assume zero if not specified)
Example -  Given the base address of an array B[1300-1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B [1700].
 Solution -           A[i]   =    B    + W* (I - LB)
                                      = 1020 +  2  * ( 1700 - 1300 )
                                      = 1020 + 2 *  (400)
                                     =1020 + 800
                                     =1820
Address calculation in 2D-Array
It is calculated in two ways :
1. Row Major System
2. Column Major System

Row Major System :


                                    A[i][j]= B + W* [N* ( I - Lr ) + ( J - Lc) ]
                                         B=base address
                                        W=storage size of one element stored in array
                                         N= Number of column of the given matrix
                                        I=  Subscript of  element whose address to be found
                                        Lr = Lower limit of row [assume zero if not given
                                        J = Column subsrcipt of element whose address is to be found
                                       Lc = Lower limit of column 
Example - An Array X[- 15.......10,15.......40] requires one byte of storage . If the beginning loacation is 1500 determine the location of  X[15][20].
Solution -     
                  Number of columns = (Uc - Lc ) + 1 =( 40 - 15 ) + 1 = 25 + 1 = 26
           

                             A[i][j]= B       +   W*   [N*  ( I - Lr )      + ( J - Lc) ]
                                         =1500  +    1 * [26 * (15 - (-15)) + ( 20 - 15)]
                                         =1 500 +    1*[26* (15 + 15)  + 5]
                                          =1500 + 1* [ 26 * 30 + 5]
                                          =1500 + 1* [780+ 5]
                                         =1500 + 1*785
                                         =1500 + 785
                                        =2285

 Character String in C


1.   Strings are one dimensional character array
2.   They are terminated by null character ‘\0’
3.   C automatically places NULL character at the end of the string
Declaration and initialization
char ar[6] = {‘H’,’E’,’L’,’L’,’O’};
 char ar[]={“helo”};
H
E
L
L
O
\0

Type of string Function –

Serial No.
Function
Process
1.   
strcpy(s1,s2)
Copy string s2 into s1
2.   
Strcat(s1,s2)
Concatenate string s2 into the end of string s1
3.   
Strlen(s1)
Return the length of string s1
4.   
Strcmp(s1,s2)
Return 0 if string s1 and s2 are same ,less than 0 if s1<s2, greater than 0 is s1>s2
5.   
Strchr(s1,ch)
Returns a pointer to the first occurance of character ch in string 1
6.   
Strstr(s1)
Returns a pointer to the first occurance of the string s2 in string s1

pointer in c

Pointer in c
1.  Pointer is a variable which store address of which store address.
2.  Declaration of pointer –
                      type *variable_name ;
example -            int  * p;


#include<stdio.h>
#include<conio.h>
Void main( )
{
Int var=20;
Int *ip;
Ip=&var;      //store address of var in pointer variable
printf(“\n address of var variable :%x “,&var ) ;
printf(“ \n address store in ip variable %x”,ip;
printf(“\n value of *ip variable %d”,*ip);
getch();
}

pointer in c

Null Pointer

1. If you dont have exact address  to assign it is always good practice to assign NULL value to pointer.
2. It is done at the time of declaration.
3. A pointer that is assigned NULL is called NULL pointer.
4. Null pointer is a constant with a value 0 assigned in libraries.
Example  -
//Null pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr=NULL;
printf("The value of ptr is :%x",ptr);
getch();

}

output --
The value of ptr is : 0
Static Memory Allocation
1. It is done before execution of program.
2. This memory allocation is fixed and can not be changed
3. It is simple 
 the main disadvantage is wastage of memory

Dynamic Memory Allocation
1. Allocation is done before program execution.
2. Memory can be freed when not required.
3. More efficient method.
Difference between statoc memory allocation and dynamic memory allocation
Static memory allocation
Dynamic memory allocation
1. It is done before execution of program.
1. Allocation is done at the time of execution.
2. Less Efficient
2. More efficient
3. There is no memory reusability
3. Memory reusability and memory can be freed when not required
4. It uses data structure called stack
3. It uses data structure called heap


Memory Allocation Function 
size of array is fixed and cannot change but sometime it is insufficient
to solve this problem we allocate memory manually during run time using memory allocation function.


 Function
 Description
Example
 malloc()
 allocates single block of requested memory
ptr=(int*)calloc(n*sizeof(int));
 calloc()
 allocates multiple block of requested memory
ptr=(int*)calloc(n,sizeof(int));
 realloc()
 reallocate the memory occupied by malloc()
ptr= realloc(ptr,new-size);
 free ()
 free allocated memory
free(ptr)
 //static memory allocation
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int *ar;
int i,n;
printf("\nEnter number of elements:");
scanf("%d",&n);
ar=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter number:",i);
scanf("%d",&ar[i]);
}
printf("\nArray is :\n");
for(i=0;i<n;i++)
{
printf("\n%d",ar[i]);
}
getch();
}





No comments:

Post a Comment

if you have any doubt. please let me know.

Data communication

 Communication means exchange of data between two or more Devices. Components of data communication - 1. Sender.    2. Receiver.     3. Mess...