Академический Документы
Профессиональный Документы
Культура Документы
Arrays 2
1
Declaration 2
2
Initialization 3
3
String 14
5
String operations 18
6
String Arrays 19
7
Simple programs 22
8
sorting 24
9
searching 26
10
matrix operations 30
11
1
UNIT III ARRAYS AND STRINGS
3.1 Array
Array is a collection of similar data items, which are stored under a common
name. A value in an array is identified by Index.
Array is a derived data type. It is very effective when working with large number
of data of same data type or different data types. Array reduces the programming size or
coding.
All elements of an array share the same name, and they are distinguished form
one another with help of an element number.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
3.1.1 Declaration
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows:
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
int age[4];
age[0]=14;
age[1]=13;
age[2]=15;
age[3]=17;
It is a linear array when working with only one set of data at a time whose
elements are accessed with a single index.
3
It is similar to declaring any other primary data type except, the number element
must specified for an array in bracket [ ].
int a[5];
float b[10];
double c[6];
char s[4];
The arrays declared as above. The numbers inside the bracket is the number of elements
for that variable. Therefore a can store 5 int values, b 10 float values, c 6 double values,
and s 4 char values.
All the array elements are assigned values. The last element for the character
arrays must be provided for NULL character.
4
Reading & assigning an array
#include >stdio.h>
Int main(void)
{
int x[5],i;
for (i=0; i<5; ++i)
{
scanf(“%d”, x[i]);
}
Printh(“the elements are: \n”);
for (i=0; i<5; ++i)
{
printf(“%d”, x[i]);
}
Output:
1
2
3
4
5
5
the elements are:
1
2
3
4
5
Ex: 2
6
printf("\nthe sum of negative numbers are %d\n",ne);
getch();
}
Output:
enter the array size
-4
-5
If the number is greater than zero, normally we say that it is positive otherwise negative
number.
Similarly we also find how many odd numbers & even numbers in a set,
7
int a[10],i,o=0,e=0,n;
clrscr();
printf("enter the size of the array:");
scanf("%d",&n);
printf("enter the elements \n");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n;i++)
{
if(a[i] % 2 ==0)
o++;
else
e++;
}
printf("\n the sum of odd numbers are %d\n",o);
printf("\n the sum of even numbers are %d\n",e);
getch();
}
Output:
Enter the size of the array : 5
10
20
30
8
The sum of even numbers are : 60
Two dimensional arrays are stored in a row-column, where one index refers the
row and the second index refers column.
In this method the value in the entire array element will be zero.
0 0 0
Row 0 0 0 0
Row 0 0 0 1
Row 2
9
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("enter the A matrix");
for(i=0;i<=2;i++)
for(j=0;j<=2;i++)
scanf("%d",&a[i][j]);
printf("enter the B matrix");
for(i=0;i<=2;i++)
for(j=0;j<=2;i++)
scanf("%d",&b[i][j]);
printf("sum of matrices");
for(i=0;i<=2;i++)
for(j=0;j<=2;i++)
c[i][j]= a[i][j]+b[i][j];
printf(“Addition of two matrices\n”)’;
for(i=0;i<=2;i++)
for(j=0;j<=2;i++)
printf("%d",c[i][j]);
}
Output:
10
3.1.3. Multidimensional Array
The dimension with three or more called multidimensional arrays.
Multidimensional Array
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int arr[3][3][3]=
{
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},
};
11
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
getch();
}
Output:
:::3D Array Elements:::
11 12 13
14 15 16
17 18 19
21 22 23
24 25 26
27 28 29
31 32 33
34 35 36
37 38 39
12
3.1.3. Dynamic Array
1. malloc
2. calloc
3. realloc
4.free
1. malloc()
malloc () function is used to allocate space in memory during the execution of the
program. The syntax of this
2.Calloc()
13
3. realloc()
realloc () function modifies the allocated memory size by malloc () and calloc ()
functions to new size.New block is allocated for the full size of reallocation, then copies
the existing data to new block and then frees the old block.
4. free()
free () function frees the allocated memory by malloc (), calloc (), realloc ()
functions and returns the memory to the system.
free (pointer_name);
These files are located in <stdlib.h.> file. It is used in linked lists, stacks, and queues in
the data structures.
Example:
/* Welcome message*/
#include <stdio.h>
int main ()
{
char g[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
14
printf("message: %s\n", g );
return 0;
}
Output:
Hello
‘\0’ is not necessary. C language automatically insert at the end of the string.
Output:
H
E
L
L
O
Hello
15
3.2.1. Built in functions for string handling
16
The C library supports a function that converts a string of digits into their integer
values.
The function takes the form
x = atoi(string)
x = ‘a’;
printf(“%d \n”,x);
Example:1
Output:
Enter in a name in lowercase
madhus
the name in uppercase is : MADHUS
17
Example:2
/* string operations */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
Output:
HelloWorld
Strlen(“HelloWorld”): 10
18
Example:3
Output:
Enter the string to calculate it’s length : saravanan
Length of entered string is = 9
Example:4
19
}
Output:
Enter a string ; abirami
Length of a string : 7
Example:5
/* to concate two strings with out using string function */
#include <stdio.h>
int main()
{
char s1[50], s2[50], i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0'; i++)
for(j=0; s2[j]!='\0'; j++)
s1[i]=s2[j];
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}
Output:
Enter the first string: vijaya
Enter second string: lakshmi
After concatenation : vijayalakshmi
Example:6
/* sorting a word in order */
#include<stdio.h>
20
#include<string.h>
void main()
{
char s[20],s1[20];
int i,j,n,temp;
clrscr();
printf("\n Enter any string : ");
gets(s);
n=strlen(s);
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n The sorted string is :");
for(i=0;i<=n;i++)
printf("%c",a[i]);
getch();
}
21
Output:
/* Matrix Multiplication*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int row1,col1,row2,col2,i,j,k;
clrscr();
printf("Enter the row value of first matrix");
scanf("%d",&row1);
printf("\nEnter the column value of first matrix");
scanf("%d",&col1);
printf("\nEnter the row value of second matrix");
scanf("%d",&row2);
printf("\nEnter the column value of second matrix");
scanf("%d",&col2);
if(row1==col2)
{
printf("Enter the values of I matrix\n");
for(i=1;i<=row1;i++)
{
for(j=1;j<=col1;j++)
{
scanf("%d",&a[i][j]);
22
}
}
printf("Enter the values of II matrix\n");
for(i=1;i<=row2;i++)
{
for(j=1;j<=col2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=row1;i++)
{
for(j=1;j<=col2;j++)
{
c[i][j]=0;
for(k=1;k<=col2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=row1;i++)
{
for(j=1;j<=col2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Multiplication cannot be performed");
getch();
}
23
Output:
/* Sort String */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i,j,n;
char name[10][10],temp[10];
clrscr();
printf("\n Enter the size");
scanf("%d",&n);
printf("Enter the Names :");
for(i=0;i<n;i++)
scanf("%s",&name[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(strcmp(name[i],name[j])>0)
24
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
printf("The sorted Order is.....\n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
getch();
}
Output
25
Program 3 : Write a program to search strings.
/* Linear Search*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,ele,a[100],f=0;
clrscr();
printf("Enter the n limit :");
scanf("%d",&n);
printf("Enter the elements :");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter the element to be searched");
scanf( "%d",&ele);
for(i=1;i<=n;i++)
{
if(a[i]==ele)
f=f+1;
}
if(f!=0)
printf("present");
else
printf("not present");
getch();
}
Output:
26
23
34
45
56
Enter the elements to be searched:12
Present
Enter the element to be searched:78
Not present
27
UNIT III ARRAYS AND STRINGS
/* Binary Search*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100],i,n,c,m,ele,f=1;
clrscr();
printf("Enter the size of the array");
scanf("%d",&n);
printf("\nEnter the elements in ascending order");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the elements to be searched");
scanf("%d",&ele);
low=0;
high=n-1;
while(low<=high)
{
m=(low+high)/2;
if(ele<a[m])
high=m-1;
else if(ele>a[m])
low=m+1;
else
{
printf("\nEntered element %d is in position %d",ele,m+1);
f=2;
break;
}
}
if(f==1)
printf("\nEntered element is not in the array");
28
UNIT III ARRAYS AND STRINGS
getch();
}
Output:
29
UNIT III ARRAYS AND STRINGS
Matrix operations
#include <stdio.h>
void main()
{
int i, j,n,m,c, d, a[5][5],t[5][5];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
scanf("%d",&a[i][j]);
}
}
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
t[i][j] = a[i][j];
}
}
printf("Transpose matrix is :-");
for( i = 0 ; i < n ; i++ )
{
for( j = 0 ; j < m ; j++ )
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
30
UNIT III ARRAYS AND STRINGS
}
Output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of matrix
1
2
1
2
Transpose matrix is :
2
1
2
1
31
UNIT III ARRAYS AND STRINGS
UNIT-3
PART-A
PART-B
1. Describe how to declare two dimensional array using pointers. And write a for loop to store
information and later modification to the declared pointer.
2. Write a C program to arrange the numbers in ascending order.
3. Write a C program to multiply two matrices.
4. Write a C program to search an element using binary search.
5. Write a C program to sort the given names [bubble sort].
6. Explain in detail about various string operations with examples.
7. What is a two dimensional array. Explain its declaration, assignment and various initialization
methods with examples.
32