Array in C
Array in C
INTRODUCTION TO ARRAYS:
ARRAY DEFINITION
An array is a data structure that is used for the storage of homogenous data i.e., data
of the same type. An array is a collection of elements of same data type.
The data type of an element is called element type. The individual elements of an
array are distinguished and are referred to or accessed according to their positions in an
array.
The position of an element in an array is specified with an integer value known as
index or subscript. The array index in C starts with 0 i.e. index of the first element of an
array is 0. The memory space required by an array can be computed as size of element type
& number of elements in an array. Arrays are always stored in contiguous memory
location.
Classification
1. Single dimensional array/one dimensional array
2. Two dimensional array
3. Multi-dimensional array
1. Declaration of Array
The general form of a single dimensional array declaration is
type variable_name [size];
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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];
Consider array elements to be assigned as follows
number[0] = 12 number[1] = 22 number[2] = 33
number[3] = 44 number[4] = 56
This would cause the array number to be store the value as shown below
will initialize the first three elements to 1, 3, 5 and the remaining two elements to zero.
The size may be omitted. In such cases, the compiler allocated enough space for all
initialized elements.
int a[] = { 1, 3, 5, 6 };
will declare the a array to contain four elements with initial values 1, 3, 5 and 6.
3. Reading Elements of Single – Dimensional Array
An iteration statement is used for reading the elements of a single – dimensional
array
General Form
for ( i=0; i< size of array ; i++)
scanf(“control string”, &array_name[i]);
where i is the index variable. We can use any integer variable as index variable.
Example
int a[5];
The elements of the array can be read using the following statement
for ( i=0; i< 5 ; i++)
scanf(“%d”, &a[i]);
printf(“%d”,a[2]);
will print the element of the array a at position 2 i.e. 5.
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
number[3] = 44 number[4] = 56
This would cause the array number to be store the value as shown below
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 }
};
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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.
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
{
if(a[i]>big)
big=a[i];
}
printf("\nThe Biggest Element = %d",big);
}
Sample Input / Output
Enter the number Element in the array
6
Enter the Elements in the Array
125 56 254 35 485 55
The Biggest Element = 485
25 653 4 58 96 5
The Smallest Element = 4
/*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)
{
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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]);
}
/*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++)
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
{
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]);
}
for(i=0;i<n;i++)
{
if(a[i]==x)
{
f=1;
printf("\nThe Element Found at %d",i);
break;
}
}
if(f==0)
printf("\nThe Element Not Found in the List");
}
for(i=0;i<n;i++)
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
{
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])
{
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++)
{
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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++)
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");
}
}
1 2 3 5
4 6 2 8
The Given Matrix is
1 2 3 5
4 6 2 8
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
for(j=0;j<c2;j++)
printf("\t%d",B[i][j]);
printf("\n");
}
}
scanf("%d%d",&r2,&c2);
if(c2==r2)
printf("\nMatrix Muliplication is not Possible");
else
{
printf("\nEnter the Matrix A");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
printf("\nEnter the Matrix B");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&B[i][j]);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
C[i][j]=0;
for(k=0;k<r2;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}
}
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");
}
}
Sample Input / Output
Enter the Number of Rows and Columns of Matrix 1
24
Enter the Number of Rows and Columns of Matrix 2
42
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
#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]);
}
}
printf("\n\n---------- Matrix A is --------------\n");
for(i=1;i<=m;i++)
{
printf("\n");
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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(j=1,d=0;j<=x;j++)
{
d=d+(f[1][j]*c[j]);
}
return(d);
}
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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 B A B U
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.
}
Sample Input / Output
Enter the String
Welcome
The Given String is Welcome
Syntax:
var=strlen(string);
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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");
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
scanf("%s",s1);
strcpy(s2,s1);
printf("\nThe Copied String is %s",s2);
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();
}
Morning
The Concatenated String is GoodMorning
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);
}
The strrev() function is used to reverse a string. This function takes only one
argument and return one argument.
Syntax:
strrev(string);
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();
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
i++;
else
{
f=1;
break;
}
}
if(f==1)
printf("\nThe two Strings are not Equal");
else
printf("\nThe two Strings are Equal");
}
/*Palindrome or not*/
PROGRAM:
# include <stdio.h>
# include <string.h>
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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] ;
}
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:
{
if(strcmp(str[j], str[j + 1]) > 0)
{
strcpy(temp, str[j]) ;
strcpy(str[j], str[j + 1]) ;
strcpy(str[j + 1], temp) ;
}
}
}
printf("\nThe sorted order of strings are : \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%s \n", str[i]) ;
}
}
OUTPUT:
Enter the number of strings : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish
#include <stdio.h>
void main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
}
Output:
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
/*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]);
}
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]);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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
/*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)
{
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
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
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]);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
OUTPUT:
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE
PREPARED BY
R.GNANAVEL AP/CSE