[ create a new paste ] login | about

Link: http://codepad.org/0LERegNW    [ raw code | output | fork ]

C, pasted on May 6:
/* Bài 380/95/SBT Thầy NTTMK:Đếm số lượng giá trị chẵn nhỏ nhất trong ma trận các số nguyên. 285.cpp */
/* 

Tác giả: Nguyễn Việt Nam Sơn
Trung tâm đào tạo tin học - Thiết kế phần mềm - Sơn Đẹp Trai: www.SonDepTrai.com

Nguồn source code này Tôi viết vào năm 2012 lúc mới bắt đầu học lập trình C/C++ nên một số cách sẽ không được tối ưu - Bạn chỉ nên dùng trên tinh thần tham khảo thôi nhé.
Mong giúp đỡ được Bạn trên con đường Học Lập Trình.
TẤT CẢ VÌ SỰ THÀNH CÔNG CỦA BẠN

*/
#include<stdio.h>
#include<conio.h>
#include<limits.h>
#define MAX 100
#define bool

void nhapmatran(int a[MAX][MAX],int &n,int &m)
{
	do{
		printf("Nhap vao so dong cua ma tran:m=");
		scanf("%d",&m);
		if(m<1||m>MAX)
			printf("So dong ban nhap vao khong hop le!Xin vui long nhap lai!\n");
		else
			break;
	}while(m<1||m>MAX);
	do{
		printf("Nhap vao so cot cua ma tran:n=");
		scanf("%d",&n);
		if(n<1||n>MAX)
			printf("So cot ban nhap vao khong hop le!Xin vui long nhap lai!\n");
		else
			break;
	}while(n<1||n>MAX);
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("Nhap vao a[%d][%d]=",i,j);
			scanf("%d",&a[i][j]);
		}
	}
}

void xuatmatran(int a[MAX][MAX],int n,int m)
{
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
}

void xulydulieu(int a[MAX][MAX],int n,int m)
{
	int min=INT_MAX,dem=0,Co;
	bool Co=false;
	for(int i=0;i<m;i++) // vòng lặp đầu tiên này là để tìm ra giá trị chẵn nhỏ nhất.
	{
		for(int j=0;j<n;j++)
		{
			if(a[i][j]%2==0&&a[i][j]<min)
				min=a[i][j];
		}
	}
	for(int i=0;i<m;i++)	// vòng lặp thứ hai này là để so sánh giá trị chẵn nhỏ nhất vừa tìm được với các giá trị trong mảng.
	{
		for(int j=0;j<n;j++)
		{
			if(min==a[i][j])	// ở đây cho a[i][j]==min thì kết quả cũng như nhau
				dem++;
		}
	}
	printf("\nSo luong gia tri chan nho nhat co trong ma tran la:%d",dem);
}

void main()
{
	int a[MAX][MAX],n,m,tieptuc;
	quaylai:nhapmatran(a,n,m);
	printf("\n>>>>>>>>>>>>>>>MA TRAN VUA NHAP LA:<<<<<<<<<<<<<<<<<<<<<\n");
	xuatmatran(a,n,m);
	xulydulieu(a,n,m);
	printf("\nBan co muon tiep tuc chay chuong trinh khong ? Neu co bam phim C,nguoc lai bam bat ky 1 phim nao khac de ket thuc\n");
	tieptuc=getch();
	if(tieptuc=='c'||tieptuc=='C')
		goto quaylai;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Line 17: error: conio.h: No such file or directory
Line 18: error: expected ';', ',' or ')' before '&' token
In function 'xuatmatran':
Line 48: error: 'for' loop initial declaration used outside C99 mode
Line 50: error: 'for' loop initial declaration used outside C99 mode
In function 'xulydulieu':
Line 61: error: 'false' undeclared (first use in this function)
Line 61: error: (Each undeclared identifier is reported only once
Line 61: error: for each function it appears in.)
Line 62: error: 'for' loop initial declaration used outside C99 mode
Line 64: error: 'for' loop initial declaration used outside C99 mode
Line 70: error: redefinition of 'i'
Line 62: error: previous definition of 'i' was here
Line 70: error: 'for' loop initial declaration used outside C99 mode
Line 72: error: 'for' loop initial declaration used outside C99 mode
In function 'main':
Line 82: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: