What's new

# C & C++Two Dimensional Array Program

J

#### Jeanh

, Last edited by a moderator:
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:

#### globetrotters

Established
Thanks for sharing!

Comment
J

welcome po

Comment

#### genji

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?

Comment
J

#### Jeanh

try mo sa turbo c

Comment

ok thanks

Comment
J

welcome po

Comment

#### klevie_04

##### Grasshopper
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.

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

declare variable i first:

int i;

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

try mu

Comment

#### genji

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

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

Comment

#### mirG

##### Forum Expert
Thank you for sharing!

Comment

#### klevie_04

##### Grasshopper
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:

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

Comment
J

opo bossing

Comment

#### klevie_04

##### Grasshopper
java bozz magaling kah???

Comment
J

#### Jeanh

di po weh..pero may thread po ako ng java..visit mo na lang po

Comment

Members online
1,269
Guests online
838
Total visitors
2,107