What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

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:

http://3.bp.phc.onl/#forbidden#/-JLOqzmiT0w4/Tlyvz8_THOI/AAAAAAAAACA/hMadlan9Yg0/s320/boundary.JPG

 

genji

Addict
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

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.

instead:

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

declare variable i first:

int i;

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

try mu
 
Comment

genji

Addict
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
 
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:

http://3.bp.phc.onl/#forbidden#/-JLOqzmiT0w4/Tlyvz8_THOI/AAAAAAAAACA/hMadlan9Yg0/s320/boundary.JPG

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

Jeanh

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

Online statistics

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

Forum statistics

Threads
811,929
Messages
16,061,399
Members
1,436,113
Top