Arrays Simple Programs
Arrays Simple Programs
#include<stdio.h>
void main()
{
int i,a[5];
for(i=0;i<5;i++)
{
a[i]= i*2; // assigning the values
printf(" %d",a[i]);
}
Program to print 2,4,6,8-----20 using arrays by read the N value ( assigning the values)
#include<stdio.h>
void main()
{
int i,a[10],n;
printf("Enter the value of N");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]= i*2;
printf(" %d",a[i]);
}
}
#include<stdio.h>
void main()
{
int i,a[5]={1,2,3,4,5};
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
#include <stdio.h>
void main()
{
int a[20],i,n,sum=0;
printf("Enter the value of N");
scanf("%d",&n);
printf("Enter the array value:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum= sum+a[i];
}
printf("sum=%d",sum);
}
Linear Search
#include <stdio.h>
int main()
{
int a[20], key, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter values");
}
Binary search
#include <stdio.h>
void main()
{
int a[10],n,i,mid,low,high,key;
printf("Enter the value of n");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key<a[mid])
{
high=mid-1;
}
else if(key==a[mid])
{
printf("%d is found in the location %d",key,mid);
break;
}
else
{
low=mid+1;
}
}
if(low>high)
{
printf("%d is not found",key);
}
}
Bubble sort
#include <stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("Enter the value of n");
scanf("%d",&n);
printf("Enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;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 sorted elemnts are:");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
Selection sort
#include <stdio.h>
void main()
{
int a[10],n,i,j,temp,min;
printf("Enter the value of n");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("THe sorted elemnts are:");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
int a[20][20],i,j;
printf("Enter the value of A Matrix:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("the resultant 2D A matrix is:");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf(" %d",a[i][j]);
}
}
}
#include <stdio.h>
void main()
{
int a[20][20],i,j,t[20][20];
printf("Enter the value of A Matrix:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
//Transpose Matrix//
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
t[i][j]=a[j][i];
}
}
printf("The Transpose matrix is:");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t %d",t[i][j]);
}
Matrix Multiplication
#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],r1,r2,c1,c2,i,j,k;
printf("enter the size of matrix A:");
scanf("%d%d",&r1,&c1);
printf("enter the size of matrix B:");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\n enter the matrix A elements");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("\n enter the matrix B elements");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\n the resultant matrix is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
else
{
printf("the matrix multiplication is not possible");
}