Вы находитесь на странице: 1из 26

Programs

1.to accept and print an array.


#include<stdio.h>
main()
{
int n,i;
printf("enter number");
scanf("%d",&n);
int num[n];
for(i=0;i<n;i++)
{
printf("enter number");
scanf("%d",&num[i]);
}
for(i=0;i<n;i++)
{
printf("%d",num[i]);
}
}
2.print largest and the smallest number in the array.
#include<stdio.h>
main()
{
int x[]={1,2,3,4,5,6,7,8,9};
int i,lar=x[0],lp=0,small=x[0],sp=0;
for(i=0;i<9;i++)
{
if(x[i]>=lar)
{
lar=x[i];
lp=i;
}
}
printf("largest %d at %d",lar,lp);
for(i=0;i<9;i++)
{
if(x[i]<=small)
{
small=x[i];
sp=i;
}
}
printf("smallest %d at %d",small,sp);
}
3.to accept and print 2D array
#include<stdio.h>
main()
{
int n,r,c;
printf("enter number");
scanf("%d",&n);
int x[n][n];
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
printf("enter elements");
scanf("%d",&x[r][c]);
}
printf("\n");
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
printf("%d",x[r][c]);
}
printf("\n");
}
}
4.addition of 2 matrices
#include<stdio.h>
main()
{
int A[4][4]={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};
int B[4][4]={{4,4,4,4},{3,3,3,3},{2,2,2,2},{1,1,1,1}};
int r,c,sum=0;
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
sum=(A[r][c])+(B[r][c]);
printf("%d",sum);
}
printf("\n");
}
}
5.multiplication of matrices
void main()
{
int m1,n1,m2,n2,i,j,k;
printf("enter 1st matrix rows and columns ");
scanf("%d %d",&m1,&n1);
printf("enter 2nd matrix rows and columns ");
scanf("%d %d",&m2,&n2);
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}
if(n1==m2)
{
int c[m1][n2];
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
else
printf("not possible");
}
6.duplicate
#include<stdio.h>
main()
{
int i,j,c=0,n;
printf("enter size of array");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("enter the elements of the array");
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i] == arr[j])
{
c++;
}
}
}
printf("no of duplicate elements = %d",c);
}
7.to print the diagonal elements of a matrix
void diag(int a[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf(a[i][j]);
}
}
}
}
void main()
{
int i,j,a[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("enter the elements of the array");
scanf("%d",&a[i][j]);
}
}
diag(a);
}
8.to insert an element at a specified position in an array
#include<stdio.h>
main()
{
int n,i,p,s;
printf("enter the size of array and the position at which an element is to
be inserted and the element");
scanf("%d %d %d",&s,&p,&n);
int x[s];
for(i=0;i<s-1;i++)
{
printf("enter elements of the array");
scanf("%d",&x[i]);
}
for(i=s-1;i>=p;i--)
{
x[i+1]=x[i];
}
x[p]=n;
for(i=0;i<s;i++)
{
printf("%d",x[i]);
}
}
9.to delete an element from an array
#include<stdio.h>
main()
{
int n,i,p,s;
printf("enter the size of array and the position at which an element is to
be deleted and the element");
scanf("%d %d %d",&s,&p,&n);
int x[s];
for(i=0;i<s-1;i++)
{
printf("enter elements of the array");
scanf("%d",&x[i]);
}
for(i=p;i<s-1;i++)
{
x[i]=x[i+1];
}
x[s-1]=0;
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}
}
10.selection sort.
#include<stdio.h>
main()
{
int i,j,s,p,t=0;
int x[10]={1,5,8,9,7,2,0,8,9,6};
for(i=0;i<10;i++)
{
s=x[i];
p=i;
for(j=i+1;j<10;j++)
{
if(x[j]<x[p])
{
t=x[j];
x[j]=x[p];
x[p]=t;
}
}
}
for(i=0;i<10;i++)
printf("%d",x[i]);
}
11.bubble sort
#include<stdio.h>
main()
{
int i,j,t=0;
int x[10]={1,2,5,6,7,8,3,4,0,4};
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
for(i=0;i<10;i++)
{
printf("%d",x[i]);
}
}
12.linear search
#include<stdio.h>
main()
{
int n,i,s,c;
printf("enter size of array and the number to be searched");
scanf("%d",&s,&n);
int x[s];
for(i=0;i<s;i++)
{
printf("enter elements of the array");
scanf("%d",&x[i]);
}
for(i=0;i<s;i++)
{
if(x[i]==n)
c++;
}
if(c!=0)
printf("element found ");
else
printf("element not found");
}
13. binary search
#include<stdio.h>
void sort(int [],int);
main()
{
int n,l,i,f=0,m;
printf("enter the number to search; enter the size of array");
scanf("%d",&n,&l);
int x[l];
for(i=0;i<l;i++)
{
printf("enter the elements of the array");
scanf("%d",&x[i]);
}
sort(x,l);
while(f<=l)
{
m=(l+f)/2;
if(x[m]==n)
break;
if(x[m]<n)
f=m;
if(x[m]>n)
l=m;
}
printf("found at %d",m);
}
void sort(int x[],l)
{
int i,j,t=0;
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}

Arrays
Array- an array is a collective name given to a group of
similar elements whereas a variable is an entity that may
change during program execution.
An array is a variable that is capable of holding
many values. For example, an array initialization would
be, int number[8];
For which there are 8 storage locations reserved where 8
different values can be stored belonging to same type.
Whereas an ordinary variable initialization variable
would be, int number;
And only one storage location is reserved and can hold
only one value at a time.
Advantages of using arrays:
*Array is a simplest kind of data structure
*it is easy to create, understand & implement arrays
*In arrays direct access to any element is possible.
However, modifications done to one element does not
effect the other element.
*Arrays have the capability of linking together especially
with multiple dimension
*Every array element can be accessed at constant time.
*Array variables are capable of storing more no. Of
values.
One dimensional array:
It is the simplest form of array in which list of
elements are stored in contiguous memory locations and
are accessed using only one subscript.
Declaration of one dimensional array:
Datatype array-name [size]
datatype specifies the data type of array elements
array-name indicates the name of array variable
size specifies the no. Of elements in the array
Eg: int a [10]
Accessing elements in the array:
Once the arrays are declared, every individual
element can be accessed using the subscript which is an
integer expression or integer constant. The subscript
specifies the position of element in the array this is called
index of array element. The array subscript start at 0
and ends at size of array -1.
Array-name [i]....eg: ar [0],ar [1]
Printing one dimensional array:
Array elements can be printed by using
forloop and a printf statement.
Eg: int a [5]={10,20,30,40,50};
For(int i=0;i <=5;i++)
Printf (\not the elements of array are: %d,a [i]);
Initialization of one-dimensional array:
We can initialize array elements at the place of
their declaration itself. The general format of this
initialization is,
Type array-name [size]={list of elements separated
by commas}
Eg: int a [4]={5,10,1,55}
Here ,the size of array is 4,the value 5 is a
assigned to first element i.e..,a [0]=5,the value 10 is
assigned to second element a [1]=10,the value 1 is
assigned to third element a [2]=1 and the value 55 is
assigned to fourth element a [3]=55.
Suppose the list of element specified are less than the
size of array then only that many elements are initialized.
The remaining values are assigned garbage values.
If suppose:
Int xyz []={5,6,7,8,9};
In the above statement, we did not specify the size of an
array. In such cases compiler fixes the size of the array
based on the number of values In the list. In above eg:
since we have 5 values in the list, size of this array is 5
A one-dimensional array program to print array of 20:
#include<stdio.h>
Void main()
{
Int arr [20],i;
For (i=0;i <20;i++)
{
Scanf(%d,&arr [i]);
}
For (i=0;i <20;i++)
{
Printf (%d\n,arr [i]);
}
}
Necessity to give size of an array in array declaration:
Whenever an array is declared, the task of the
compiler is to allocate the base address and withhold
sufficient space in the memory to store all the array
elements. Hence, if the size is given during array
declaration then the compiler will allocate the specified
amount of space in the memory. But if the size is not
given then the compiler will create a big enough space in
the memory that will be wasted if not used.
Two dimensional arrays:
When the data must be stored in the form of a
matrix we use two dimensional arrays
Eg:int a [5][3];
Above declaration represents a two
dimensional array consisting of 5 rows and 3 columns. So
the total no. Of elements which can be stored in this
array are 53 i.e...15
Declaration of two dimensional arrays:
Syntax: type array name[row-size][column-size]
type represents the data type of array elements
array-name is the name of the array
row-size represents no. Of rows in the array
column-size represents no. Of columns in the array
Eg:int xyz [2][5]
Here,xyz is an array consisting of two rows with
each other consisting of 5 integer elements
Float a [5][5]
Here, a is an array consisting of 5 rows with each
row consisting of 5 floating point numbers.
Initialization of two dimensional arrays:
Two dimensional arrays can also be initialized at
the place of declaration itself. The general format of
initializing two dimensional array is,
Type array-name [row-size][column-size]={{list of
elements in row 1},
{List of elements in row 2},
.
.
{List of elements in row n}.
Eg:1.int xyz [2][3]={{2,3,4}, {5,6,7}};
Here 2,3&4 are assigned to 3 columns of 1st row
and 5,6&7 are assigned to 3 columns of 2nd row in order.
2.int xyz [2][3]={{0}, {0}};
Here, 1st elements of each row is initialized to
zero while other elements have some garbage values.

Referencing elements of two-dimensional array:


The elements of an array can be referenced using
two indices , one for obtaining row number and another
for obtaining column number.
Eg: xyz [i[j][k] is used to access ith row and jth
column.
No. Of elements in two-dimensional array is given as,
(Upper_bound-lower_bound+1)no. Of columns
Or
Number of rows number of columns
Storage representation of two-dimensional arrays:
Generally, we logically say that, two-
dimensional arrays consists of rows and columns but
when it is stored in memory, no facility for two-
dimensional storage is available. The memory is linear.
Hence, the actual storage differs from our matrix
representation. Two major types of representations can
be used for two-dimensional arrays.
(a) Row major representation
(b) Column major representation.

Row major representation: in row major representation,


the elements of 1st row are stored in contiguous memory
locations followed by elements in 2nd row and so on.
Accessing address of an array element:
If we know the base address of an array, we
can access any element in that array.
In an array a [m][n], the address of an element a [i][j]
is given as,
Base address of an element a [i][j] is given as,
Base address+(in+j)size of array element.
Column major representation: in column major
representation the elements of first row are stored in
contiguous memory locations followed by elements of
second row and so on.
Accessing address of an array element:
In an array a [m][n] stored using column
major order, the address of a [i][j] is given as,
Base address+(i+jm)size of array element
Matrix representation of two dimensional arrays::
A two-dimensional array is an array of arrays. In
contrast to one dimensional representation of arrays in
which the data is organised linearly in one direction .
The two dimensional representation of arrays stores
the data in two dimensions or directions. This
representation of elements in two directions is like a table
with rows and columns which is nothing but a matrix.
The diagrammatic representation of two-
dimensional arrays is shown in the figure given below:
from the figure it is clear that a two dimensional array is

an array of one dimensional array.


Multidimensional arrays:
A three dimensional array can be thought as an
array of arrays. It is a collection of two dimensional
arrays.
A three dimensional array consisting of 3 two
dimensional arrays having 2 rows and 3 columns
Initialization of three dimensional array:
If we want to initialize elements of a large sized
array, suppose of 250 elements, it is difficult to initialize
such an array at a time of declaration. In such cases
Declaration must be done explicitly. In C we use loops
(while,for,while or do while) to initialize the elemnets.
There is no shortcut to initialize array of large size in c.
One dimensional v/s multidimensional arrays:
1. One dimensional array can accommodate data only
in one dimension whereas multidimensional arrays
can accommodate in more than one dimensions.
2. Only one subscript is specified for one-dimensional
arrays whereas more than one subscripts are
specified for multidimensional arrays,so that the
elements can be identified.
For one dimensional, the statement can be,
Int a [i];
For multidimensional, the statement can be,
Int a[i][j];
Int a [i][j][k];
3. multidimensional arrays consist of rows and columns
depending on their dimensions. But one dimensional
array do not consist of rows and columns.
4. A multi dimensional array can be 2-D or 3-D.

Вам также может понравиться