[ create a new paste ] login | about

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

C, pasted on May 6:
/* Bài 387/96/SBT Thầy NTTMK:Liệt kê các dòng có nhiều số chẵn nhất trong ma trận các số nguyên . 418.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>	// Hàm có tác dụng tìm giá trị lớn nhất và nhỏ nhất của kiểu số nguyên (int) .
#define MAX 100
#define bool

void nhapmatran(int a[MAX][MAX],int &m,int &n)
{
	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 m,int n)
{
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
}

void lietkesoluongcacsochantrongcacdongvatimdongconhieusochannhat(int a[MAX][MAX],int m,int n)
{
	int b[MAX],max=INT_MIN,vitri=0,Co;
	bool Co=false;
	for(int i=0;i<m;i++)
	{
		int dem=0;	//dem=0 phải để ở đây để mỗi lần đếm xong 1 dòng nó lại trả về giá trị 0 như cũ rồi tiếp tục đếm sang dòng mới,nếu không nó sẽ đem kết quả vừa đếm ở dòng trên tiếp tục đếm thêm ở dòng tiếp theo.
		for(int j=0;j<n;j++)
		{
			if(a[i][j]%2==0)
			{
				dem++;
				b[i]=dem;
				Co=true;	// ở đây ta cần đặt thêm biến bool để khi trong ma trận không có dòng nào chứa số chẵn thì nó sẽ báo lỗi ra chứ nếu không thì tuy không có 1 dòng nào chứa số chẵn nó vẫn in ra các dòng chứa nhiều số chẵn nhất là : tất cả các dòng .
			}
		}
	}
	if(Co==true)
	{
		for(int i=0;i<m;i++)
			printf("\nSo luong cac so chan cua dong %d la:%d",i,b[i]);
	}
	else
		printf("\nKhong co dong nao co chua so chan");
	if(Co==true)
	{
		for(int i=0;i<m;i++)
		{
			if(b[i]>max)
			{
				max=b[i];
			}
		}
		printf("\nCac dong chua nhieu so chan nhat la:");
		for(int i=0;i<m;i++)
		{
			if(b[i]==max)
			{
				vitri=i;
				printf("%4d",vitri);
			}
		}
	}
}

void main()
{
	int a[MAX][MAX],m,n,tieptuc;
	quaylai:nhapmatran(a,m,n);
	printf("\n>>>>>>>>>>>>>>>>>>>>MA TRAN VUA NHAP LA:<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
	xuatmatran(a,m,n);
	lietkesoluongcacsochantrongcacdongvatimdongconhieusochannhat(a,m,n);
	printf("\nBan co muon tiep tuc chay chuong trinh hay 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
18
19
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 'lietkesoluongcacsochantrongcacdongvatimdongconhieusochannhat':
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 65: error: 'for' loop initial declaration used outside C99 mode
Line 71: error: 'true' undeclared (first use in this function)
Line 77: error: 'for' loop initial declaration used outside C99 mode
Line 84: error: 'for' loop initial declaration used outside C99 mode
Line 92: error: redefinition of 'i'
Line 84: error: previous definition of 'i' was here
Line 92: error: 'for' loop initial declaration used outside C99 mode
In function 'main':
Line 104: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: