You are on page 1of 105

Arrays

 One dimensional array


 Two dimensional array
 Multi dimensional array
One dimensional array
Declaration of 1D:
Syntax:
type array name[size];
Example:
int a[5];
float b[5];
char ch[5]
Example:
1.int a[5];

a[0] a[1] a[2] a[3] a[4]


Read operation of 1D
void main()
{
int a[5],i,n;
printf(“enter n value”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
 12345

 a[0] a[1] a[2] a[3] a[4]


1 2 3 4 5
Write operation of 1D
printf(“the array elements are”);
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
o/p: 1 2 3 4 5
Reverse order
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the array elements are\n");
for(i=n-1;i>=0;i--)
{
printf("%d\n",a[i]);
}
To find max and min elements
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
Insert new element
printf("enter the element to be inserted");
scanf("%d",&ele);
printf("enter the position");
scanf("%d",&pos);
m=n-pos+1;
for(i=0;i<=m;i++)
{
a[n-i+2]=a[n-i+1];
}
a[pos]=ele;
printf("the final list is\n");
for(i=0;i<n+1;i++)
{
printf("%d\n",a[i]);
}
Searching techniques

Linear search

printf("enter the element");


scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
f=0;
break;
}
}
if(f==0)
{
printf("the element is found");
}
else
{
printf("the element is not found");
}
Binary search

printf("Enter a number to find :");


scanf("%d", &key);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(key==a[mid])
{
break;
}
else if(key<a[mid])
{
high=mid-1;
mid=(low+high)/2;
}
else if(key>a[mid])
{
low=mid+1;
mid=(low+high)/2;
}
}
if(low==high)
{
printf("the element is found");
}
else
{
printf("element is not found");
}
getch();
}
Sorting techniques

Selection sort: passwise largest element left.

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]

a[2][0] a[2][1] a[2][2]


Read operation of 2D
int a[3][3],i,j,m,n;
clrscr();
printf("enter the m and n vlaues");
scanf("%d%d",&m,&n);
printf("enter the array elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
Write operation of 2D
printf("the array elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
Addition operation of 2D
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
multiplication
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;

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

 In c programming language, arrays are used in wide


range of applications.
 Arrays are used to Store List of values
 Arrays are used to Perform Matrix Operations
 Arrays are used to implement Search Algorithms
 Arrays are used to implement Sorting Algorithms
 Arrays are used to implement Data structures
Array Limitations

 Memory Allocated during Compile time.


 Once Memory is allocated at Compile Time it Cannot
be Changed during Run-time
 In arrays cannot be stored different data types because
arrays can have same data type.
Advantages

 Its convenient way of storing the data of same data type


with same size.
 It allocates contiguous memory locations for its
elements.
 It allows to store the elements in any dimensional array
Disadvantages

1.It allows us to enter only fixed number of elements into


it.
2.We cannot alter the size of the array once array is
declared.
3. We must know in advance that how many elements are
to be stored in array
Strings
 string: Array of characters. terminated with the NULL
character. (‘\0’)
Syntax:
type array name[size];
Example:
char a[5];
a[0] a[1] a[2] a[3] a[4]

 string input/output:
 scanf("%s",a); or gets(a);
printf("%s",a); or puts(a);
 string.h : collection of functions for string
manipulation.
Read and Write operation String
 void main()
 {

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

 They are very much useful when a block of statements


has to be written /executed again and again.
 They are very useful when program size is too large and
complex.
 They are also used to reduce the difficulties during
debugging a program.
 It makes the program more readable and easy to
understood.
 Function declaration:
Type function name(list of arguments)
Function definition:
Syntax:
Type function name(list of aruguments)
{
statements;
}
 Function calling:

Type function name(list of arguments)


{
statements;
}
Type function name(variables)
{
statements;
}
To find biggest no given any three nos
void maximum(int,int,int);
void main()
{
int x,y,z;
clrscr();
printf("enter the values");
scanf("%d%d%d",&x,&y,&z);
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);
}
Categories of functions
 Function with no arguments and no return value
 Function with arguments and no return value
 Function with arguments and with return value
 Function with no arguments and with return value
Function with no arguments and no return value
Syntax:

Function prototype() type fun name()


{ {
------- -------
------- -------
fun name(); -------
-------- -------
--------- }
}
Cf1:

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:

Function prototype; type fun name(arguments)


{ {
------- -------
------- -------
fun name(arguments); -------
-------- -------
--------- }
}
cf2:

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:

Function prototype; type fun name(arguments)


{ {
------- -------
------- -------
fun name(arguments); -------
-------- -------
--------- }
}
Cf3:

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:

Function prototype; type fun name()


{ {
------- -------
------- -------
fun name(); -------
-------- -------
--------- }
}
Cf4:

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

 In C language, each variable has a storage class which


decides the following things:
 Scope : The value of the variable would be available
inside a program.
 Lifetime : The lifetime of a variable is the time period
in which the variable has valid memory
 The following storage classes are most often used in C
programming,
 Automatic variables (auto).
 External variables (extern).
 Static variables (static).
 Register variables (register).
 Automatic variables: (auto)
 A variable declared inside a function.
 Automatic variables can also be called local variables
because they are local to a function. 
 Syntax:
auto datatype variables;
Ex:
void main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf(“%d”,i);
}
printf(“%d”,i);
}
printf(“%d”,i)
} o/p:3 2 1
External or Global variable

 A variable that is declared outside of the main function


is called a Global /extern Variable.
 Global/extern variables are available throughout the
program .
 Syntax: extern datatype variables;
 Ex1:
void fun1();
void fun2()
int number=10; //global variable
void main()
{
printf(“ My value is %d\n", number);
fun1();
fun2();
getch();
}
void fun1()
{
printf(“ My value is %d", number);
}
void fun2()
{
printf(“ My value is %d", number);
}
Ex2:
void fun(int);
extern int a=7;
void main()
{
printf(“%d”,a);
fun();
getch();
}
void fun()
{
a++;
printf(“%d”,a);
}
Static variable

Syntax: static data type variables;


Ex:
void test();
void main()
{
test();
getch();
}
void test()
{
static int a = 0;
a = a + 1;
printf("%d\t",a);
test();
}
Register variables
 Register variables inform the compiler to store the
variable in CPU register instead of memory.
 Register variables have faster accessibility than a
normal variable
 Syntax:
 register int number;
 Ex:
void main()
{
register int i;
for(i=1;i<=5;i++)
{
printf(“%d”,i);
}
}
o/p:1 2 3 4 5
Storage classes:

 https://www.youtube.com/watch?
v=lRRPO4C0suQ

You might also like