unit-iii
unit-iii
unit-iii
Arrays – Initialization – Declaration – One dimensional and Two dimensional arrays. String-
String operations – String Arrays. Simple programs- sorting- searching – matrix operations.
ARRAYS
Definition :
Array is a collection of same data type elements under the same variable name
referenced by index number. Arrays allow you to store group of data of a single type.
Characteristics:
An array is a derived data type. It is used to represent a collection of elements of the
same data type.
The elements can be access with base address (index) and the subscripts define the
position of the element.
In array the elements are stored in continuous memory location, the starting memory
location is represented by the base address of the array
It is easier to refer the array elements by simply incrementing the value of the
subscript
Array is a linear and homogeneous data structure
Ex: int a[5];
It tells the compiler that ‘a’ is an integer type of array and can store 5 integers.
Advantages:
An array is a derived data type. It is used to represent a collection of elements of the
same data type.
The elements can be access with base address (index) and the subscripts define the
position of the element.
In array the elements are stored in continuous memory location, the starting memory
location is represented by the base address of the array
It is easier to refer the array elements by simply incrementing the value of the
subscript
Initializing an array:
The values can be initialized to an array when they are declared like ordinary variables
otherwise they hold garbage values.
The array can be initialized in the following two types:
o At compile time(static Initialization)
o At run time(Dynamic Initialization)
static Initialization:
Syntax:
Data_type Array_name[size] = {list of values}
The list of values must be separated by commas.
Ex:
Int marks[5]= {70,80,98,35,56};
70 marks[0]
80 marks[1]
98 marks[2]
35 marks[3]
56 marks[4]
int age[]={2,4,34,3,4};
The elements can be used like ordinary variables
Character array can also initialized in similar manner
Ex:
char name[10]={‘R’,’A’,’J’};
The above statements declare the name variables as an array of character with the string
“LAK”.
Dynamic Initialization:
int sum[5];
for(int i=0;i<5;i++)
sum[i]=i;
Like the array can also be initialized by reading data from the user.
Ex:
int sum[5];
for(int i=0;i<5;i++)
scanf(“%d”,&a[i]);
Example: Program to accept 5 numbers and print whether the number is even or odd.
#include <stdio.h>
#include<conio.h>
GE 6151 Unit – III 4
void main()
{
int array[5], I;
clrscr();
printf("Enter the elements of the array \n");
for (i = 0; i <5; i++)
{
scanf("%d", &array[i]);
}
printf("Even numbers in the array are - ");
for (i = 0; i < 5; i++)
{
if (array[i] % 2 == 0)
{
printf("%d \t", array[i]);
}
}
printf("\n Odd numbers in the array are -");
for (i = 0; i <5; i++)
{
if (array[i] % 2 != 0)
{
printf("%d \t", array[i]);
}
}
getch();
}
Two dimensional array:
Two dimensional arrays are used in situation where a table of values need to be
stored in an array.
These can be defined in the same manner as in one dimensional array except a
separate pair of square brackets are required for each subscript
Syntax:
Data_type Array_name[row_size][column_size];
Two dimensional arrays are stored in a row column matrix where the left index
indicated the row and the right index indicated the column.
Example:
Output:
1[2000] 2[2002] 3[2004]
4[2006] 5[2006] 6[2007]
7[2009] 8[2011] 9[2013]
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
printf("%d\t", multiply[c][d]);
}
printf("\n");
}
}
getch();
}
Bubble Sort:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,a[20],temp;
clrscr( );
printf(“Sorting the elements using bubble sort\n”);
printf(“Enter the size of the array \n”);
scanf(“%d”,&n);
printf(“Enter the elements of the array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“The elements sorted using bubble sort are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
getch( );
}
String Declaration:
Syntax:
Datatype variable_name [size];
Ex:
char name[30];
String Initialization:
Syntax:
Datatype variable_name [size] = string;
Ex:
char name[]={‘r’,’a’,’j’,’a’,’\0’};
GE 6151 Unit – III 12
where the \0 is specified at the end of string
‘C’ provides another method for initialing string
char name[]=”raja”;
Here \0 is not necessary
Reading & writing String:
The %s control string can be used in scanf() function to read a string from the terminal. and
same may be used to write string to the terminal in printf() function.
Ex:
char a[10];
scanf(“%s”, a);
printf(“%s”, a);
1) Copying String
Syntax: strcpy(destination,source);
Here, source and destination are both the name of the string.
This statement, copies the content of string source to the content of string
destination.
This function will replace the existing value of destination with source.
Example:
char string1[15]=”Hello”;
char string2[15]=”World”;
strcpy(string1,string2);
printf(“%s%s”,string1,string2);
output:
World World
2) Comparing String:
Case Sensitive:
This function compares the value from string2 with string1.
If both the string1 and string2 are exactly the same then the function will return
zero or else it will return some positive or negative value.
For the above example the function will return negative of positive value.
Here string1 and string2 will not change.
Syntax:
GE 6151 Unit – III 13
strcmp(string1,string2);
Example:
int n;
char string1[15]=”Hello”;
char string2[15]=”Hello”;
n=strcmp(string1,string2);
printf(“%n”,n);
Output:
0
Non-Case Sensitive:
Syntax: stricmp(string1, string2);
Example:
int n;
char string1[15]=”Hello”;
char string2[15]=”hello”;
n=stricmp(string1,string2);
printf(“%n”,n);
Output:
0
3) Concatenation String
strcat(string1,string2);
This function is used to join two strings.
It concatenates source string at the end of destination string.
Here String 2 is concatenate with String1.
Example:
char string1[15]=”Hello”;
char string2[15]=”World”;
n=strcat(string1,string2);
printf(“%s”,string1);
Output:
HelloWorld
strncat(string1,string2,No_of_characters);
This function is used to join two strings.
It concatenates portion of source string at the end of destination string.
Here String 2 is concatenate with String1.
Example:
char string1[15]=”Hello”;
char string2[15]=”World”;
4) Copying String
strcpy(string1,string2);
This function copy’s the contents of one string2 into another string1.
Example:
char string1[15]=”Hello”;
char string2[15]=”World”;
n=strcpy(string1,string2);
printf(“%s”,str1);
Output:
World
strncpy(string1,string2,no_of_characters);
This function copies portion of contents of one string2 into another string1.
Example:
char string1[15]=””;
char string2[15]=”Hello World”;
n=strcpy(string1,string2,5);
printf(“%s”,string1);
Output:
Hello
8) Duplicate String:
strdup( ) function in C duplicates the given string
Syntax:
string2=strdup(string1);
Example:
char string1=”Hello”;
char string2;
string2=strdup(string1);
printf(“%s”,string2);
Output:
Hello
9) Find a character in string (strrchr())
strchr( ) function returns pointer to the last occurrence of the character in a
given string
Syntax:
strrchr(String,chracter);
Example:
char string1[15]=”Hello World”;
printf(strrchr(string1,’l’));
GE 6151 Unit – III 16
Output:
10
Simple Programs:
Binary Search
#include <stdio.h>
void main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
Insertion Sort:
#include <stdio.h>
void main()
{
Selection Sort:
#include <stdio.h>
void main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
Quick Sort:
#include<stdio.h>
void quicksort(int [10],int,int);
void main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
}
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}