Unit2 231CSC201T
Unit2 231CSC201T
1. Declaration of Array
The general form of a single dimensional array declaration is
type variable_name [size];
A single dimensional array declaration consists of a type specifier i.e. element type,
an identifier i.e. name of array and a size specifier i.e. number of elements in the array
enclosed with in square brackets.
e.g. int number[5];
2
We can also initialize the above two dimensional array in the form of a matrix as
shown below
int table[2][3] = {
{ 1, 3, 5},
{ 7, 8, 2 }
};
When the array is completely initialized with all values we need not specify the size
of the first dimension. That is
int table[ ][3] = {
{ 1, 3, 5},
{ 7, 8, 2 }
};
is permitted.
6
/*Program to Separate the given list into Positive and Negative Numbers*/
#include<stdio.h>
void main()
{
int a[20],i,n,pos[20],neg[20],pc,nc;
pc=0;
nc=0;
printf("\nEnter the number of Elements");
scanf("%d",&n);
printf("\nEnter the Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>=0)
{
pos[pc]=a[i];
pc=pc+1;
}
else
{
neg[nc]=a[i];
nc=nc+1;
}
}
printf("\nPositive Numbers in the array\n");
for(i=0;i<pc;i++)
printf("\t%d",pos[i]);
printf("\nNegative Numbers in the array\n");
for(i=0;i<nc;i++)
printf("\t%d",neg[i]);
}
11
/*Program to Separate the given list into Odd and Even Numbers*/
#include<stdio.h>
void main()
{
int a[20],i,n,odd[20],even[20],oc,ec;
oc=0;
ec=0;
printf("\nEnter the number of Elements");
scanf("%d",&n);
printf("\nEnter the Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
even[ec]=a[i];
ec=ec+1;
}
else
{
odd[oc]=a[i];
oc=oc+1;
}
}
printf("\nEven Numbers in the array");
for(i=0;i<ec;i++)
printf("\t%d",even[i]);
printf("\nOdd Numbers in the array");
for(i=0;i<oc;i++)
printf("\t%d",odd[i]);
}
12
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
14
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
Example Program: Matrix Operations (Addition, Scaling, Determinant and
Transpose)
/*Program to Read and Print Two - Dimensional Array*/
#include<stdio.h>
void main()
{
int A[5][5],i,j,r,c;
printf("\nEnter the number of Rows and Columns");
scanf("%d%d",&r,&c);
printf("\nEnter the Matrix");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
15
scanf("%d",&A[i][j]);
printf("\nThe Given Matrix is ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",A[i][j]);
printf("\n");
}
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nThe Resultant Matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
}
Sample Input / Output
Enter the number of Rows and Columns
2 4
Enter the Matrix A
1 2 3 4
5 6 7 8
Enter the Matrix B
6 7 8 3
4 6 2 3
The Resultant Matrix is
7 9 11 7
9 12 9 11
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&B[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[i][j]=A[i][j]-B[i][j];
printf("\nThe Resultant Matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
}
Sample Input / Output
Enter the number of Rows and Columns
2 4
Enter the Matrix A
8 7 9 7
6 8 5 4
Enter the Matrix B
1 2 3 4
4 5 3 1
The Resultant Matrix is
7 5 6 3
2 3 2 3
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
r2=c1;
c2=r1;
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
B[j][i]=A[i][j];
printf("\nThe Transpose Matrix is\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",B[i][j]);
printf("\n");
}
}
}
Printf(“\nThe Resultant Matrix is\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
20
#include<stdio.h>
int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
int i,j;
printf("\n\nEnter order of matrix : ");
scanf("%d",&m);
printf("\nEnter the elements of matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
21
}
printf("\n\n---------- Matrix A is -------------- \n");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=m;j++)
{
printf("\t%d \t",a[i][j]);
}
}
printf("\n \n");
printf("\n Determinant of Matrix A is %d .",determinant(a,m));
}
}
for(t=1,pr=1;t<=(1+j);t++)
pr=(-1)*pr;
c[j]=pr*determinant(b,x-1);
}
for(j=1,d=0;j<=x;j++)
{
d=d+(f[1][j]*c[j]);
}
return(d);
}
}
23
STRINGS
STRING MANIPULATION
In „C‟ language the group of character, digits, and symbols enclosed within
quotation marks are called as string otherwise strings are array of characters. Null character
(„\0‟) is used to mark the end of the string.
Example:
char name[]={„B‟,‟A‟,‟B‟,‟U‟,‟\0‟};
Each character is stored in one byte of memory and successive characters of the
string are stored in successive byte.
Memory map of string
String
Address 5001 5002 5003 5004
INITIALIZATION OF STRING
The string can be initialized as follows.
char name[]=”BABU”;
The characters of the string are enclosed within a pair of double quotes.
The initialization of NULL character is not essential because the C compiler inserts
the NULL (\0) character automatically at the end of the string.
Syntax:
var=strlen(string);
25
Example:
/*Program to Find Length of a String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20];
int n;
clrscr();
printf("\nEnter the String\n");
scanf("%s",s1);
n=strlen(s1);
printf("\nThe Length of the Given String is %d",n);
getch();
}
Syntax:
strcpy(string1,string2);
i.e., The contents of string2 is assigned to the contents of string1.
Example:
/*Program to Copy a String into another String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
strcpy(s2,s1);
printf("\nThe Copied String is %s",s2);
26
getch();
}
Syntax:
strcat(string1,string2);
When the above strcat() function is executed, string2 is combined with string1 and it
removes the null character (\0) of string1 and places string2 from there.
Example:
/*Program to Concate two Strings*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String 2\n");
scanf("%s",s2);
strcat(s1,s2);
printf("\nThe Concatenated String is %s",s1);
getch();
}
Syntax:
Strcmp(string1,string2);
Example:
/*Program to Compare two Strings*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String 2\n");
scanf("%s",s2);
if(strcmp(s1,s2)>0)
printf("\n%s Comes Before %s",s2,s1);
else if(strcmp(s1,s2)<0)
printf("\n%s Comes Before %s",s1,s2);
else
printf("\n%s Equals to %s",s1,s2);
}
Syntax:
strrev(string);
28
Example:
/*Program to Find Reverse of a String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20];
clrscr();
printf("\nEnter the String 1");
scanf("%s",s1);
strrev(s1);
printf("\nReverse string is %s",s1);
getch();
/*Palindrome or not*/
PROGRAM:
# include <stdio.h>
# include <string.h>
void main()
{
char str[20], rev[20] ;
int i, j, l ;
printf("Enter a string : ") ;
scanf("%s", str) ;
for(l = 0 ; str[l] != '\0' ; l++) ;
for(i = l - 1, j = 0 ; i >= 0 ; i--, j++)
{
rev[j] = str[i] ;
}
33
rev[j] = '\0' ;
if(stricmp(str, rev) == 0)
printf("\nThe given string is a palindrome") ;
else
printf("\nThe given string is not a palindrome") ;
}
OUTPUT:
Enter a string : madam
The given string is a palindrome
OUTPUT:
Enter the number of strings : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish
void main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
}
Output:
/*SELECTION SORT*/
Program:
#include<stdio.h>
void main()
{
inti,j,k,n,c,a[20],min;
printf("$$$ SELECTION SORT $$$");
printf("\nEnter the array limit:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
36
}
printf("Passes of selection sort:\n");
for(i=0;i<n-1;i++)
{
min=i;;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
if(i!=min)
{
k=a[i];
a[i]=a[min];
a[min]=k;
}
printf("\nPass=%d,\t",i+1);
for(c=0;c<n;c++)
{
printf("%d\t",a[c]);
}
}
printf("\nThe sorted elements are:\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
$$$ SELECTION SORT $$$
Enter the array limit: 6
Enter array elements: 23 78 45 8 32 56
Passes of selection sort:
Pass=1, 8 78 45 23 32 56
Pass=2, 8 23 45 78 32 56
Pass=3, 8 23 32 78 45 56
Pass=4, 8 23 32 45 78 56
Pass=5, 8 23 32 45 56 78
The sorted elements are: 8 23 32 45 56 78
37
/*LINEAR SEARCH*/
Program:
#include<stdio.h>
void main()
{
int a[10],i,n,key,f=0;
printf("\n\n LINEAR SEARCH \n\n");
printf("Enter the array limit:\t");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:\t");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element found\n");
f=1;
break;
}
}
if(f==0)
{
printf("Element not found\n");
}
}
OUTPUT:
LINEAR SEARCH
Enter the array limit: 5
Enter array elements:
1
3
5
7
9
Enter the element to be searched: 9
Element found
38
LINEAR SEARCH
Enter the array limit: 5
Enter array elements:
2
4
6
8
10
Enter the element to be searched: 9
Element not found
/*BINARY SEARCH*/
PROGRAM:
#include<stdio.h>
void main()
{
int c, first, last, middle, n, f=0, search, array[100];
printf("\n\n\n Binary Search using Array \n\n\n");
printf("Enter number of elements:\t");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
{
scanf("%d",&array[c]);
}
printf("Enter value to find:\t");
scanf("%d",&search);
first = 0;
last = n - 1;
while( first <= last )
{
middle = (first+last)/2;
if ( search < array[middle] )
{
last = middle - 1;
}
else if( search > array[middle] )
{
first= middle + 1;
}
else
39
{
printf("%d found at location %d.\n", search, middle+1);
f=1;
break;
}
}
if ( f==0 )
{
printf("Not found! %d is not present in the list.\n", search);
}
}
OUTPUT: