Diễn Đàn Chia SẻĐăng Nhập

Trang chia sẻ tài liệu, kiến thức, thủ thuật... và mọi thứ bạn cần

Khi đàn ông im lặng là họ đang suy nghĩ, khi đàn bà im lặng là họ đang suy diễn

Tuyển chọn các dạng bài tập C++ hay

bài tập C++ chọn lọc

Câu 1: Giải phương trình bậc nhất.

Spoiler

PHP:

#include <stdio.h>

void main()

{

float a, b;

printf("\nGiai phuong trinh bac nhat AX + B = 0");

printf("\nCho biet ba he so A B : ");

scanf("%f%f", &a, &b);

if (a==0)

if (b!=0)

printf("Phuong trinh vo nghiem");

else

printf("Phuong trinh co nghiem khong xac dinh");

else

printf("Dap so cua phuong trinh tren = %f", -b/a);

getch();

}

Câu 2: Giải phương trình bậc hai.

Spoiler

PHP:

#include <stdio.h>

#include <math.h>

void main()

{

float x,x1,x2,a,b,c,delta;

printf("Nhap vao tham so cua phuong trinh bac 2 \(a b c\) : "); scanf("%f%f%f",&a,&b,&c);

if (a==0)

if (b==0) printf("Phuong trinh vo nghiem\n");

else {x=-c/b; printf("Phuong trinh co mot nghiem x = %.3f\n",x);}

else {

delta=b*b-4*a*c;

if (delta<0)

printf("Phuong trinh vo nghiem thuc\n");

else

if (delta==0)

{x=-b/(2*a);

printf("Phuong trinh co mot nghiem duy nhat x = %.3f\n",x);}

else {

x1=(-b+sqrt(delta))/(2*a);

x2=(-b-sqrt(delta))/(2*a);

printf("Phuong trinh co nghiem 2 nghiem thuc :\n x1 = %.3f\n x2 = %.3f\n",x1,x2);

}

}

}

Câu 3: Giải hệ phương trình bậc nhất.

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

void main()

{

int a, b, c, d, e, f, dthuc;

float x, y;

printf("\nNhap vao cac he so a,b,c,d,e,f : ");

scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);

dthuc = b*d - e*a;

if (dthuc != 0)

{

y = (float)(c*d-a*f)/dthuc;

x = (float)(b*f-c*e)/dthuc;

printf("Nghiem x = %f, y = %f", x, y);

}

else

printf("\nHe phuong trinh vo ngiem.");

getch();

}

Câu 4: Tính căn bậc 2 theo phương pháp hàm Newton

Spoiler

PHP:

#include <stdio.h>

#include <math.h>

void main()

{

double a, xn, ketqua;

printf("\nNhap vao so muon tinh can bac hai : ");

scanf("%lf", &a);

xn = (a+1)/2;

do {

ketqua = xn;

xn = 0.5 * (xn + a/xn);

} while (fabs(xn-ketqua) > 0.0001);

printf("\nKet qua = %lf", xn);

getch();

}

Câu 5: In ra màn hình tam giác cân

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

void main() {

clrscr();

short d[]={1,2};

printf("Nhap chieu cao tam giac: ");

int i,num,space;

scanf("%d",&i);

num=2*i+1;

for(int n=0;n<i;++n) {

space=(num-(2*n+1))/2;

while(space-->0)

printf(" ");

int index;

for(int j=0;j<2*n+1;++j) {

index=(j%2)?1:0;

printf("%d",d[index]);

}

printf("\n");

}

getch();

}

Câu 6: In ra bảng cửu chương.

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

{

int i, j;

char chuoi[] = "B A N G C U U C H U O N G";

char ten[10][5] = {"","","Hai", "Ba", "Bon", "Nam",

"Sau", "Bay", "Tam", "Chin"};

clrscr();

textcolor(YELLOW);

gotoxy((80 - strlen(chuoi)) / 2, 1);

cprintf("%s\n\n", chuoi);

for (i=2; i<=9; i++)

{

gotoxy(10*(i-2) + (10 - strlen(ten[i]))/2, 4);

textcolor(i);

cprintf("%s", ten[i]);

}

for (j=1; j<=10; j++)

for (i=2; i<=9; i++)

{

gotoxy(10*(i-2) + 1, j+4);

textcolor(i);

cprintf("%dx%2d = %2d", i, j, i*j);

}

getch();

}

Câu 7: In ra năm âm lịch tương ứng với năm nhập vào.

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

void main()

{

unsigned nam;

char can[][5] = {"Giap", "At", "Binh", "Dinh", "Mau", "Ky",

"Canh", "Tan", "Nham", "Quy"};

char chi[][5] = {"Ty", "Suu", "Dan", "Meo", "Thin", "Ty",

"Ngo", "Mao", "Than", "Dau", "Tuat", "Hoi"};

printf("\nNhap nam can biet : ");

scanf("%d", &nam);

printf("Nam am lich cua %d la %s %s", nam, can[(nam+6)%10], chi[(nam+Cool%12]);

getch();

}

Câu 8 Tính số ngày trong một tháng, một năm bất kỳ

Spoiler

PHP:

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

{

long int thang, nam;

cout<<"Nhap thang va nam: ";

cin>>thang>>nam;

if ((thang>12||thang<1)||(nam<0))

cout<<"Nhap thang hoac nam sai";

else

{

bool namnhuan=((nam%4==0&&nam%100!=0)||(nam%400==0&&nam%100==0));

int songay;

if(thang==4||thang==6||thang==9||thang==11)

songay = 30;

else

{

if(thang==2)

songay = namnhuan?29:28;

else

songay=31;

}

cout<<"So ngay cua thang "<<thang<<" trong nam "<<nam<<" la: "<<songay<<endl;

}

getch();

}

Câu 9: Nhập chuỗi và in chuỗi

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

void main()

{

char name[80];

printf("\nXin cho biet ten cua ban : ");

gets(name);

printf("Chao %s\n", name);

getch();

}

Câu 10: Đảo chuỗi

Spoiler

PHP:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

int main ()

{

clrscr ();

char* p;

int i,n;

p = (char*)malloc(128);

printf("\n Nhap xau ki tu :");

gets (p);

n = strlen(p);

printf("\n xau dao: \n");

for( i = n-1; i>=0; i --)

putchar(p[i]);

printf("\n");

free(p);

getch ();

return 0;

}

Câu 11: Đếm số lần xuất hiện của các ký tự trong chuỗi

Spoiler

PHP:

#include <stdio.h>

#include <ctype.h>

void main()

{

char chuoi[80];

int i = 0, count = 0;

printf("\nNhap vao mot chuoi bat ky : ");

gets(chuoi);

while (chuoi[i] != 0)

{

if (isalpha(chuoi[i++]))

count++;

}

printf("So ky tu trong chuoi = %d", count);

getch();

}

Câu 12: Loại bỏ khoảng trống thừa trong chuỗi.

Spoiler

PHP:

#include <stdio.h>

#include <string.h>

#include <conio.h>

#pragma warn -pia

char *trim(char *chuoi)

{

char *p;

while (p = strstr(chuoi, " "))

memmove(p, p+1, strlen(chuoi) - (p - chuoi));

if (chuoi[0] == ' ')

memmove(chuoi, chuoi+1, strlen(chuoi) - 1);

if (chuoi[strlen(chuoi)-1] == ' ')

chuoi[strlen(chuoi)-1] = 0;

return chuoi;

}

void main()

{

char chuoi[125];

printf("\nNhap chuoi mau : ");

textattr(0x1e);

gets(chuoi);

trim(chuoi);

printf("\nChuoi sau khi da trim : ");

textattr(0x1e);

cputs(chuoi);

getch();

}

Câu 13: In chuỗi theo các từ mỗi từ một dòng

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

{

char s[50];

int i, len;

printf("\nNhap vao mot chuoi : ");

gets(s);

len = strlen(s);

i = 0;

while (i<len)

{

while (s[i] == ' ' && i<len)

i++;

while (s[i] != ' ' && i<len)

putc(s[i++], stdout);

putc('\n', stdout);

}

getch();

}

Câu 14: Chương trình đếm số ký tự trong một chuỗi ASCII

Spoiler

PHP:

#include <stdio.h>

#include <ctype.h>

#include<conio.h>

void main()

{

char chuoi[80];

int i = 0, count = 0;

printf("\nNhap vao mot chuoi bat ky : ");

gets(chuoi);

while (chuoi[i] != 0)

{

if (isalpha(chuoi[i++]))

count++;

}

printf("So ky tu trong chuoi = %d", count);

getch();

}

Câu 15: Cho biết kích thước 1 file

Spoiler

PHP:

#include <stdio.h>

#include <io.h>

#include <fcntl.h>

#include <sys\stat.h>

int main()

{

int fp;

long file_size;

if ((fp = open("f:/cprojects/urls.txt", O_RDONLY)) == -1)

printf("Error opening the file \n");

else

{

file_size = filelength(file_handle);

printf("The file size in bytes is %ld\n", file_size);

close(fp);

}

return 0;

}

Câu 16: Đọc nội dung 1 file

Spoiler

PHP:

#include <stdio.h>

void main(void)

{

FILE *fp;

char ch;

fp = fopen("websites.txt","r");

ch = getc(fp);

while(ch!=EOF)

{

putchar(ch);

ch = getc(fp);

}

printf("\n\n");

}

Câu 17: Đếm tần suất 1 kí tự trong 1 file

Spoiler

PHP:

# include <stdio.h>

# include <string.h>

main()

{

FILE *fp;

char in[100];

long int freq[257];

int i;

printf("\nFile frequency table generator\n\n");

printf("\nInput file:");

scanf("%s",in);

fp=fopen(in,"rb");

if(fp==NULL)

{

printf("\nCould not open input file.Aborting\n");

return 1;

}

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

freq[i]=0;

while(i=fgetc(fp),i!=EOF)

{

freq[i]++;

}

fcloseall();

fp=fopen("count.txt","w");

fprintf(fp,"\nCharacter frequency table of %s\n",in);

fprintf(fp,"\nCharacter ASCII frequency\n\n");

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

{

if(i==26)

{

fprintf(fp,"\t 26\t %ld\n",freq[26]);

}

else if(i==9)

{

fprintf(fp,"\t 9\t %ld",freq[9]);

}

else if(i<10)

{

fprintf(fp,"%c\t %d\t %ld\n",i,i,freq[i]);

}

else if(i<100)

{

fprintf(fp,"%c\t %d\t %ld\n",i,i,freq[i]);

}

else

{

fprintf(fp,"%c\t %d\t %ld\n",i,i,freq[i]);

}

}

fcloseall();

printf("\nFrequency table copied to count.txt\n");

}

Câu 18: Cho biêt thông tin FAT

Spoiler

PHP:

#include <stdio.h>

#include <dos.h>

void main(void)

{

struct fatinfo fat;

getfatd(&fat);

printf("Sectors per cluster %d\n", fat.fi_sclus);

printf("Clusters per disk %u\n", fat.fi_nclus);

printf("Bytes per cluster %d\n", fat.fi_bysec);

printf("Disk type %x\n", fat.fi_fatid & 0xFF);

}

Câu 19: Bài toán trâu ăn cỏ

Spoiler

PHP:

/* Giai bai toan co :

Tram trau tram co

Trau dung an nam

Trau nam an ba

Trau gia an mot

*/

#include <stdio.h>

#include <conio.h>

void main()

{

int tdung, tnam, tgia, phuongan=0;

for (tdung = 1; tdung <= 98; tdung ++)

for (tnam = 1; tnam < 99 - tdung; tnam ++)

for (tgia = 1; tgia < 99 - (tdung + tnam); tgia++)

if ((tdung*5 + tnam*3 + tgia) == 100)

{

printf("\nTrau dung : %5d ; Trau nam : %5d ; Trau gia : %5d",

tdung, tnam, tgia);

phuongan++;

}

printf("\nTong cong co %d phuong an.", phuongan);

getch();

}

Câu 20: Bài toán 8 Hoàng Hậu

Spoiler

PHP:

#include <stdio.h>

#include<conio.h>

int dong[8], cot[8], cheoxuoi[15], cheonguoc[15];

void print ()

{

int i;

printf("\n");

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

printf("%3d", dong[i]);

}

void thu(int i)

{

int j;

for (j=0; j<8; j++)

{

if (cot[j] == 1 && cheoxuoi[i+j] ==1 && cheonguoc[i-j+7] == 1)

{

dong[i] = j;

cot[j] = 0;

cheoxuoi[i+j] = 0;

cheonguoc[i-j+7] = 0;

if (i<7)

thu(i+1);

else

print();

cot[j] = 1;

cheoxuoi[i+j] = 1;

cheonguoc[i-j+7] = 1;

}

}

}

void tim()

{

int i, q;

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

{

cot[i] = 1;

dong[i] = -1;

}

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

{

cheoxuoi[i] = 1;

cheonguoc[i] = 1;

}

thu(0);

}

void main()

{

tim();

getch();

}

Câu 21 Bài kiểm tra số nguyên tố

Spoiler

PHP:

#include<iostream.h>

#include<conio.h>

#include<math.h>

bool LaNguyenTo(int n);

void main()

{

int n;

cout<<"Nhap vao mot so bat ky: ";cin>>n;

if(LaNguyenTo(n))

cout<<"La so nguyen to";

else

{

cout<<"Khong la so nguyen to vi no chia het cho ";

for(int j=2;j<n;j++)

{

if(n%j==0)

cout<<j<<" va ";

}

cout<<"The thoi";

}

getch();

}

bool LaNguyenTo(int n)

{

bool co=true;

if(n<2)

{cout<<"Du lieu nhap sai va so do ";

return !co;}

else

{

for(int i=2;i<=sqrt(n)+1;i++)

{

if(n%i==0)

{

co=false;

break;

}

}

}

return co;

}

Câu 22: Liệt kê các hoán vị của N phần tử

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

#define MAX 10

int mang[MAX], n;

void swap (int *x, int *y)

{

int tmp;

tmp = *x;

*x = *y;

*y = tmp;

}

void hoanvi(int k)

{

int j;

if (k==1)

{

printf("\n");

for (j=0; j<n; j++)

printf("%d ", mang[j]);

}

else

for (j=k-1; j>=0; j--)

{

swap(&mang[k-1], &mang[j]);

hoanvi(k-1);

swap(&mang[j], &mang[k-1]);

}

}

void main()

{

int i;

printf("\nCho biet so phan tu (N < 10) : ");

scanf("%d", &n);

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

mang[i] = i;

hoanvi(n);

getch();

}

Câu 23: Tính tổ hợp chập K của N phần tử

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

unsigned long giaithua(int n)

{

unsigned long ketqua = 1;

int i;

for (i=2; i<=n; i++)

ketqua *= i;

return ketqua;

}

unsigned long to_hop_chap(int k, int n)

{

return giaithua(n) / (giaithua(k) * giaithua(n-k));

}

void main()

{

int n, k;

printf("\nNhap vao gia tri N va K : ");

scanf("%d%d", &n, &k);

printf("Top hop chap %d cua %d = %lu", k, n, to_hop_chap(k, n));

getch();

}

Câu 24: Bài tập tìm max min của 4 số.

Spoiler

PHP:

#include<iostream.h>

#include<conio.h>

#include<math.h>

float max(int a, int b);

float min(int a, int b);

void main()

{

int a,b,c,d;

cout<<"Nhap vao so thu 1: ";cin>>a;

cout<<"Nhap vao so thu 2: ";cin>>b;

cout<<"Nhap vao so thu 3: ";cin>>c;

cout<<"Nhap vao so thu 4: ";cin>>d;

cout<<"max= "<<max(max(max(a,b),max(b,c)),max(c,d))<<" min= "<<min(min(min(a,b),min(b,c)),min(c,d))<<endl;

cout<<"Vay trung binh cong cua 4 so do la: "<<(a+b+c+d)/4;

getch();

}

float max(int a, int b)

{

if(a>b)

return a;

else

return b;

}

float min(int a, int b)

{

if(a>b)

return b;

else

return a;

}

Câu 25: Chương trình đọc số có 3 chữ số

Spoiler

PHP:

#include<iostream.h>

#include<conio.h>

void main()

{

int a;

cout<<"Nhap vao mot so co 3 hay 2 tham chi 1 chu so: ";

cin>>a;

int hangtram=a/100;

int hangchuc=((a/10)%10);

int hangdonvi=a%10;

switch(hangtram)

{

case 1 :cout<<"Mot tram ";break;

case 2 :cout<<"Hai tram ";break;

case 3 :cout<<"Ba tram ";break;

case 4 :cout<<"Bon tram ";break;

case 5 :cout<<"Nam tram ";break;

case 6 :cout<<"Sau tram ";break;

case 7 :cout<<"Bay tram ";break;

case 8 :cout<<"Tam tram ";break;

case 9 :cout<<"Chin tram ";break;

}

switch(hangchuc)

{

case 0:

if(hangdonvi==0)

cout<<"";

else

{if(hangchuc==0&&hangtram==0)

cout<<"";

else

cout<<"ninh";}

break;

case 1:cout<<"muoi";break;

case 2:cout<<"hai muoi";break;

case 3:cout<<"ba muoi";break;

case 4:cout<<"bon muoi";break;

case 5:cout<<"nam muoi";break;

case 6:cout<<"sau muoi";break;

case 7:cout<<"bay muoi";break;

case 8:cout<<"tam muoi";break;

case 9:cout<<"chin muoi";break;

}

switch(hangdonvi)

{

case 0:cout<<"Khong";break;

case 1:

if(hangchuc==1||(hangtram==0&&hangchuc==0))

cout<<" mot";

else

cout<<" mo't";

break;

case 2:cout<<" hai";break;

case 3:cout<<" ba";break;

case 4:

if(hangchuc==1)

cout<<" bon";

else

{if(hangchuc==0&&hangtram==0)

cout<<"bon";

else

cout<<" tu";}

break;

case 5:

if(hangchuc==0&&hangtram==0)

cout<<" nam";

else

cout<<" lam";

break;

case 6:cout<<" sau";break;

case 7:cout<<" bay";break;

case 8:cout<<" tam";break;

case 9:cout<<" chin";break;

}

getch();

}

Câu 26 Tìm phần tử lớn nhất nhỏ nhất trong mảng một chiều

Spoiler

PHP:

#include <conio.h>

#include <stdlib.h>

void main()

{

int mang[20];

int i, minval, maxval;

/* Khoi tao mang ngau nhien */

randomize();

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

mang[i] = random(100);

/* Tim gia tri lon nhat va nho nhat */

minval = maxval = mang[0];

for (i=1; i<20; i++)

{

if (maxval < mang[i])

maxval = mang[i];

else if (minval > mang[i])

minval = mang[i];

}

/* In mang */

clrscr();

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

{

if (mang[i] == maxval)

textcolor(YELLOW);

else if (mang[i] == minval)

textcolor(RED);

else

textcolor(WHITE);

cprintf("%3d", mang[i]);

}

getch();

}

Câu 27: Trộn 2 dãy giảm thành 1 dãy tăng

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

#define MAX 10

void main()

{

int a[MAX], b[MAX], c[2*MAX], n1, n2, i, i1, i2;

printf("\nCho biet so phan tu cua mang thu nhat : ");

scanf("%d", &n1);

printf("Nhap vao cac phan tu (giam dan) cua mang thu nhat : ");

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

scanf("%d", &a[i]);

printf("\nCho biet so phan tu cua mang thu hai : ");

scanf("%d", &n2);

printf("Nhap vao cac phan tu (giam dan) cua mang thu hai : ");

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

scanf("%d", &b[i]);

i1 = n1-1;

i2 = n2-1;

for (i=0; i<n1 + n2; i++)

{

if (i1 < 0 || i2 < 0)

break;

if (a[i1] < b[i2])

{

c[i] = a[i1];

i1--;

}

else

{

c[i] = b[i2];

i2--;

}

}

if (i1 >= 0)

while (i1 >= 0)

c[i++] = a[i1--];

if (i2 >= 0)

while (i2 >= 0)

c[i++] = b[i2--];

printf("\nCac phan tu cua mang tron : ");

for (i=0; i<n1+n2; i++)

printf("%d ", c[i]);

getch();

}

Câu 28: Dãy tăng dần

Spoiler

PHP:

#include <stdio.h>

void main()

{

int a[10], i, maxstart, maxend, maxlen, tmpstart, tmpend, tmplen;

printf("\nNhap vao 10 phan tu nguyen cua day :");

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

scanf("%d", &a[i]);

printf("Day da cho :\n");

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

printf("%6d", a[i]);

maxstart = maxend = tmpstart = tmpend = 0;

maxlen = tmplen = 1;

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

{

if (a[i] < a[tmpend])

{

if (maxlen < tmplen)

{

maxstart = tmpstart;

maxend = tmpend;

maxlen = tmplen;

}

tmpstart = tmpend = i;

tmplen = 1;

}

else

{

tmplen++;

tmpend++;

}

}

if (maxlen < tmplen)

{

maxstart = tmpstart;

maxend = tmpend;

}

printf("\nDay tang co so phan tu nhieu nhat la : \n");

for (i=maxstart; i<=maxend; i++)

printf("%6d", a[i]);

getch();

}

Câu 29: Dãy tăng có dãy dài nhất

Spoiler

PHP:

#include <stdio.h>

void main()

{

int a[10], i, maxstart, maxend, maxtotal, tmpstart, tmpend, tmptotal;

printf("\nNhap vao 10 phan tu nguyen cua day :");

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

scanf("%d", &a[i]);

printf("Day da cho :\n");

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

printf("%6d", a[i]);

maxstart = maxend = tmpstart = tmpend = 0;

maxtotal = tmptotal = a[0];

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

{

if (a[i] < a[tmpend])

{

if (maxtotal < tmptotal)

{

maxstart = tmpstart;

maxend = tmpend;

maxtotal = tmptotal;

}

tmpstart = tmpend = i;

tmptotal = a[i];

}

else

{

tmptotal += a[i];

tmpend++;

}

}

if (maxtotal < tmptotal)

{

maxstart = tmpstart;

maxend = tmpend;

}

printf("\nDay tang co tong nhieu nhat la : \n");

for (i=maxstart; i<=maxend; i++)

printf("%6d", a[i]);

getch();

}

Câu 30: Tìm tất cả ước của một số N

Spoiler

PHP:

[HTML]#include <stdio.h>

#include <conio.h>

void main()

{

int n, i;

printf("Cho gia tri N = ");

scanf("%d", &n);

printf("Cac uoc so cua %d la :\n", n);

for (i=1; i<n; i++)

if ((n % i) == 0)

printf("%5d", i);

getch();

} [/HTML]

Câu 31: Bội số chung và ước số chung

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

unsigned USCLN (unsigned n, unsigned m)

{

while (n != 0 && m != 0)

if (n>m)

n -= m;

else

m -= n;

if (n == 0)

return m;

else

return n;

}

unsigned BSCNN (unsigned n, unsigned m)

{

return n * m / USCLN(n, m);

}

void main()

{

unsigned n, m;

printf("\nNhap hai vao so nguyen duong : ");

scanf("%u%u", &n, &m);

printf("\nUSCLN cua %u va %u = %u", n, m, USCLN(n,m));

printf("\nBSCNN cua %u va %u = %u", n, m, BSCNN(n,m));

getch();

}

Câu 32: Ma phương

Spoiler

PHP:

#include <stdio.h>

#include <conio.h>

// func declaration

void matrix( int n );

// main()

int main(void)

{

int n;

// input until it's valid.

do

{

printf("\n Plz input size of matrix [ odd size & n < 20 ]: n = ");

scanf("%d",&n);

if ( n % 2 == 0 ) printf("\n Invalid input value .. Plz re-input ... \n");

}

while ( n % 2 == 0 );

if ( n > 20 ) { n = 19 ; // in case of n is greater than 20

printf("\n %d is greater than 20 & set to be default as 19 .",n ); } // end if

// call matrix()

matrix(n);

// stop to watch

getch();

return 0;

}

// function matrix(int n)

void matrix( int n )

{

int a[20][20];

int i, j, row, col, count = 1;

int old_row, old_col, sum = 0;

// set starting value of array

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

for ( j = 0 ; j < n ; j++ )

a[i][j] = 0;

// set the 1st value to start

row = 0; col = (n-1) / 2;

while ( count < n*n + 1 )

{

a[row][col] = count++ ; // set value for elements

old_row = row ; old_col = col; // save the last addresses

// define whether going out of array

row -= 1; if ( row == -1 ) row = n - 1;

col += 1; if ( col == n ) col = 0;

// in case of already having number

if ( a[row][col] != 0 )

{

row = old_row + 1;

col = old_col;

} // end if

} // end while

// print result

printf("\n");

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

{

for ( j = 0 ; j < n ; j++ )

printf("%4d",a[i][j]);

printf("\n");

} // end for

// calculate sum

for ( j = 0 ; j < n ; j++ )

sum += a[0][j];

printf("\n Sum of each row - column - diagonal line is : %d " , sum);

return;

}

Câu 33: Tổng 2 ma trận

Spoiler

PHP:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot);

void nhapmt(float a[][10],int hang,int cot);

void inmt(float a[][10],int hang,int cot);

void main()

{

system("color 3e");

float a[10][10],b[10][10],c[10][10];

int hang1,cot1;

cout<<"Moi ban nhap vao ma tran a: \n";

cout<<"Nhap vao so hang cua ma tran a: ";

cin>>hang1;

cout<<"Nhap vao so cot cua ma tran a: ";

cin>>cot1;

nhapmt(a,hang1,cot1);

inmt(a,hang1,cot1);

int hang2,cot2;

cout<<"Moi ban nhap vao ma tran b: \n";

do

{

cout<<"Nhap vao so hang cua ma tran b: ";

cin>>hang2;

}while(hang2 != hang1);

do

{

cout<<"Nhap vao so cot cua ma tran b: ";

cin>>cot2;

}while(cot2 != cot1);

nhapmt(b,hang2,cot2);

inmt(b,hang2,cot2);

cout<<"\nVay tong cua hai ma tran a,b la: \n";

congmt(a,b,c,hang1,cot1);

inmt(c,hang1,cot1);

getch();

}

void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot)

{

for (int i=0; i<hang; i++)

for (int j=0; j<cot; j++)

c[i][j] = a[i][j] + b[i][j];

}

void nhapmt(float a[][10],int hang,int cot)

{

for(int i = 0;i < hang;i++)

{

for(int j = 0; j < cot; j++)

{

cout<<"Nhap vao phan tu ["<<i<<";"<<j<<"]: ";

cin>>a[i][j];

}

}

}

void inmt(float a[][10],int hang,int cot)

{

for(int i = 0; i < hang; i++)

{

for(int j = 0; j < cot; j++)

{

cout<<a[i][j]<<"\t";

}

cout<<endl;

}

}

Câu 34: Tích 2 ma trận

Spoiler

PHP:

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

void main()

{

int *a,*b,*c;

int m,n;

int i,j;

clrscr();

//Nhap so hang so cot

printf("Nhap vao m:");scanf("%d",&m);

printf("Nhap vao n:");scanf("%d",&n);

//Cap phat bo nho

a=(int*)calloc(m*n,sizeof(int));

b=(int*)calloc(m*n,sizeof(int));

c=(int*)calloc(m*n,sizeof(int));

// Nhap so lieu va tinh toan

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

printf("Nhap vao a[%d,%d]=",i,j);scanf("%d",&a[(i-1+j)+((i-1)*(n-1))]);

}

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

printf("Nhap vao b[%d,%d]=",i,j);scanf("%d",&b[(i-1+j)+((i-1)*(n-1))]);

c[(i-1+j)+((i-1)*(n-1))]=a[(i-1+j)+((i-1)*(n-1))]+b[(i-1+j)+((i-1)*(n-1))];

}

// xuat cac mang a,b,c ra man hinh

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

printf("\t%d",a[(i-1+j)+((i-1)*(n-1))]);

if(j==n)printf("\n");

}

printf("\n===========\n");

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

printf("\t%d",b[(i-1+j)+((i-1)*(n-1))]);

if(j==n)printf("\n");

}

printf("\n===========\n");

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

printf("\t%d",c[(i-1+j)+((i-1)*(n-1))]);

if(j==n)printf("\n");

}

getch();

}

Câu 35: Kiểm tra xem ma trận B có là con của ma trận A không?

Spoiler

PHP:

#include<stdio.h>

#include<conio.h>

#define N 2

#define M 5

void search(int b[N][N],int a[M][M])

{

int i,j,k,l,m,x,y,dem,demx,demy;

int timthay=1;

for(k=0;k<=M-N;++k)

{

for(l=0;l<=M-N;++l)

{

dem=demx=demy=0;

x=l;y=k;

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

{

for(j=0;j<N;++j)

{

if(b[i][j]==a[y][x])dem++;

++x;++demx;

if(demx==N){demx=0;x=l;}

}

++y;++demy;

if(demy==N){demy=0;y=k;}

}

if(dem==N*N)break;

}

if(dem==N*N)break;

}

if(dem!=N*N)timthay=0;

if(timthay==0)printf("\nKo tim thay");

else printf("\nTim thay");

}

void main()

{

clrscr();

int a[M][M]={ 1, 2, 3, 4, 5,

6, 7, 8, 9,10,

11,12,13,14,15,

16,17,18,19,20,

21,22,23,24,25};

int b[N][N]={4, 5,

9,10};

search(b,a);

getch();

}