Xét sự tồn tại của một phần tử trong ma trận
March 07, 2023
Viết chương trình nhập vào một ma trận các số nguyên, gồm có M dòng N cột. In ra ma trận đó ra màng hình. Nhập một số nguyên bất kỳ và xét xem có phần tử nào của ma trận trùng với số này không? Ở vị trí nào? Có bao nhiêu phần tử?
Bài Giải
/*
Name: Xet su ton tai cua mot phan tu trong ma tran
Copyright: None
Author: Tran Anh
Description: https://gimi.vn
*/
#include<conio.h>
#include<stdio.h>
void inp(int t[][50], int u, int v)
{
for (int i=0;i<u;i++)
for (int j=0;j<v;j++)
{
printf("\t-Nhap phan tu A[%d][%d]: ",i,j);
scanf("%d",&t[i][j]);
}
}
void prt(int t[][50], int u, int v)
{
for (int i=0;i<u;i++)
{
for (int j=0;j<v;j++)
printf("%6d",t[i][j]);
printf("\n");
}
}
void process(int t[][50], int u, int v, int k)
{
int i,j,test=0,sl=0;
for (i=0;i<u;i++)
for (j=0;j<v;j++)
if (k==t[i][j]) test=1;
if (test)
printf("\n\t=> Phan tu %d CO ton tai trong ma tran!\n",k);
else
printf("\n\t=> Phan tu %d KHONG ton tai trong ma tran!\n",k);
if (test)
{
printf("\t=> Tai vi tri: \n\t");
for (i=0;i<u;i++)
for (j=0;j<v;j++)
if (k==t[i][j])
{
printf(" A[%d][%d]",i,j);
sl++;
}
printf("\n\t=> Co %d phan tu.",sl);
}
}
main()
{
int m,n;
printf("\t-Nhap so dong: "); scanf("%d",&m);
printf("\t-Nhap so cot: "); scanf("%d",&n);
int a[50][50];
inp(a,m,n);
printf("\t=> Ma tran ban da nhap: \n");
prt(a,m,n);
int k;
printf("\t- Nhap phan tu can tim: "); scanf("%d",&k);
process(a,m,n,k);
getch();
return 0;
}
Name: Xet su ton tai cua mot phan tu trong ma tran
Copyright: None
Author: Tran Anh
Description: https://gimi.vn
*/
#include<conio.h>
#include<stdio.h>
void inp(int t[][50], int u, int v)
{
for (int i=0;i<u;i++)
for (int j=0;j<v;j++)
{
printf("\t-Nhap phan tu A[%d][%d]: ",i,j);
scanf("%d",&t[i][j]);
}
}
void prt(int t[][50], int u, int v)
{
for (int i=0;i<u;i++)
{
for (int j=0;j<v;j++)
printf("%6d",t[i][j]);
printf("\n");
}
}
void process(int t[][50], int u, int v, int k)
{
int i,j,test=0,sl=0;
for (i=0;i<u;i++)
for (j=0;j<v;j++)
if (k==t[i][j]) test=1;
if (test)
printf("\n\t=> Phan tu %d CO ton tai trong ma tran!\n",k);
else
printf("\n\t=> Phan tu %d KHONG ton tai trong ma tran!\n",k);
if (test)
{
printf("\t=> Tai vi tri: \n\t");
for (i=0;i<u;i++)
for (j=0;j<v;j++)
if (k==t[i][j])
{
printf(" A[%d][%d]",i,j);
sl++;
}
printf("\n\t=> Co %d phan tu.",sl);
}
}
main()
{
int m,n;
printf("\t-Nhap so dong: "); scanf("%d",&m);
printf("\t-Nhap so cot: "); scanf("%d",&n);
int a[50][50];
inp(a,m,n);
printf("\t=> Ma tran ban da nhap: \n");
prt(a,m,n);
int k;
printf("\t- Nhap phan tu can tim: "); scanf("%d",&k);
process(a,m,n,k);
getch();
return 0;
}
Post a Comment