Cấu trúc :
*Cú pháp: <Kiểu> <Tên mảng ><[số phần tử]>
- Tên mảng: đây là một cái tên đặt đúng theo quy tắc đặt tên của danh biểu. Tên này
cũng mang ý nghĩa là tên biến mảng.
- Số phần tử: là một hằng số nguyên, cho biết số lượng phần tử tối đa trong mảng là
bao nhiêu (hay nói khác đi kích thước của mảng là gì).
- Kiểu: mỗi phần tử của mảng có dữ liệu thuộc kiểu gì.
*Mảng :Mảng là một dãy các phần tử có cùng kiểu được đặt liên tiếp trong bộ nhớ và có thể truy xuất đến từng phần tử bằng cách thêm một chỉ số vào sau tên của mảng.
II.Bài Tập
Bài 1 : Write a program that asks the user to type 10 integers of an array. The program must compute and write how many integers are greater than or equal to 10.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int n;
int C=0;
for(n=0;n<10;n++){
printf("Nhap A[%d]=",n+1);
scanf("%d",&A[n]);
}
printf("\n Cac so trong lon hon = 10 trong day :");
for(n=0;n<10;n++){
if(A[n]>=10){
printf("%d ",A[n]);
C=C+1;
}
}
return 0;
}
Bài 2 :Write a program that asks the user to type 10 integers of an array. The program must output the largest element in the array, and the index at which that element was found.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int n;
for(n=0;n<10;n++){
printf("Nhap A[%d]=",n+1);
scanf("%d",&A[n]);
}
int max=A[0];
for(n=0;n<10;n++){
if(A[n]>max){
max=A[n];
}
}
printf("Max=%d",max);
for(n=0;n<10;n++){
if(A[n]==max)
printf("\nVi tri=%d",n+1);
}
return 0;
}
Bài 3:Input values are accepted from the user into the array. Displays highest of the entered values. Prints average of values entered.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[5];
int i;
for(i=0;i<5;i++){
printf("Nhap A[%d]=",i+1);
scanf("%d",&A[i]);
}
int max=A[0];
for(i=0;i<5;i++){
if(A[i]>max){
max=A[i];
}
}
printf("Max=%d",max);
int t;
for(i=0;i<5;i++){
t=t+A[i];
}
printf("\nTrung Binh Cong =%d",t/i);
return 0;
}
Bài 4 Write a program that accepts the following numbers in an array and reverses the array
#include <stdio.h>
#include <stdlib.h>
/* Write a program that accepts the following numbers in an array and reverses the array */
int main(int argc, char *argv[]) {
int t;
printf("So Luong Gia Tri Trong Mang= ");
scanf("%d",&t);
int A[t],B[t],i;
for (i=0;i<t;i++){
printf("A[%d]= ", i+1);
scanf("%d",&A[i]);
B[t-i-1] = A[i];
}
for (i=0;i<t;i++) printf("\n\tA[%d]= %d\t",i+1,A[i]);
for (i=0;i<t;i++) printf("\nB[%d]= %d\t",i+1,B[i]);
return 0;
}
. Write a program to count the number of vowels in a line of text
Bài 6 :
Write a program that asks the user to type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers. The program writes "V is in the array" or "V is not in the array".
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int i;
for(i=0;i<10;i++){
printf("A[%d]=",i+1);
scanf("%d",&A[i]);
}
int V;
printf("Nhap V =");
scanf("%d",&V);
int d;
d=0;
for(i=0;i<10;i++)
if(V==A[i])
d=1;
if(d==1)
printf("V co trong mang");
else printf("V khong co trong mang");
return 0;
}
Bài 7 :
Write a program that asks the user to type 10 integers of an array and an integer value V. The program must search if the value V exists in the array and must remove the first occurrence of V, shifting each following element left and adding a zero at the end of the array. The program must then write the final array
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int i;
for(i=0;i<10;i++){
printf("A[%d]=",i+1);
scanf("%d",&A[i]);
}
int V;
printf("Nhap V =");
scanf("%d",&V);
for(i=0;i<10;i++) if(V==A[i]) break;
for (i;i<10;i++) A[i]=A[i+1];
A[9]=0; printf("\n\n\n");
for (i=0;i<10;i++) printf("%d ",A[i]);
return 0;
}
Bài 8 :
Write
a program that asks the user to type 10 integers of an array and an
integer value V and an index value i between 0 and 9. The program must
put the value V at the place i in the array, shifting each element right
and dropping off the last element. The program must then write the
final array
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int j;
for(j=0;j<10;j++){
printf("A[%d]=",j+1);
scanf("%d",&A[j]);
}
int V,i;
printf("Nhap V =");
scanf("%d",&V);
printf("Nhap i =");
scanf("%d",&i);
for(j=9;j>i;j--)
A[i]=V;
printf("final array :");
for(j=0;j<10;j++)
printf("\t%d",A[j]);
return 0;
}
Bài 9 :
Write
a program that asks the user to type 10 integers of an array. The
program will then display either "the array is growing", "the array is
decreasing", "the array is constant", or "the array is growing and
decreasing."
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10];
int j,i,m,n;
for(j=0;j<10;j++){
printf("A[%d]=",j+1);
scanf("%d",&A[j]);
}
i=m=n=0;
for(j=0;j<9;j++){
if(A[j]==A[j+1])
i=i+1;
if(A[j]>A[j+1])
m=m+1;
if(A[j]<A[j+1])
n=n+1;
}
if(i==9){
printf("Mang lien tuc");}
else if(m==9){
printf("Mang dang giam");
}
else if(n==9){
printf("Mang dang tang");
}
else printf("Mang vua tang vua giam");
return 0;
}
Bài 10 :
10. Write a program that asks the user to type 10 integers of an array. The program will then sort the array in descending order and display it.
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A[10],i,a,b;
for(i=0;i<10;i++) {
printf("A[%d]:",i+1);
scanf("%d",&A[i]);
}
for(i=0;i<10;i++) {
for(a=0;a<10;a++) {
if(A[a]>A[a-1]) {
b=A[a-1];
A[a-1]=A[a];
A[a]=b;
}
}
}
for(i=0;i<10;i++) {
printf("%d ",A[i]);
}
return 0;
}
Bài 11 :
11. Write a program which takes 2 arrays of 10 integers each, a and b. c is an array with 20 integers. The program should put into c the appending of b to a, the first 10 integers of c from array a, the latter 10 from b.
include <stdio.h>
#include <stdlib.h>
/* Write a program which takes 2 arrays of 10 integers each, a and b.
c is an array with 20 integers. The program should put into c the appending of b to a,
the first 10 integers of c from array a, the latter 10 from b. Then the program should display c */
int main(int argc, char *argv[]) {
int A[20],i;
for(i=0;i<10;i++){
printf("A[%d]=",i+1);
scanf("%d",&A[i]);
}
for(i=10;i<20;i++){
printf("B[%d]=",i-9);
scanf("%d",&a[i]);
}
for(i=0;i<20;i++) printf("C[%d]=%d\t",i+1,a[i]);
printf("\n\n\n");
system("pause");
return 0;
}








Không có nhận xét nào:
Đăng nhận xét