[go: up one dir, main page]

0% found this document useful (0 votes)
8 views8 pages

Arrays Simple Programs

Uploaded by

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

Arrays Simple Programs

Uploaded by

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

Arrays programs

Assigning the values: Program to print 2,4,6,8-----20 using arrays.

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

Initialize the array value

#include<stdio.h>
void main()
{
int i,a[5]={1,2,3,4,5};
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}

Sum of N numbers using arrays


#include<stdio.h>
void main()
{
int sum=0,i,n,a[10];
printf("Enter the value of n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the %d value:",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("The sum of N Number is :%d", sum);
}

Sum and average using arrays


#include<stdio.h>
void main()
{
int sum=0,i,n,Marks[10], avg;
printf("ENter the value of n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the %d subject Marks:",i+1);
scanf("%d",&Marks[i]);
sum=sum+Marks[i];
avg=sum/n;
}
printf("The sum of all the subject is :%d\n Average is:%d", sum,avg);
}

Sum of numbers using arrays

#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);
}

Read and display of array elements


#include <stdio.h>
void main()
{
int a[20],i,n;
printf("Enter the value of N");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("array[%d]=",i);
scanf("%d",&a[i]);
}
printf("The array elements are\n");
for(i=0;i<n;i++)
{
printf(" array[%d]=%d",i,a[i]);
}
}

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");

for (i= 0; i < n; i++)


{
scanf("%d", &a[i]);
}
printf("Enter a number to search\n");
scanf("%d", &key);

for (i = 0; i < n; i++)


{
if (a[i] ==key) /* If required element is found */
{
printf("%d is present at location %d\n", key, i);
break;
}
}
if (a[i]!=key)
{
printf("%d isn't present in the array.\n", key);
}

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

Read and display of 2D Matrix


#include <stdio.h>
void main()
{

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

Program for transpose Matrix

#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");
}

You might also like