<< Chapter < Page Chapter >> Page >

printf("Ma tran tong C:\n");

InMaTran(c,M,N);

getch();

return 0;

}

Ví dụ 2: Nhập vào một ma trận 2 chiều gồm các số thực, in ra tổng của các phần tử trên đường chéo chính của ma trận này.

Ta nhận thấy rằng giả sử ma trận a có M dòng, N cột thì các phần tử của đường chéo chính là các phần tử có dạng: a[i][i]với i size 12{ in } {} [0…min(M,N)-1].

#include<conio.h>

#include<stdio.h>

int main()

{

float a[10][10], T=0;

int M, N, i,j, Min;

clrscr();

printf("Ma tran co bao nhieu dong? ");scanf("%d",&M);

printf("Ma tran co bao nhieu cot? ");scanf("%d",&N);

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

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

{

printf("Phan tu o dong %d cot %d: ",i,j);

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

}

printf("Ma tran vua nhap: \n");

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

{

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

printf("%.2f",a[i][j]);

printf("\n");

}

Min=(M>N) ? N: M; /* Tìm giá trị nhỏ nhất của M&N*/

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

T=T+a[i][i];

printf("Tong cac phan tu o duong cheo chinh la: %f",T);

getch();

return 0;

}

Bài tập

Mục đích yêu cầu

Làm quen với kiểu dữ liệu có cấu trúc trong C, kiểu mảng. Thực hiện các bài tập trong phần nội dung bằng cách kết hợp kiểu dữ liệu mảng, các kiểu dữ liệu đã học và các phần đã học trong các bài tập trước.

Nội dung

1. Viết chương trình nhập vào một dãy n số thực a[0], a[1],..., a[n-1], sắp xếp dãy số theo thứ tự từ lớn đến nhỏ. In dãy số sau khi sắp xếp.

2. Viết chương trình sắp xếp một mảng theo thứ tự tăng dần sau khi đã loại bỏ các phần tử trùng nhau.

3. Viết chương trình nhập vào một mảng, hãy xuất ra màn hình:

- Phần tử lớn nhất của mảng.

- Phần tử nhỏ nhất của mảng.

- Tính tổng của các phần tử trong mảng .

4. Viết chương trình nhập vào một dãy các số theo thứ tự tăng, nếu nhập sai quy cách thì yêu cầu nhập lại. In dãy số sau khi đã nhập xong. Nhập thêm một số mới và chèn số đó vào dãy đã có sao cho dãy vẫn đảm bảo thứ tự tăng. In lại dãy số để kiểm tra.

5. Viết chương trình nhập vào một ma trận (mảng hai chiều) các số nguyên, gồm m hàng, n cột. In ma trận đó lên màn hình. Nhập một số nguyên khác vào 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ử ?

6. Viết chương trình để chuyển đổi vị trí từ dòng thành cột của một ma trận (ma trận chuyển vị) vuông 4 hàng 4 cột. Sau đó viết cho ma trận tổng quát cấp m*n.

Ví dụ:

1 2 3 4 1 2 9 1

2 5 5 8 2 5 4 5

9 4 2 0 3 5 2 8

1 5 8 6 4 8 0 6

7. Viết chương trình nhập vào một mảng số tự nhiên. Hãy xuất ra màn hình:

- Dòng 1 : gồm các số lẻ, tổng cộng có bao nhiêu số lẻ.

- Dòng 2 : gồm các số chẵn, tổng cộng có bao nhiêu số chẵn.

- Dòng 3 : gồm các số nguyên tố.

- Dòng 4 : gồm các số không phải là số nguyên tố.

8. Viết chương trình tính tổng bình phương của các số âm trong một mảng các số nguyên.

9. Viết chương trình thực hiện việc đảo một mảng một chiều.

Ví dụ : 1 2 3 4 5 7 9 10 đảo thành 10 9 7 5 4 3 2 1 .

10. Viết chương trình nhập vào hai ma trận A và B có cấp m, n. In hai ma trận lên màn hình. Tổng hai ma trận A và B là ma trận C được tính bởi công thức:

cij= aij +bij ( i=0,1,2,...m-1; j=0,1,2...n-1)

Tính ma trận tổng C và in kết quả lên màn hình.

11. Viết chương trình nhập vào hai ma trận A có cấp m, k và B có cấp k, n. In hai ma trận lên màn hình. Tích hai ma trận A và B là ma trận C được tính bởi công thức:

cij= ai1*b1j + ai2 *b2j + ai3 *b3j + ... + aik *bkj (i=0,1,2,...m-1;j=0,1,2...n-1)

Tính ma trận tích C và in kết quả lên màn hình.

12. Xét ma trận A vuông cấp n, các phần tử a[i, i] ( i= 1 ... n ) được gọi là đường chéo chính của ma trận vuông A. Ma trận vuông A được gọi là ma trận tam giác nếu tất cả các phần tử dưới đường chéo chính đều bằng 0. Định thức của ma trận tam giác bằng tích các phần tử trên đường chéo chính.

Ta có thể chuyển một ma trận vuông bất kỳ về ma trận tam giác bằng thuật toán:

- Xét cột i (i =0,1...n-2)

- Trong cột i xét các phần tử a[k,i] ( k=i+1...n-1)

+ Nếu a[k,i]=0 thì tăng k lên xét phần tử khác

+ Nếu a[k,i]<>0 thì làm như sau:

Nhân toàn bộ hàng k với - a[i,i]/a[k,i]

Lấy hàng i cộng vào hàng k sau khi thực hiện phép nhân trên.

Đổi chỗ hai hàng i và k cho nhau

Nhân toàn bộ hàng k với -1 sau khi đã đổi chỗ với hàng i

Tăng k lên xét phần tử khác.

Viết chương trình tính định thức cấp n thông qua các bước nhập ma trận, in ma trận, đưa ma trận về dạng tam giác, in ma trận tam giác, in kết quả tính định thức.

13. Viết chương trình thực hiện việc trộn hai dãy có thứ tự thành một dãy có thứ tự. Yêu cầu không được trộn chung rồi mới sắp thứ tự. Khi trộn phải tận dụng được tính chất đã sắp của hai dãy con.

Questions & Answers

differentiate between demand and supply giving examples
Lambiv Reply
differentiated between demand and supply using examples
Lambiv
what is labour ?
Lambiv
how will I do?
Venny Reply
how is the graph works?I don't fully understand
Rezat Reply
information
Eliyee
devaluation
Eliyee
t
WARKISA
hi guys good evening to all
Lambiv
multiple choice question
Aster Reply
appreciation
Eliyee
explain perfect market
Lindiwe Reply
In economics, a perfect market refers to a theoretical construct where all participants have perfect information, goods are homogenous, there are no barriers to entry or exit, and prices are determined solely by supply and demand. It's an idealized model used for analysis,
Ezea
What is ceteris paribus?
Shukri Reply
other things being equal
AI-Robot
When MP₁ becomes negative, TP start to decline. Extuples Suppose that the short-run production function of certain cut-flower firm is given by: Q=4KL-0.6K2 - 0.112 • Where is quantity of cut flower produced, I is labour input and K is fixed capital input (K-5). Determine the average product of lab
Kelo
Extuples Suppose that the short-run production function of certain cut-flower firm is given by: Q=4KL-0.6K2 - 0.112 • Where is quantity of cut flower produced, I is labour input and K is fixed capital input (K-5). Determine the average product of labour (APL) and marginal product of labour (MPL)
Kelo
yes,thank you
Shukri
Can I ask you other question?
Shukri
what is monopoly mean?
Habtamu Reply
What is different between quantity demand and demand?
Shukri Reply
Quantity demanded refers to the specific amount of a good or service that consumers are willing and able to purchase at a give price and within a specific time period. Demand, on the other hand, is a broader concept that encompasses the entire relationship between price and quantity demanded
Ezea
ok
Shukri
how do you save a country economic situation when it's falling apart
Lilia Reply
what is the difference between economic growth and development
Fiker Reply
Economic growth as an increase in the production and consumption of goods and services within an economy.but Economic development as a broader concept that encompasses not only economic growth but also social & human well being.
Shukri
production function means
Jabir
What do you think is more important to focus on when considering inequality ?
Abdisa Reply
any question about economics?
Awais Reply
sir...I just want to ask one question... Define the term contract curve? if you are free please help me to find this answer 🙏
Asui
it is a curve that we get after connecting the pareto optimal combinations of two consumers after their mutually beneficial trade offs
Awais
thank you so much 👍 sir
Asui
In economics, the contract curve refers to the set of points in an Edgeworth box diagram where both parties involved in a trade cannot be made better off without making one of them worse off. It represents the Pareto efficient allocations of goods between two individuals or entities, where neither p
Cornelius
In economics, the contract curve refers to the set of points in an Edgeworth box diagram where both parties involved in a trade cannot be made better off without making one of them worse off. It represents the Pareto efficient allocations of goods between two individuals or entities,
Cornelius
Suppose a consumer consuming two commodities X and Y has The following utility function u=X0.4 Y0.6. If the price of the X and Y are 2 and 3 respectively and income Constraint is birr 50. A,Calculate quantities of x and y which maximize utility. B,Calculate value of Lagrange multiplier. C,Calculate quantities of X and Y consumed with a given price. D,alculate optimum level of output .
Feyisa Reply
Answer
Feyisa
c
Jabir
the market for lemon has 10 potential consumers, each having an individual demand curve p=101-10Qi, where p is price in dollar's per cup and Qi is the number of cups demanded per week by the i th consumer.Find the market demand curve using algebra. Draw an individual demand curve and the market dema
Gsbwnw Reply
suppose the production function is given by ( L, K)=L¼K¾.assuming capital is fixed find APL and MPL. consider the following short run production function:Q=6L²-0.4L³ a) find the value of L that maximizes output b)find the value of L that maximizes marginal product
Abdureman
types of unemployment
Yomi Reply
What is the difference between perfect competition and monopolistic competition?
Mohammed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Am nhac. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10735/1.3
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Am nhac' conversation and receive update notifications?

Ask