Unit 2-Notes-1
Unit 2-Notes-1
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
#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]);
}
}
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)
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.
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();
}
return 0;
}
binary search
#include <stdio.h>
#include <conio.h>
main()
{
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.
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'};