Unit 2
Unit 2
PART A
1. What is an array? Write the syntax for multi-dimensional array. [APR/MAY
2018](OR) What is an array? Give the general form of array declaration.
An array is a collection of variables of the same data type that are referenced by
a common name.
Syntax: <datatype><variable_name>[index];
Syntax for multi-dimensional array:
<datatype>array_name[a][b]…..[k];
Arrays of three or more dimensions may be used, depending upon the situations and
also these array occupy more amount of memory space
#include <stdio.h>
int main()
int m, n, p, q, i, j, k, sum = 0;
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
if (m != p)
else
{
for (i = 0; i < n; i++)
C[i][j]=c[i][j]+a[i][k]*[k][j];
printf("%d\t", c[i][j]);
printf("\n");
return 0;
2. What are the different types of string function? Describe with their purpose.
[APR/MAY 2018] (OR) Describe about the string operation?
There are four important string handling function in the C language
I. strlen() function
II. strcpy() function
III. strcat() function
IV. strcmp() function
the header filr #include<string.h> is used when these functions are called in C
program.
I. strlen() function:
strlen() function is used to find the length of the character string.
Example:
int n;
char ss[10]=”Chennai;’
n=strlen(ss);
this will return a length of the string 7 which is assigned to an integer
variable n.
The null character ‘\0’ available at the end of a string is not counted.
II. strcpy() function:
strcpy() function is used to copy a character string to a character variable.
Example:
char city[20];
strcpy(city,”chennal”);
this will assign the string “Chennai” to the character variable city.
A character value like city=”Chennai” cannot be assigned in the C
language.
III. strcat() function:
strcat() function is used to join character strings. When two strings are
joined it is referred as concatenation of strings.
Example:
char city[20]=”Chennai”;
char pin[8]=”650 006”;
strcpy(city,pin);
this will join the two strings and store the result in city as
“Chennai 650 006”.
The resultant string is always stored in the left side string variable.
IV. strcmp() function:
strcpy() function is used to compare two character strings. It returns a 0
when the two strings are identical. Otherwise it returns a numerical value
which is the difference ASCII values of the first mismatching characters of
the strings being compared.
Example:
char city[20]=”one”;
char town[20]=”two”;
strcmp(city,town);
this will return an integer value -5 which is the difference in the ANCII
value of the first mismatching letters ‘c’ and ‘t’.
The integer value obtained as the difference may be assigned to an
integer variables as follows,
int n;
n=strcmp(city,town)
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],str3[40];
int res,len;
clrscr();
Output:
Enter the string 1 babu
Enter the string 2 rubin
Strlen(str1):4
Str1<str2
Strcpy (str3,str1):babu
Strcat(str1,str2):baburubin
3. Write the C program to find the number of vowels, consonants, Digits and
white space in a string. [APR/MAY 2018]
#include <stdio.h>
int main()
char line[150];
++vowels;
++consonants;
++digits;
++spaces;
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
return 0;
Output
Consonants: 11
Digits: 9
White spaces: 2
One dimensional array is like a list and the item in an array can be accessed
by just giving one subscript.
Syntax:
<datatype> identifier_name[size];
Example:
int a[5];
Array initialisation:
The initialisation of global arrays and static local arrays at the time of
declaration.
Syntax:
<datatype>array_name[size]={value list};
char array_name[size]=”string”;
Example:
Charn mess[]={‘w’,’e’,’l’,’c’,’o’,’m’,’e’};
Program:
#include <stdio.h>
int main()
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &marks[i]);
sum += marks[i];
average = sum/n;
return 0;
Output:
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Syntax:
<datatype> array_name[row_size][column_size];
int a[3][3];
Example:
int a[2][3]={1,1,1,2,2}
Program:
#include<stdio.h>
int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}
Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40
Multidimensional array:
C allows more than two dimensions. The exact limit, if any is determined by
the compiler under use,
Syntax:
<datatype>array_name[a][b]…..[k];
Arrays of three or more dimensions may be used, depending upon the situations and
also these array occupy more amount of memory space
5. Computing Mean, Median, and Mode for the one dimensional array?
Computing Mean:
#include<stdio.h>
void main()
{
int i,n,a[20],sum=0;
float mean;
printf(“Enter N:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter %d number:”,i+1);
scanf(“%d,&a[i];
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=(float)sum/n;
printf(“\nThe mean value is %f”,mean);
}
OUTPUT:
Enter N: 5
Enter 1 number: 12
Enter 2 number: 134
Enter 3 number: 14
Enter 4 number: 5
Enter 5 number: -8
The mean value is 31.400000
Computing Median:
#include<stdio.h>
void main()
{
int i,,j,temp,n,a[20],sum=0;
float median;
printf(“Enter N:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter %d number:”,i+1);
scanf(“%d,&a[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
if(n%2==0)
{
median=((a(n/2)+a(n/2-1))/2.0);
}
else
{
median= a(n/2);
}
printf(“\n The median value is %f”,median);
getch();
}
OUTPUT:
Enter N: 4
Enter 1 number: 14
Enter 2 number: 12
Enter 3 number: 11
Enter 4 number: 13
The median value is 12.50000
Computing Mode:
#include<stdio.h>
void main()
{
int maxvalue=0, maxcount=0,i,j,a[20],n,count=0;
printf(“Enter N:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter %d number:”,i+1);
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
//int count=0;
for(j=0;j<n;j++)
{
if(a[j]==a[i])
++count;
}
if(count>maxcount)
{
maxcount=count;;
maxvalue=a[i];
}
}
printf(“The mode value is %d”,maxvalue);
getch();
}
OUTPUT:
Enter N: 4
Enter 1 number: 0
Enter 2 number: 6
Enter 3 number: 7
Enter 4 number: 2
Enter 5 number: 7
The mode value is 7
6. Matrix operation for addition, multiplication, scaling, determinant and
transpose in two dimensional array?
Matrix addition:
#include<stdio.h>
void main()
{
int i,j,p,q,n,m,a[3][3],b[3][3],c[3][3];
printf(“Enter the size of the matrix A”);
scanf(“%d%d”,&n,&m);
printf(“Enter the size of the matrix B”);
scanf(“%d%d”,&p,&q);
if((n==p) && (m==q))
printf(“Enter the first matrix”);
for(i=1;i<=n,i++)
{
for(j=1;j<=m;j++)
{
scanf(“%d”,&a[i][j];
}
}
printf(“Enter the second matrix”);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
C[i][j]=a[i][j]+b[i][j];
}
}
printf(“ Display addition matrix”);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf(“%d \t”,c[i][j]);
}
printf(“\n”);
}
getch();
}
OUTPUT:
Enter the size of matrix: 3
Enter the first matrix
1 2 3
2 1 2
3 1 1
Enter the second matrix
1 2 3
2 1 2
3 1 1
Display addition matrix
2 4 6
4 2 4
6 2 2
Matrix scaling:
#include<stdio.h>
void main()
{
int i,j,a[3][3];
int n,num;
printf(“Enter the size of the matrix”);
scanf(“%d”,&n);
printf(“Enter the first matrix”);
for(i=1;i<=n,i++)
{
for(j=1;j<=n;j++)
{
scanf(“%d”,&a[i][j];
}
}
scanf("%d", &num);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=num * a[i][j];
}
}
printf(“ Display addition matrix”);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d \t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
Matrix determinant:
Matrix determinant of 2x2:
a b
A=
c d |A|= ad-bc
#include<stdio.h>
void main()
{
int a[2][2],i,j;
long determinant;
printf(“Enter the 4 elements of matrix:”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j];
}
}
determinant=a[0][0]*a[1][1]-a[1][0]*a[0][1];
printf(“\n Determinant of 2x2 matrix:%ld”, determinant);
getch();
}
OUTPUT:
Enter the 4 elements of matrix:
4 8
3 9 Determinant of 2x2 matrix: 12
Matrix transpose:
#include<stdio.h>
void main()
{
int i,j,a[3][3];
printf(“Enter the matrix”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Transpose matrix”);
for(j=1;j<=3;j++)
{
for(i=1;i<=3;i++)
{
printf(“%d\t”,a[i][j]);
}
Print(“\n”);
}
getch();
}
OUTPUT:
Enter the matrix
1 2 3
2 3 4
5 8 7
Transpose matrix
1 2 5
2 3 8
3 4 7
7. Write the program to sort the element using Selection sort?
Steps:
Start from the first element in the array and search for the smallest
element in the array.
Swap the first element with the smallest element of the array.
Take a subarray and search for the smallest number in the subarray
and swap it with the first element of the array.
Repeat the step 2 and 3 with new subarrays until the array gats sorted.
Initial array
16 19 11 15 10 12 14
First iteration
16 19 11 15 10 12 14
10 19 11 15 16 12 14
Second iteration
10 19 11 15 16 12 14
10 11 19 15 16 12 14
Third iteration
10 11 19 15 16 12 14
10 11 12 15 16 19 14
Fourth iteration
10 11 12 15 16 19 14
10 11 12 14 16 19 15
Fifth iteration
10 11 12 14 16 19 15
10 11 12 14 15 19 16
Sixth iteration
10 11 12 14 15 19 16
10 11 12 14 15 16 19
Final iteration
10 11 12 14 15 16 19
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int a[10];
int i,j,k,n,temp,index_of_smallest;
clrscr();
scanf(“%d”,&n);
for(k=0;k<n;k++)
scanf(“%d”,&a[k]);
i=0;
while(i<n)
index_of_smallest=i;
for(j=i;j<n;j++)
if(a[j]<a[index_of_smallest])
index_of_smallest=j;
//swapping
temp=a[i];
a[i]=a[index_of_smallest];
a[index_of_smallest]=temp;
i++;
for(i=0;i<n;i++)
printf(“%d\t”,a[i];
getch();
Output:
16 19 11 15 19 12 14
10 11 12 14 15 16 19
8. Write a program to search for an element using (a) linear search (b) binary
search
(a) Linear search:
Linear search is also called as sequential search. Linear search is a method
for finding a particular value in a list. That consists of checking every one of its
elements, one at a time and in sequence, until the desired one is found.
Program:
#include<stdio.h>
void main()
{
int a[100],n,i,s,flag;
clrscr;
printf(“\n Enter the limit:”);
scanf(“%d”,&n);
printf(“\n Enter the element:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“\n Enter the elements to be searched:”);
scanf(“%d”,&s);
for(i=0;i<n;i++)
{
if(s==a[i])
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag=1)
printf(“\n The element is %d in the position %d”,s,i+1);
else
printf(“\n %d is not found in the list”,s);
getch();
}
Output:
Enter the limit: 4
Enter the elements: 25 85 95 13
Enter the element to be searched: 87
87 is not found in the list
Steps:
1 2 3 4 5 9 8
0 1 2 3 4 5 6
Step1:
Step 2:
Mid=(beg+end)/2 =(0+6)/2= 3.
The center of the element i.e middle of the element is array[3]. The
element in array[3] is 4. So the middle of the element is 4.
Step 3:
Then check whether the element to be searched is less than the middle
of the element. If it is so then search the element from the half of the
array, this condition is not satisfied here.
Check whether the element to be searched is greater than the middle
of the element. If it is so then search the element from the second half
of the array. This condition is satisfied here i.e 9>4
Beg end
5 9 8
4 5 6
Step 4:
Search is successful.
Drawback :
Program:
1. #include <stdio.h>
2.
3. void main()
4. {
5. int array[10];
6. int i, j, num, temp, keynum;
7. int low, mid, high;
8.
9. printf("Enter the value of num \n");
10. scanf("%d", &num);
11. printf("Enter the elements one by one \n");
12. for (i = 0; i < num; i++)
13. {
14. scanf("%d", &array[i]);
15. }
16. printf("Enter the element to be searched \n");
17. scanf("%d", &keynum);
18. /* Binary searching begins */
19. low = 1;
20. high = num;
21. do
22. {
23. mid = (low + high) / 2;
24. if (keynum < array[mid])
25. high = mid - 1;
26. else if (keynum > array[mid])
27. low = mid + 1;
28. } while (keynum != array[mid] && low <= high);
29. if (keynum == array[mid])
30. {
31. printf("SEARCH SUCCESSFUL \n");
32. }
33. else
34. {
35. printf("SEARCH FAILED \n");
36. }
37. }
Output:
#include<stdio.h>
void main()
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf(“After sorting:\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
Output:
12 0 -14 5 16
After sorting:
-14 0 5 12 16