Simple C Programs on Array and matrix






Let US C.....








1./* Program to count the number of positive and negative numbers*/ 

#include< stdio.h >
void main( )
{
int a[50],n,count_neg=0,count_pos=0,I;
printf(“Enter the size of the arrayn”);
scanf(“%d”,&n);
printf(“Enter the elements of the arrayn”);
for I=0;I < n;I++)
scanf(“%d”,&a[I]);
for(I=0;I < n;I++)
{
if(a[I] < 0)
count_neg++;
else
count_pos++;
}
printf(“There are %d negative numbers in the arrayn”,count_neg);
printf(“There are %d positive numbers in the arrayn”,count_pos);
}

------------------------------------------------------------------------------------------------------------

2. /*Operation  min or max or sort in array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num[100],max,a,j,temp,r,min;
clrscr();
printf("\n\n\t 1.max no \n\n\t 2.min no \n\n\t 3.sorting \n\n\t 4.Exit");
printf("\n\n\t Enter your option=");
scanf("%d",&a);
printf("\n\n\t Enter your array= ");
scanf("%d",&r);
printf("\n\n\t Enter your array element= ");
switch(a)
{
case 1:
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
max=0;
for(i=0;i<r;i++)
if(num[i]>max)
{
max=num[i];
}
printf("\n\n\t Maximum value is %d.",max);
break;
case 2:
for(i=0;i<r;i++)
{
scanf("%d",&num[i]);
}
min=num[0];
for(i=0;i<r;i++)
if(num[i]<min)
{
min=num[i];
}
printf("\n\n\t Minimum value is %d.",min);
break;
case 3:


for(i=0;i<r;i++)
{
scanf("%d",&num[i]);
}

for(i=0;i<r;i++)
{
for(j=i+1;j<r;j++)
{

if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}

}
}

printf("\n\n\t Assending Order is = \n");

for(i=0;i<r;i++)
{
printf("\n\n\t %d",num[i]);
}
break;
case 4:
       printf("\n\n\t Programme is exited");
}
getch();
}

------------------------------------------------------------------------------------------------------------

3./*Average marks*/
#include<stdio.h>
#include<conio.h>
void main()
{
int avg,sum=0;
int i;
int marks[5];  /*array declaration*/
clrscr();
for (i=0;i<=4;i++)
{
printf("Enter marks=");
scanf("%d",&marks[i]);  /*store data in array*/
}
for (i=0;i<=4;i++)
sum=sum+marks[i];      /*read data from array*/

avg=sum/5;
printf("Average marks=%d\n",avg);
getch();
}
------------------------------------------------------------------------------------------------------------

4./*Addition of Array*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a1,a2;
int num1[10],num2[10],total[10];
clrscr();

printf("\n\n\t How many Elements want to store in 1st Array = ");
scanf("%d",&a1);

printf("\n\n\t How many Elements want to store in 2nd Array = ");
scanf("%d",&a2);


if(a1==a2)
{
printf("\n\n\t Enter First Array = \n");
for(i=0;i<a1;i++)
{
scanf("%d",&num1[i]);
}

printf("\n\n\t Enter Second Array = \n");
for(i=0;i<a2;i++)
{
scanf("%d",&num2[i]);
}

printf("\n\n\t Addition of Array is = ");
for(i=0;i<a1;i++)
{
total[i] = num1[i] + num2[i];
printf("\n\n\t %d",total[i]);
}
}
else
{
printf("\n\n\n\t Addition is not possible TRY AGAIN.........!");
}
getch();
}

------------------------------------------------------------------------------------------------------------

5./* example program to add two matrices & store the results in the 3rd matrix */ 
#include< stdio.h > 
#include< conio.h > 
void main() 
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; 
clrscr(); 
printf(“enter the order of the matrixn”); 
scanf(“%d%d”,&p,&q); 
if(m==p && n==q) 
printf(“matrix can be addedn”); 
printf(“enter the elements of the matrix a”); 
for(i=0;i < m;i++) 
for(j=0;j < n;j++) 
scanf(“%d”,&a[i][j]); 
printf(“enter the elements of the matrix b”); 
for(i=0;i < p;i++) 
for(j=0;j < q;j++) 
scanf(“%d”,&b[i][j]); 
printf(“the sum of the matrix a and b is”); 
for(i=0;i < m;i++) 
for(j=0;j < n;j++) 
c[i][j]=a[i][j]+b[i][j]; 
for(i=0;i < m;i++) 
for(j=0;j < n;j++) 
printf(“%dt”,&a[i][j]); 
printf(“n”); 

Share on Google Plus

About Dattatray

Discover more,Learn more !!!