<< Chapter < Page Chapter >> Page >

Với cách truy xuất theo kiểu này, Tên biến mảng[Chỉ số] có thể coi như là một biến có kiểu dữ liệu là kiểu được chỉ ra trong khai báo biến mảng.

Ví dụ 1:

int a[10];

Trong khai báo này, việc truy xuất các phần tử được chỉ ra trong hình 1. Chẳng hạn phần tử thứ 2 (có vị trí 1) là a[1]…

Ví dụ 2: Vừa khai báo vừa gán trị cho 1 mảng 1 chiều các số nguyên. In mảng số nguyên này lên màn hình.

Giả sử ta đã biết số phần tử của mảng là n; việc hiển thị 1 giá trị số nguyên lên màn hình ta cần sử dụng hàm printf() với định dạng %d, tổng quát hóa lên nếu muốn hiển thị lên màn hình giá trị của n số nguyên, ta cần gọi hàm printf() đúng n lần. Như vậy trong trường hợp này ta sử dụng 1 vòng lặp để in ra giá trị các phần tử.

Ta có đoạn chương trình sau:

#include<stdio.h>

#include<conio.h>

int main()

{

int n,i,j,tam;

int dayso[]={66,65,69,68,67,70};

clrscr();

n=sizeof(dayso)/sizeof(int); /*Lấy số phần tử*/

printf("\n Noi dung cua mang ");

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

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

return 0;

}

Ví dụ 3: Đổi một số nguyên dương thập phân thành số nhị phân. Việc chuyển đổi này được thực hiện bằng cách lấy số đó chia liên tiếp cho 2 cho tới khi bằng 0 và lấy các số dư theo chiều ngược lại để tạo thành số nhị phân. Ta sẽ dùng mảng một chiều để lưu lại các số dư đó. Chương trình cụ thể như sau:

#include<conio.h>

#include<stdio.h>

int main()

{

unsigned int N;

unsigned int Du;

unsigned int NhiPhan[20],K=0,i;

printf("Nhap vao so nguyen N= ");scanf("%d",&N);

do

{

Du=N % 2;

NhiPhan[K]=Du; /* Lưu số dư vào mảng ở vị trí K*/

K++;/* Tăng K lên để lần kế lưu vào vị trí kế*/

N = N/2;

} while(N>0);

printf("Dang nhi phan la: ");

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

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

getch();

return 0;

}

Ví dụ 4: Nhập vào một dãy n số và sắp xếp các số theo thứ tự tăng. Đây là một bài toán có ứng dụng rộng rãi trong nhiều lĩnh vực. Có rất nhiều giải thuật sắp xếp. Một trong số đó được mô tả như sau:

Đầu tiên đưa phần tử thứ nhất so sánh với các phần tử còn lại, nếu nó lớn hơn một phần tử đang so sánh thì đổi chỗ hai phần tử cho nhau. Sau đó tiếp tục so sánh phần tử thứ hai với các phần tử từ thứ ba trở đi ... cứ tiếp tục như vậy cho đến phần tử thứ n-1.

Chương trình sẽ được chia thành các hàm Nhap (Nhập các số), SapXep (Sắp xếp) và InMang (In các số); các tham số hình thức của các hàm này là 1 mảng không chỉ định rõ số phần tử tối đa, nhưng ta cần có thêm số phần tử thực tế được sử dụng của mảng là bao nhiêu, đây là một giá trị nguyên.

#include<conio.h>

#include<stdio.h>

void Nhap(int a[],int N)

{

int i;

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

{

printf("Phan tu thu %d: ",i);scanf("%d",&a[i]);

}

}

void InMang(int a[], int N)

{

int i;

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

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

printf("\n");

}

void SapXep(int a[], int N)

{

int t,i;

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

for(int j=i+1;j<N;j++)

if (a[i]>a[j])

{

t=a[i];

a[i]=a[j];

a[j]=t;

}

}

int main()

{

int b[20], N;

printf("So phan tu thuc te cua mang N= ");

scanf("%d",&N);

Nhap(b,N);

printf("Mang vua nhap: ");

InMang(b,N);

SapXep(b,N); /* Gọi hàm sắp xếp*/

printf("Mang sau khi sap xep: ");

InMang(b,N);

getch();

return 0;

}

Kết quả chạy chương trình có thể là:

Mảng nhiều chiều

Mảng nhiều chiều là mảng có từ 2 chiều trở lên. Điều đó có nghĩa là mỗi phần tử của mảng là một mảng khác.

Questions & Answers

the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
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