C_Unit II_Notes (1)
C_Unit II_Notes (1)
UNIT II
ARRAYS
Definition:
An array is a derived data type. It is a collection of data elements of similar
data types. The data elements are stored in contiguous memory location.
Syntax :
datatype arrayname[size]
int a[10];
where
int is the data type of the elements.
a is the array name.
[10] is the index which denotes the array contains 10 data elements.
ARRAY INITIALIZATION:
The array can be initialized with elements while declaring the array.
Example:
int a[5]={1,2,3,4,5}
Here, 5 data elements are stored in the array called “a”. The array elements
are stored sequentially in separate locations.
The array elements are called as below:
A[0] refers to 1st element 1
A[1] refers to 1st element 2
A[2] refers to 1st element 3
A[3] refers to 1st element 4
1
A[4] refers to 1st element 5
CHARACTERISTICS OF AN ARRAY:
1. All the data elements share the same name, and they are distinguished
from one another with the help of an element number.
2. Any particular element of an array can be modified separately without
disturbing other elements.
3. The element number in an array plays major role for calling each
element.
CLASSIFICATION OF ARRAY:
1. One-dimensional Array.
2. Two dimensional Array.
1. One-Dimensional Array:
The array elements are arranged in rows or columns. The data are stored
in continuous memory location.
Example:
int a[5]={1,2,3,4,5}
Example Program:
#include<stdio.h>
void main()
{
int a[10],n,i;
clrscr();
2
printf("\nEnter the size of the array");
scanf("%d",&n);
printf("\nEnter the elements into the array one by one");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
OUTPUT:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[25],i,n,max;
clrscr();
printf("\nEnter the no. of elements in the array");
scanf("%d",&n);
printf("\nEnter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("\nThe largest element is %d",max);
getch();
}
output:
Enter the no. of elements in the array4
Enter the elements in the array
6
7
5
45
The largest element is 45
4
SEARCHING AN ELEMENT IN AN ARRAY
Linear search
#include <stdio.h>
#include <conio.h>
void main()
{
int a[25],i,j,n,x;
clrscr();
printf("\nEnter the no. of elements in the array");
scanf("%d",&n);
printf("\nEnter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to be searched in the array");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
printf("\nThe element is found");
break;
}
}
if(i==n)
{
printf("\nThe element is not found");
}
getch();
}
OUTPUT:
Enter the no. of elements in the array 4
Enter the elements in the array
2
1
4
5
Enter the element to be searched in the array 2
The element is found
5
Binary Search:
#include<stdio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid])
{
flag=1;
break;
}
else if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
Output:
6
INSERTING AN ELEMENT INTO AN ARRAY
#include <stdio.h>
void main()
{
int a[30],i,n,x,p;
clrscr();
printf("\nEnter the size of the array");
scanf("%d",&n);
printf("\nenter the elements into the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be inserted");
scanf("%d",&x);
printf("\nEnter the position in which the element to be inserted");
scanf("%d",&p);
for(i=n-1;i>=p;i--)
a[i+1]=a[i];
a[p]=x;
printf("\nThe elements are \n");
for(i=0;i<=n;i++)
printf("%d\t",a[i]);
getch();
}
OUTPUT:
Enter the size of the array 5
3 4 5 5 2 1
SORTING OF AN ARRAY
#include <stdio.h>
#include <conio.h>
void main()
{
int a[25],i,j,n,temp;
clrscr();
printf("\nEnter the no. of elements in the array");
7
scanf("%d",&n);
printf("\nEnter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
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;
}
}
}
printf("\nThe sorted array is \n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
OUTPUT:
8
Bubble Sort:
#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i< n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < n - 1; i++)
{
for (j= 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[ j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
}
Output:
9
2.Two-Dimensional Array:
The array elements are thought to be stored in the form of rows and columns.
Syntax :
datatype arrayname[row size][column size];
int a[3][3];
Diagrammatic representation:
Example Program:
#include<stdio.h>
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\nEnter the size of the array");
scanf("%d",&n);
printf("\nEnter the elements into the array one by one");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe array elements are\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
10
getch();
}
OUTPUT:
MATRIX ADDITION
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n;
clrscr();
printf("\nEnter the no. of rows in the matrix A");
scanf("%d",&m);
printf("\nEnter the no. of columns in the matrix A");
scanf("%d",&n);
printf("\nEnter the elements in the matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements in the matrix B");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
11
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nThe Resultant matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
12
MATRIX MULTIPLICATION
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p;
clrscr();
printf("\nEnter the no. of rows in the matrix A");
scanf("%d",&m);
printf("\nEnter the no. of columns in the matrix A");
scanf("%d",&n);
printf("\nEnter the no. of columns in the matrix B");
scanf("%d",&p);
printf("\nEnter the elements in the matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements in the matrix B");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nThe Resultant matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
13
OUTPUT:
Enter the no. of rows in the matrix A2
TRANSPOSE OF A MATRIX:
#include <stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n;
Output:
Enter the order of the matrix
33
Enter the coefiicients of the matrix
379
275
634
The given matrix is
379
275
634
Transpose of matrix is
326
773
954
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],i,j,m,n,sum=0;
clrscr();
printf("\nEnter the no. of rows in the matrix A");
scanf("%d",&m);
printf("\nEnter the no. of columns in the matrix A");
scanf("%d",&n);
printf("\nEnter the elements in the matrix A");
15
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe Addition of diagonal elements in the matrix is\n");
for(i=0;i<m;i++)
{
sum=sum+a[i][i];
}
printf("%d",sum);
getch();
}
OUTPUT:
int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}} };
Three-dimensional:
#include <stdio.h>
void main()
{
int test[2][3][2];
16
printf("Enter 12 values: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
}
}
}
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
17
CHARACTER ARRAY:
The data elements stored in the array belong to character data type.
Hence the name, character array.
18
STRING HANDLING:
char name[]=”India”;
The C compiler inserts the NULL character automatically at the end of the
string. So, initialization of NULL character is not essential.
Display of strings with different formats:
Char name[]=”computer”;
/*Displaying String*/
printf("%s",nickname);
return 0;
}
19
Read & Write Strings in C using gets() and puts() functions
#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];
puts(nickname);
return 0;
}
20
9. strncat( ) Appends a string to the end of another
string up to n
Syntax:
strlen(string)
Example of strlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome";
printf("Length of string str1: %d", strlen(str1));
return 0;
}
Output:
Length of string str1: 7
Syntax:
strcpy(destination, source);
Example of strcpy:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30];
char s2[30] = "Good Day!";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("%s", s1);
return 0;
}
Output:
Good Day!
Example of strcmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Goodday";
char s2[20] = "Welcome";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}
else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
string 1 and 2 are different
Syntax:
strcat(str1,str2)
Example of strcat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld
Example of strrev:
22
#include<stdio.h>
#include<string.h>
int main()
{
char name[30] = "Hello";
return 0;
}
Output:
String before strrev( ) : Hello
String after strrev( ) : olleH
Syntax
strlwr(string)
Example of strlwr:
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "MODIFY This String To LOwer";
printf("%s\n",strlwr (str));
return 0;
}
Output:
modify this string to lower
Syntax
strupr(string)
Example of stuprr:
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Modify This String To Upper";
23
printf("%s\n",strupr(str));
return 0;
}
Output:
MODIFY THIS STRING TO UPPER
void str_len()
24
{
int n;
fflush(stdin);
printf("\n Enter the string");
gets(a);
n=strlen(a);
printf("\nThe length of the string is %d",n);
}
void str_comp()
{
fflush(stdin);
printf("\n Enter the I string");
gets(a);
printf("\nEnter the II String");
gets(b);
if(strcmp(a,b)==0)
printf("\n The two strings are identical");
else
printf("\nThe strings are different");
}
void str_cpy()
{
fflush(stdin);
printf("\n Enter the string");
gets(a);
strcpy(b,a);
printf("\nThe copied string :");
puts(b);
}
void str_con()
{
fflush(stdin);
printf("\n Enter the I string");
gets(a);
printf("\nEnter the II String");
gets(b);
strcat(a,b);
printf("\nThe concatenated string is : ");
puts(a);
}
OUTPUT:
25
1. finding the length of the string
2. string comparison
3. string copy
4. String concatenate
Enter ur choice 1
#include <stdio.h>
#include <conio.h>
#include <string.h>
void str_len();
void str_con();
void str_cpy();
char a[25],b[25],c[50];
void main()
{
int choice;
clrscr();
printf("1. finding the length of the string");
printf("\n2. string copy");
printf("\n3. String concatenate");
printf("\nEnter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
str_len();
break;
case 2:
str_cpy();
break;
case 3:
str_con();
break;
default:
exit(1);
}
getch();
}
26
void str_len()
{
int n=0,i;
fflush(stdin);
printf("\n Enter the string");
gets(a);
for(i=0;a[i]!='\0';i++)
n++;
printf("\nThe length of the string is %d",n);
}
void str_cpy()
{
int i;
fflush(stdin);
printf("\n Enter the string");
gets(a);
for(i=0;a[i]!='\0';i++)
b[i]=a[i];
printf("\nThe copied string :");
puts(b);
}
void str_con()
{
int n=0,i;
fflush(stdin);
printf("\n Enter the I string");
gets(a);
printf("\nEnter the II String");
gets(b);
for(i=0;a[i]!='\0';i++)
{
n++;
}
for(i=0;b[i]!='\0';i++)
{
a[n++]=b[i];
}
printf("\nThe concatenated string is : ");
puts(a);
}
27
OUTPUT:
Char a[2][10];
Here, The array accommodates 2 strings with 9 characters long. The last
location is allotted for storing the NULL character.
Example Program;
#include<stdio.h>
void main()
{
char a[4][10],i,j,n;
clrscr();
printf("\nEnter the number of names to be stored in the array");
scanf("%d",&n);
printf("\nEnter the names one by one\n");
for(i=0;i<n;i++)
{
fflush(stdin);
gets(a[i]);
}
printf("\nThe names are\n");
for(i=0;i<n;i++)
{
puts(a[i]);
printf("\n");
28
}
getch();
}
OUTPUT:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[25][25],i,j,n,temp[20];
clrscr();
printf("\nEnter the no. of strings in the array");
scanf("%d",&n);
printf("\nEnter the strings in the array\n");
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
29
}
printf("\nThe sorted strings in the array is \n");
for(i=0;i<n;i++)
{
printf("%s\n",a[i]);
}
getch();
}
OUTPUT:
30
}
}
************
31