Code:
#include "stdafx.h" #include "stdio.h" #include "conio.h" #include "time.h" #include "stdlib.h" #include "windows.h" #include "math.h" #define max 100 void nhap(int a[][max], int &n ) { do{ printf("\n Nhap ma tran cap n (n>=2n&&<=50)n="); scanf_s("%d",&n); } while (n<2&&n>50); srand((unsigned)time(NULL)); for (int i=0;i<n;i++) { for (int j=0;j<n;j++) a[i][j]=rand()%100; } } void xuat (int a[][max], int n) { printf("\n"); printf("\n\t"); for (int i=0;i<n;i++) { for (int j=0;j<n;j++) printf("%4d",a[i][j]); printf("\n\t"); } } void tansuat(int a[][max], int n) { printf("Tan so xuat hien cac phan tu trong ma tran"); int i,j; int ts[max][max]; for(int i=0; i<n; i++) for(int j=0; j<n; j++) ts[i][j] = 1; // Khoi tao tan suat = 0 for(int i=0; i<n; i++) for(int j=0; j<n; j++) { for(int x=0; x<i; x++) for(int y=0; y<j; y++) if(a[i][j] == a[x][y]) break; else { for(int c=i;c<n; c++) for(int d=j+1;d<n; d++) if(a[i][j] == a[c][d]) ts[i][j]++; } } for (i=0; i<n; i++) for(j=0; j<n; j++) if(ts[i][j] > 0) printf(" \n\t| %3d ....%3d| lan\n",a[i][j],ts[i][j]); } void chonthaotac() { printf("\n*-1: Xoa man hinh"); printf("\n* 0: Thoat "); printf("\n* 1: Nhap"); printf("\n* 2: Xuat"); printf("\n* 3: Tan suat \n "); } void main() { int a[max][max],n,thaotac; do { printf("\n CHON THAO TAC "); chonthaotac(); scanf("%d",&thaotac); if (thaotac==0) exit(0); else switch(thaotac) { case -1: system("cls"); case 1: nhap(a,n); break; case 2: xuat(a,n); break; case 3: printf("\n Tan suat xuat hien cua phan tu trong mang"); tansuat(a,n); break; } } while (1); _getch(); }
Comment