What's new

Closed Two Dimensional Array Program

Status
Not open for further replies.
J

Jeanh

Guest
How to find boundary elements and higher and lower level of a matrix.



Code:
#include<stdio.h>

#include<conio.h>
void main()
{
int a[5][5],m,n,sum=0;
clrscr();
printf("Enter row :");
scanf("%d",&m);
printf("Enter coloum :");
scanf("%d",&n);
printf("Enter matrix element :\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix is : \n");
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nBoundary element :");
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if((i*j==0)||(i==m-1)||(j==n-1))
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
}
}
printf("\nSum of boundary element is : %d",sum);

printf("\nHigher level : ");
sum=0;
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{

printf("\t%d",a[i][j]);
sum=sum+a[i][j];
}
}

}
printf("\nSum of higher level = %d",sum);

printf("\nLower lavel : ");
sum=0;
i=m;
for(int j=0;j<n;j++)
{
i=i-1;
printf("\t%d",a[i][j]);
sum=sum+a[i][j];
}
printf("\nSum of lower level = %d",sum);
getch();
}

Output:

boundary.jpg

13153523_1106774132678828_1932984573_n.png
 

Attachments

Last edited by a moderator:
thanks

Sir bakit di po ma run sakin? c gamit ko
sabi 'for' loop initial declaration are only allowed in c99
no kaya problema nito?
 
thanks

Sir bakit di po ma run sakin? c gamit ko
sabi 'for' loop initial declaration are only allowed in c99
no kaya problema nito?
alam ku problema jan boz. kinakailangan nang initialize ang declaration ng value variable sa c++ na latest. example.

instead:

for(int i=1; i<=10; i++)

declare variable i first:

int i;

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

try mu
 
alam ku problema jan boz. kinakailangan nang initialize ang declaration ng value variable sa c++ na latest. example.

instead:

for(int i=1; i<=10; i++)

declare variable i first:

int i;

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

try mu


Ganun pala yun. . Thanks boz nag run na hehe
 
How to find boundary elements and higher and lower level of a matrix.



Code:
#include<stdio.h>

#include<conio.h>
void main()
{
int a[5][5],m,n,sum=0;
clrscr();
printf("Enter row :");
scanf("%d",&m);
printf("Enter coloum :");
scanf("%d",&n);
printf("Enter matrix element :\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix is : \n");
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nBoundary element :");
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if((i*j==0)||(i==m-1)||(j==n-1))
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
}
}
printf("\nSum of boundary element is : %d",sum);

printf("\nHigher level : ");
sum=0;
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{

printf("\t%d",a[i][j]);
sum=sum+a[i][j];
}
}

}
printf("\nSum of higher level = %d",sum);

printf("\nLower lavel : ");
sum=0;
i=m;
for(int j=0;j<n;j++)
{
i=i-1;
printf("\t%d",a[i][j]);
sum=sum+a[i][j];
}
printf("\nSum of lower level = %d",sum);
getch();
}

Output:

boundary.jpg

13153523_1106774132678828_1932984573_n.png
pwede na yan para sa suduko game bozz, seed random numbers nalang kulang.
 

Attachments

di po weh..pero may thread po ako ng java..visit mo na lang po
 
Status
Not open for further replies.

Similar threads

Back
Top