Professional Documents
Culture Documents
Linear search
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
} //ascending
}
}
Bubble sort -- passwise largest element right
for(i=0;i<n-1 ; i++)
{
for(j=0;j<(n- i)-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
} //ascending
}
}
Duplicate elements
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
}
}
Initialization of 1D
Syntax:
Type array name[size]={elements};
Example:
int a[5]={1,2,3,4,5};
2D
Declaration of 2D:
Data type array name[size][size];
Example:
a[0][0] a[0][1] a[0][2]
int a[3][3];
a[1][0] a[1][1] a[1][2]
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
Transpose matrix
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
printf("Transpose of the matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
Initialization of 2D
Syntax:
Type array name[size][size]={elements};
Example:
int a[2][2]={1,2,3,4,};
int a[2][2]={1,2,3,4},b[2][2]={1,2,3,4};
Multi dimensional array
Syntax:
Data type array name [size1][size2]....[size n];
Ex:
int a[3][3]……..[3];
Applications of Arrays in C
char a[5];
clrscr();
printf(“enter a string”); => hai
gets(a); or scanf(“%s”,a); a=hai
puts(a); or printf(“%s”,a); hai
getch();
}
a[0] a[1] a[2] a[3] a[4]
h a i ‘\0’
To find length of the string
1. while(a[i]!='\0')
{
i++;
}
printf("the length of the string is=%d",i);
Reverse of the string
2 while(a[i]!='\0’)
{
i++;
}
while(--i>=0)
{
b[j]=a[i];
j++;
}
b[j]=‘\0’;
printf("%s",b);
String copy
3. while(a[i]!='\0')
{
b[j]=a[i];
i++;
j++;
}
b[j]=‘\0’;
printf("b=%s",b);
String concatenate
4. while(a[i]!='\0')
{
i++;
}
while(b[j]!='\0')
{
a[i]=b[j];
i++;
j++;
}
printf("%s",a);
String compare
5. while(a[i]==b[j] && b[j]!='\0')
{
i++;
j++;
}
if(a[i]==b[j])
{
printf(“two strings areequal");
}
else
{
printf(“two strings arenot equal");
}
String handling functions
1.strlen()
2.strrev()
3.strcpy()
4.strcat()
5.strcmp()
6.strlwr()
7.strupr()
Strlen()
Syntax:
i=strlen(variable);
Example:
F1: i=strlen(a);
printf("the string length is=%d",i);
Strrev()
Syntax:
strrev(variable);
Example:
F2: strrev(a);
printf("%s",a);
Strcpy()
Syntax:
strcpy(destination variable,source variable);
Example:
F3: strcpy(b,a);
printf("%s",b);
Strcat()
Syntax:
strcat(destination variable,source variable);
Example:
F4: strcat(a,b);
printf("%s",a);
strcmp()
Syntax:
p=strcmp(variable1,variabl2);
F5: p=strcmp(a,b);
if(p==0)
{
printf("two strings are equal");
}
else
{
printf("two strings are not equal");
}
strlwr() & strupr()
Syntax:
Strlwr(variable);
F6: strlwr(a);
Printf(“%s”,a)
Palindrome or not
F7: strcpy(b,a);
strrev(b);
p=strcmp(a,b);
if(p==0)
{
printf("the given string is palindrome");
}
else
{
printf("the given string is not a palindrome");
}
1) Without using any semicolon (;) in program write a c
program which output is: HELLO WORLD?
2)What will be output of following code?
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
clrscr();
printf("%s",a);
getch();
Output:
String initialization
Syntax:
char a[10]={‘v’ , ’r’ , ’s’ , ’e’ , ’c’,’\0’};
Ex:
1.To find length of the string with and without using
string handling function.(String initialization).
void main()
{
char a[10]={‘v’ , ’r’ , ’s’ , ’e’ , ’c’,’\0’};
int i=0;
Clrscr();
i=strlen(a);
Printf(“i=%d”,i);
getch();
}
void main()
{
char a[10]={‘v’ , ’r’ , ’s’ , ’e’ , ’c’,’\0’};
int i=0;
clrscr();
while(a[i]!=‘\0’)
{
i++;
}
Printf(“i=%d”,i);
getch();
}
functions
1.function declaration
2.function definition
3.function calling
Uses of Functions
void maximum();
void main()
{
clrscr();
maximum();
getch();
}
void maximum( )
{
int a=4,b=6,c=2;
int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf("maximum=%d",max);
}
Function with arguments and no return value
Syntax:
void maximum(int,int,int);
void main()
{
int a=4,b=6,c=2;
clrscr();
maximum(x,y,z);
getch();
}
void maximum(int a,int b,int c)
{
int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf("maximum=%d",max);
}
Function with arguments and with return value
Syntax:
int maximum(int,int.int);
void main()
{
int a=4,b=6,c=2;
clrscr()
max=maximum(x,y,z);
printf("maximum=%d",max);
getch();
}
int maximum(int a,int b,int c)
{
int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
Function with no arguments and with return value
Syntax:
int maximum();
int max;
void main()
{
clrscr();
max=maximum( );
Printf(“maximum=%d”,max);
getch();
}
int maximum( )
{
int a=4,b=6,c=2
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
Parameter passing
Call-by-value
Call-by-reference
Call-by-value
fswap:
void swap(int,int);
void main()
{
int x=4,y=5;
swap(x,y);
getch();
}
void swap(int a,int b)
{
int temp;
printf("Before swap a=%d\tb=%d\n\n",a,b);
temp=a;
a=b;
b=temp;
printf("After swap a=%d\tb=%d",a,b);
}
Call-by-reference
fswap1:
void swap(int *,int *);
void main()
{
int a=4,b=5;
swap(&a,&b);
getch();
}
void swap(int *a,int *b)
{
int temp;
printf(“Before swap a=%d \t b=%d\n\n",*a,*b);
temp=*a;
*a=*b;
*b=temp;
printf("After swap a=%d \t b=%d",*a,*b);
}
Recursion
fre: factorial
int factorial(int);
void main()
{
int n,fact;
printf("enter n value");
scanf("%d",&n);
fact=factorial(n);
printf("fact=%d",fact);
getch();
}
int factorial(int n)
{
int fact;
if(n==1)
{
return 1;
}
else
{
fact=n*factorial(n-1);
}
return fact;
}
Fre1: fibonacci
void fibonacci(int);
void main()
{
int n;
printf("enter n value");
scanf("%d",&n);
printf("%d\t%d",0,1);
fibonacci(n);
getch();
}
void fibonacci(int n)
{
static int f0=0,f1=1,f2;
if(n>0)
{
f2=f0+f1;
printf("\t%d",f2);
f0=f1;
f1=f2;
fibonacci(n-1);
}
}
NCR
int factorial(int);
void main()
{
int n,r,f,f1,f2,ncr;
clrscr();
printf("enter n and r value");
scanf("%d%d",&n,&r);
f=factorial(n);
f1=factorial(r);
f2=factorial(n-r);
ncr=f/(f2*f1);
printf("ncr=%d",ncr);
getch();
}
int factorial(int n)
{
int fact,i;
if(n==1)
return 1;
else
fact=n*factorial(n-1);
return fact;
}
Advantages of recursion
Recursive programs are effectively
Recursive programs run within acceptable time and
space limits
Recursive programs used some conditions
Recursive programs are shorter and more
understandable
Functions using arrays
(or)
passing an array to a function
float mean(int[],int);
float sd(int[],int,float);
void main()
{
int a[10],i,n;
float x,y;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
x=mean(a,n);
y=sd(a,n,x);
printf("x=%f\n y=%f",x,y);
getch();
}
float mean(int a[],int n)
{
int i,sum=0;
float res;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("Sum : %d\n",sum);
res=(float)sum/n;
return res;
}
float sd(int a[],int n,float x)
{
int i;
float v=0,z;
for(i=0;i<n;i++)
{
v=v+((a[i]-x)*(a[i]-x));
}
z=sqrt(v/n);
return z;
}
Storage classes in C
https://www.youtube.com/watch?
v=lRRPO4C0suQ