By GokiSoft.com|
19:48 15/09/2023|
Lập Trình C
[Share Code] Tìm hiểu for, mảng 1 chiều trong C - C2307L
#main.c
#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 cnt = 0;
// while(cnt < 1000) {
// printf("\nScooby");
// ++cnt;
// }
// for(cnt = 0; cnt < 10; cnt++) {
// printf("\n%d. Scooby", cnt + 1);
// }
for(cnt = 0; cnt < 10; cnt+=2) {
printf("\n%d. Scooby", cnt + 1);
}
// for(; cnt < 10; cnt+=2) {
// printf("\n%d. Scooby", cnt + 1);
// }
// for(;cnt < 10;) {
// printf("\n%d. Scooby", cnt + 1);
// cnt+=8;
// }
// for(;;) {
// printf("\n%d. Scooby", cnt + 1);
// cnt+=8;
// break;
// }
// for(cnt=0;cnt<100;cnt++) {
// if(cnt % 3 == 2) {
// break;
// }
// }
// printf("\ncnt = %d", cnt);
// for(cnt=0;cnt<10;cnt++) {
// if(cnt % 3 == 0) {
// continue;
// }
// printf("\n%d", cnt);
// }
// printf("\n%d", cnt);
*/
//Vi du: khai bao 3 bien so nguyen t1, t2, t3
int t1, t2, t3;
//Vi du: khai bao 5 bien so nguyen x1, x2, x3, x4, x5
int x1, x2, x3, x4, x5;
//Vi du: khai bao 100 bien so nguyen y1, y2, ..., y100 -> Bo tay.
//Vi du: khai bao 1000 bien so nguyen
int y[100]; //khai bao 100 so nguyen -> y[0], y[1], ..., y[99]
int t[1000]; //khai bao 1000 so nguyen -> t[0], t[1], ..., t[999]
//goi mang (array) -> mang 1 chieu
//2 thuat ngu di kem
// do dai mang: length = 100 (y), 1000 (t)
// giai index: 0 -> length - 1 (99, 999)
//Nhap du lieu cho mang so nguyen
//Voi bien so nguyen t1
// printf("\nNhap t1 = ");
// scanf("%d", &t1);
// printf("\nNhap y[0] = ");
// scanf("%d",&y[0]);
// printf("\nNhap y[5] = ");
// scanf("%d",&y[5]);
// printf("\nGia tri cua y[0] = %d,y[5] = %d", y[0], y[5]);
//Nhap ca mang y
int i;
for(i=0;i<10;i++) {
printf("\nNhap y[%d] = ", i);
scanf("%d", &y[i]);
}
//Yeu cau: tinh tong cac phan tu trong mang y
int sum = 0;
for (i = 0; i < 10; i++) {
sum += y[i];
}
printf("\nTong cac phan tu mang y: %d", sum);
//Yeu cau: tinh tong cac so chia het cho 3
sum = 0;
for (i = 0; i < 10; i++){
if(y[i] % 3 == 0) {
sum += y[i];
}
}
printf("\nTong cac phan tu chia het cho 3 mang y: %d", sum);
return 0;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)