0% found this document useful (0 votes)
33 views31 pages

Unit 2-Notes-1

Anna University programming in c unit 2

Uploaded by

amazingfeistel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views31 pages

Unit 2-Notes-1

Anna University programming in c unit 2

Uploaded by

amazingfeistel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Unit – II : ARRAYS AND STRINGS

Introduction to Arrays
Explain in detail about array with an example.
• An array is a collection of similar data items that are stored under a common
name.
• ie., an array is a group of related data items that share a common name.
• A value in an array is identified by index or subscript enclosed in square brackets
with array name.
• The individual data items can be integers, floating point numbers and characters
and so on.
Features of Arrays
• An array is a derived data type.
• The elements can be accessed with base address and the subscript defined for the
position of the element.
• The elements in an array are stored in continuous memory location.
• It is easier to refer the array elements by simply incrementing the value of the
subscript.
Types of Array
• Arrays can be classified into three types. They are,
o One Dimensional Array
o Two Dimensional Array
o Multi Dimensional Array
One Dimensional Array
• The collection of data items stored under a one variable name using only one
subscript; such a variable is called as one dimensional array.
• An array with a single subscript is known as one dimensional array.
Array Declaration
• Arrays are declared in the same manner as ordinary variables except that each
array name must have the size of the array.
• Here data-type is the type specifier that indicates what type of data the declared
array is such as int, float, double or char.
• Array-Name is the name of the declared array and rules of declaring the variable
applies to array name.
• Array-Size defines how many elements the array contains. This is always an
integer.
Syntax
data_type array_name[size or subscript of the array];
Example
int x[3];

Array Initialization
• The values can be initialized to an array, when they are declared like ordinary
variables, otherwise they hold garbage values.
• The arrays can be initialized in two ways,
o At Compile time
o At Run time
At Compile time
• This initialization is done while writing the program itself.
Syntax: data_type array_name[size]={list of values};
Eg: int x[3]={5,3,7};

Example Program
#include<stdio.h>
#include<conio.h>
void main() {
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
printf("\n The value in x[%d] is %c",i,x[i]);getch();}
Output
The value in x[0] is a
The value in x[1] is b
The value in x[2] is c
The value in x[3] is d
The value in x[4] is e
At Run time
• An array can initialized at run time by the program or by taking the input from
the keyboard.
• Generally, the large arrays are declared at run time in the program itself. Such as,
int sum[20];
for (i = 0;i<20;i++)
sum[i] = 1;
Example Program
#include<stdio.h>
#include<conio.h>
void main() {
int x[2], i;
printf("\n Enter the Inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("\n The value in x[%d] is %d",i,x[i]);
getch();
}
Output:
Enter the Inputs: 3 6
The value in x[0] is 3
The value in x[1] is 6

Computing Mean, Median and Mode

#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

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])
{
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]);
}
}

Two Dimensional Arrays


• It is used in situation where a table of values needs to be stored in an array.
• It is similar to one dimensional array except a separate pair of square brackets is
required for each subscript.
• Two dimensional arrays are stored in a row-column matrix, where the left index
indicates the row and the right indicates the column.
Syntax: data_type array_name[row size][column size];
Eg: int x[3][2];

Initializing a Two Dimensional Array


• Like one dimensional array, the values can be initialized to the two dimensional
arrays at the time declaration.
Syntax: data_type array_name[row size][column size] = {List of Values};
Eg: int stud[3][2] = {
{6680, 80},
{6681, 85},
{6682, 87}
};
(Or)
int stud[3][2] = {6680, 80, 6681, 85, 6682, 87};
Example Program (To print two dimensional array row by row)
#include<stdio.h>
#include<conio.h>
void main() {
int i, j;
int x[2][2]={ {1,50},
{2,75}
};
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("\n The value in x[%d][%d] is %d",i,j,x[i][j]);
getch();}
Output:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
Matrix Operations
Matrix Addition
Write a C program to print the sum of two matrices. (Dec/Jan 2014) (Or) Write a
C program to add two matrices. (April/May 2015)
#include<stdio.h>
#include<conio.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n Enter the First matrix:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the Second matrix:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\n The First matrix is\n");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\n The Second matrix is\n");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n The Addition of two matrix is\n");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
getch();
return 0;
}

Output
Enter the element of First Matrix:
2
2
2
2
2
2
2
2
2
Enter the element of Second Matrix:
3
3
3
3
3
3
3
3
3
The First Matrix is
2 2 2
2 2 2
2 2 2
The Second Matrix is
3 3 3
3 3 3
3 3 3
The Addition of Two Matrix is
5 5 5
5 5 5
5 5 5
(Or)
Matrix Multiplication
Write a C program to multiply two matrices. (Nov/Dec 2014) (Nov/Dec 2015)
(May/June 2016)
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
clrscr();
printf("\n Enter First Matrix:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}}
printf("\n Enter Second Matrix:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}}
printf("The First Matrix is: ");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++)
printf(" %d\t", a[i][j]);
}
printf("The Second Matrix is:");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++)
printf(" %d\t", b[i][j]);
}
//Multiplication Logic
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}}
printf("\n The Multiplication of Two Matrix is:");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++)
printf(" %d\t", c[i][j]);
}
getch();
return (0);}
Output:
Enter the element of First Matrix:
2
2
2
2
2
2
2
2
2
Enter the element of Second Matrix:
3
3
3
3
3
3
3
3
3
The First Matrix is
2 2 2
2 2 2
2 2 2
The Second Matrix is
3 3 3
3 3 3
3 3 3
The Multiplication of Two Matrix is
18 18 18
18 18 18
18 18 18
(Or)

Matrix Subtraction
Write a C program to subtract two matrices and display the resultant matrix.
(May/June 2014)
#include<stdio.h>
#include<conio.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n Enter the element of First Matrix:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the element of Second Matrix:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First Matrix is:");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second Matrix is:");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("\n The Subtraction of Two Matrix is:");
for(i=0;i<3;i++) {
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
getch();return 0;}
Output:
Enter the element of First Matrix:
3
5
7
9
11
13
7
5
3
Enter the element of Second Matrix:
2
4
6
8
10
12
6
4
2
The First Matrix is
3 5 7
9 11 13
7 5 3
The Second Matrix is
2 4 6
8 10 12
6 4 2
The Subtraction of Two Matrix is
1 1 1
1 1 1
1 1 1

Transpose of a Matrix
Write a C program to transpose the given matrix.
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10][10], size, i, j, temp;
clrscr();
printf("\n Enter the Size of Matrix :");
scanf("%d", &size);
printf("\n Enter the Values of A:");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
scanf("%d", &arr[i][j]);
}}
printf("\n Given Square Matrix is:");
for (i = 0; i < size; i++) {
printf("\n");
for (j = 0; j < size; j++) {
printf("%d\t", arr[i][j]);
}}
/* Find Transpose */
for (i = 1; i < size; i++) {
for (j = 0; j < i; j++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}}
printf("\n Transpose Matrix is :");
for (i = 0; i < size; i++) {
printf("\n");
for (j = 0; j < size; j++) {
printf("%d\t", arr[i][j]);
}}
getch();
}
Output:
Enter the Size of Matrix: 3 3
Enter the Values of A:
1
2
3
4
5
6
7
8
9
Given Square Matrix is:
1 2 3
4 5 6
7 8 9
Transpose Matrix is:
1 4 7
2 5 8
3 6 9
(Or)

Addition of all Elements in Matrix


Write a C program to add all the elements in the Matrix.
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, mat[10][10], row, col;
int sum = 0;
printf("\n Enter the Number of Rows : ");
scanf("%d", &row);
printf("\n Enter the Number of Columns : ");
scanf("%d", &col);
//Accept the Elements in Matrix
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("\n Enter the Element of mat[%d][%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}
//Addition of all Elements
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
sum = sum + mat[i][j];
}}
//Print out the Result
printf("\n Sum of all Elements in Matrix : %d", sum);
getch();
return (0);
}
Output:
Enter the Number of Rows : 2
Enter the Number of Columns : 2
Enter the Element mat[0][0] : 1
Enter the Element mat[0][1] : 1
Enter the Element mat[1][0] : 1
Enter the Element mat[1][1] : 1
Sum of all Elements in Matrix : 4
Scaling
 A scaling transformation alters the size of an object.
 This operation can be carried out for polygons by multiplying the coordinate
values (x, y) of each vertex by scaling factors sx and sy to produce the
transformed coordinates (x’, y’).
 In General Fixed – Point Scaling, there are 3 steps to scale an object about the
fixed point,

1. Translate object so that the fixed point coincides with the coordinate origin.
2. Scale the object with respect to the coordinate origin.
3. Use the inverse translation of step 1 to return object to its original position.

The composite transformation matrix for this process is

PROGRAM:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\tc\bg:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}

String operations:(Handling of Character Strings)


Explain in detail about the string with an example. (Or) Explain the concept of
strings in detail. (April / May 2017)
• In ‘C’ language the group of characters, 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. Header file used is
string.h.
• Eg: char name[]={‘b’,’o’,’b’,’\0’};
• Each character is stored in one byte of memory and successive characters of the
string are stored in successive byte.
Declaration & Initialization of String
• String variable is always declared as an array.
• Syntax: data_type string_name[size]=”values”; (or) data_type
string_name[size];
The string can be initialized as follows,
char name[]=”BOB”; (or) char name[5];
• 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
itself inserts the NULL (\0) character automatically at the end of the string.
Reading and Writing String
• The ‘%s’ control string can be used in scanf() statement to read a string.
• Eg: scanf(“%s”,name);
• And the same may be used to write the string in printf() statement.
• Eg: printf(“%s”,name);

Standard String Functions


Explain the various string operations. Write a C program to find out length of the
string without using built in function. (Nov/Dec 2014) (Or) Explain the various
string handling functions in C. (April/May 2015)
The ‘C’ compiler provides the following string handling functions.
Function Purpose Syntax
strlen() It is used to find the length of the var=strlen(string);
string.
strcpy() It is used to copy one string to strcpy(string1,string2);
another.
strcat() It is used to combine two strings. strcat(string1,string2);
strcmp() It is used to compare two strings. strcmp(string1,string2);
strrev() It used to reverse a string. strrev(string);
strlwr(), It used to change the case of a string. strlwr(string);
strupr() strupr(string);
strncpy() It used to copy ‘n’ characters of one string to another.
strstr() It is used to determine the first occurrence of a given string in another
string.
strncat() It appends source string to destination string upto specified length.
strspn() It is used to find upto what length two strings are identical.
strncmp() It is used to compare ‘n’ character of two strings.
strcmpi() It is used to compare two strings without regarding the case.
strnicmp() It is used to compare first ‘n’ characters of two strings without regarding
the case.
strchr() It is used to determine the first occurrence of a given character in a
string.
strrchr() It is used to determine the last occurrence of a given character in a string.

The commonly used string manipulation functions are as follows,


strlen() function
• It is used to count and return the number of characters present in a string.
• Syntax: var=strlen(string);
Example Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char a[]="college";
int b;
clrscr();
b=strlen(a);
printf("\n The Length of the String is %d",b);
getch();
}
Output:
The Length of the String is 7
strcpy() function
• It is used to copy the contents of one string to another.
• Syntax: strcpy(string1,string2);
Example Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char a[]="IT";
char b[]="Dept";
clrscr();
strcpy(a,b);
printf("\n The String is %s",a);
getch();
}
Output:
The String is Dept
strcat() function
• It is used to concatenate or combine two strings together and forms a new string.
• Syntax: strcat(string1,string2);
Example Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char a[]="IT";
char b[]="Dept";
clrscr();
strcat(a,b);
printf("\n The String is %s",a);
getch();
}
Output:
The String is ITDept
strcmp() function
• It is used to compare two strings to find out whether they are same or different.
• The two strings are compared character by character until the end of one of the
string is reached.
• Returns a value 0, if two strings are equal.
• Returns a value <0, if s1 is less than s2.
• Returns a value >0, if s1 is greater than s2.
• Syntax: strcmp(string1,string2);
Example Program 1 Example Program 2
#include<stdio.h> #include <stdio.h>
#include<conio.h> #include <string.h>
#include<string.h> int main()
void main() { {
char a[]="itdept"; char s1[20] = "vrscet";
char b[]="it"; char s2[20] = "vrsceT";
int i; if (strcmp(s1, s2) ==0)
clrscr(); {
i=strcmp(a,b); printf("\n String 1 and String 2 are
if(i==0) Equal.");
printf("\n Strings are Equal:%d",i); }
else if(i<0) else
printf("\n String 1 is less than String {
2:%d",i); printf("\n String 1 and String 2 are
else Different.");
printf("\n String 1 is greater than String }
2:%d",i); return 0;
getch(); }
} Output:
Output: String 1 is greater than String String 1 and String 2 are Different.
2:100
strrev() function
• It is used to reverse a string.
• This function takes only one argument and returns one argument.
• Syntax: strrev(string);
Example Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char a[]="Dept";
clrscr();
printf("\n The String is %s",strrev(a));
getch();
}
Output
The String is tpeD
strlwr() and strupr() function
• It is used to change the case of the string.
• Syntax: strlwr(string); strupr(string);
Example Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]="itdept";
clrscr();
printf("\n The string is:%s",a);
strupr(a);
printf("\n The string after conversion to uppercase:%s",a);
strlwr(a);
printf("\n The string after conversion to lowercase:%s",a);
getch();
}
Output
The string is: itdept
The string after conversion to uppercase: ITDEPT
The string after conversion to lowercase: itdept

Linear search c program


#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

binary search
#include <stdio.h>
#include <conio.h>
main()
{

int arr[10], num, i, n , pos =-1, beg, end,mid, found =0;


clrscr ();
printf("\n Enter the number of elements in the array: ");
scanf ("%d", &n);
printf (" \n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);

printf("\n Enter the number that has to be searched: " );


scanf ("%d", &num);
beg = 0, end = n-1;
while (beg <end)
{
mid= (beg+ end)/2;
if (arr[mid] == num)
{
printf("\n %dis present in the array at position = %d", num, mid);
found=1;
break;
}
if (arr[mid]>num)
{
end = mid-1;
}
else if (arr[mid] < num)
beg = mid+1;
}
if ( beg > end &&found == 0)
{
printf("\n %d DOESNOTEXIST IN THE ARRAY",num);
}
getch();
return 0;
}
UNIT II ARRAYS AND STRINGS

1. Define Array. (Jan 2014) (Or) What is an Array? (May/June 2016) (Or) What is an
Array? How will you create a 2D array? (April / May 2017)
• An array is a collection of similar data items that are stored under a common name.
• ie., an array is a group of related data items that share a common name.
• A value in an array is identified by index or subscript enclosed in square brackets
with array name.
2. List down the classifications of array.
Arrays can be classified into,
• One Dimensional Array
• Two Dimensional Array
• Multi Dimensional Array
3. What is One Dimensional Array?
• The collection of data items can be stored under a one variable name using only one
subscript; such a variable is called as one dimensional array.
• An array with a single subscript is known as one dimensional array.
4. How will you declare an array?
• Arrays are declared in the same manner as ordinary variables except that each array
name must have the size of the array.
• Here data-type is the type specifier that indicates what type of data the declared
array is such as int, float, double or char.
• Array-Name is the name of the declared array and rules of declaring the variable
applies to array name.
• Array-Size defines how many elements the array contains. This is always an
integer.
Syntax
data_type array_name[size or subscript of the array];
Example
int x[3];
5. What are the features of an array?
• An array is a derived data type.
• The elements can be accessed with base address and the subscript defined for the
position of the element.
• The elements in an array are stored in continuous memory location.
• It is easier to refer the array elements by simply incrementing the value of the
subscript.
6. What is Two Dimensional Arrays?
• It is used in situation where a table of values needs to be stored in an array.
• It is similar to one dimensional array except a separate pair of square brackets is
required for each subscript.
• Two dimensional arrays are stored in a row-column matrix, where the left index
indicates the row and the right indicates the column.
• Eg: int x[3][2];
7. How will you initialize an array?
• The arrays can be initialized in two ways,
o At Compile time
o At Run time
At Compile time
• This initialization is done while writing the program itself.
• Eg: int x[3]={5,3,7};
At Run time
• An array can initialized at run time by the program or by taking the input from the
keyboard.
8. Write example code to declare two dimensional arrays. (May/June 2014)
#include<stdio.h>
#include<conio.h>
void main() {
int i, j;
int x[2][2]={ {1,50},
{2,75}
};
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("\n The value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}
Output:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
9. How will you pass arrays to pointers?
• Array can be transferred to a function as a parameter.
• To transfer an array, the array name is enough without subscripts as actual
parameters within the function call.
10. What is array of characters? (Or) Define String. Give Examples. (May/June 2016)
• A string is a collection of characters, digit, and symbols within quotes is called as
string or character array.
• A string constant is a one dimensional array of characters terminated by a null (‘\0’)
character.
• ‘\0’ is a null character used to specify the end of the string.
• Eg: char a[]={‘a’,’b’,’c’,’\0’};
11. Give an example for initialization of string array. (Nov/Dec 2014)
• String variable is always declared as an array.
• The string can be initialized as follows,
char name[]=”BOB”; (or) char name[5];
• The characters of the string are enclosed within a pair of double quotes.
12. Mention the various standard string functions in C. (Or) Name any two library
functions used for string handling. (Jan 2014) (Or) List any four string handling
functions. (May/June 2014) (April / May 2017) (Or) Give some examples of string
functions. (Nov/Dec 2015)
Function Purpose Syntax
strlen() It is used to find the length of the var=strlen(string);
string.
strcpy() It is used to copy one string to strcpy(string1,string2);
another.
strcat() It is used to combine two strings. strcat(string1,string2);
strcmp() It is used to compare two strings. strcmp(string1,string2);
strrev() It used to reverse a string. strrev(string);
strlwr(), strlwr(string);
It used to change the case of a string.
strupr() strupr(string);
strncpy() It used to copy ‘n’ characters of one string to another.
strstr() It is used to determine the first occurrence of a given string in another
string.
strncat() It appends source string to destination string upto specified length.
strspn() It is used to find upto what length two strings are identical.
strncmp() It is used to compare ‘n’ character of two strings.
strcmpi() It is used to compare two strings without regarding the case.
strnicmp() It is used to compare first ‘n’ characters of two strings without
regarding the case.
strchr() It is used to determine the first occurrence of a given character in a
string.
strrchr() It is used to determine the last occurrence of a given character in a
string.

13. Define Sorting.


• Sorting is the process of arranging elements either in ascending or in descending
order.
14. What are the types of sorting?
• Bubble Sort
• Insertion Sort
• Selection Sort
• Quick Sort
15. What is Bubble Sort?
• It is one of the easiest sorting methods.
• In this method, each data item is compared with its neighbor and if it is descending
sorting, then the bigger number is moved to the top of all, the smaller numbers are
slowly moved to the bottom position.
• It is also called as the exchange sort.
16. What is Insertion Sort?
• It is quite simple.
• Each successive element in the array to be sorted into its proper place with respect to
the other already sorted element.
• This process is continued until all the elements have been sorted.
17. What is Selection Sort?
• Here, array contains ‘n’ elements to be sorted in correct sequence.
• The first element is compared with the remaining n-1 elements and which is the
lower element; place that element in first position.
• Then the second element is compared with the remaining n-2 elements and so on.
• This process is continued until all the elements in an array are sorted.

18. What is Quick Sort?


• It is the most effective and efficient sorting technique.
• It is developed by C.A.R.HOARE.
• It is implemented based on the idea of partitions.
• Quick sort identifies an element from the given list of ‘n’ elements (Say ‘k’).
• Then it moves all the elements in the list that is less than ‘k’ to the left side of the ‘k’
and greater than to the right side of ‘k’.
19. List down the types of searching techniques.
• Linear Search (Or) Sequential Search
• Binary Search
20. What is Linear Search? (Or) What is Sequential Search?
• It is the simplest technique for searching an array for a particular record or data
element.
• In linear search, we start with the first available element that is the element that
comes first.
• If this is the required element then our search is over, else we take up the second
element and so on.
• This process is continued until we find the required element or all the elements in
the table is exhausted.
21. What is Binary Search?
• It is simpler and faster than linear search.
• In binary search, on each search the array to be searched is divided into two parts,
one of which is ignored as it will not contain the required element.
• One essential condition for the binary search is that the array which is to searched
should be arranged in order.
22. Differentiate between array and structure.
Array Structure
 An array is a collection of similar  Structure is a collection of different
data items of same type. data items of different type.
 An array is a derived data type.  It is a user defined data type.
 An array behaves like a built-in data  It must be declared and defined.
type.

23. Declare a character array of size 5 and assign vowels to it. (Nov/Dec 2015)
Character array can be declared as,
char vowels[SIZE]={'a','e','i','o','u'};

You might also like