You are on page 1of 5

1.

TÌM KẾT QUẢ THỰC HIỆN CHƯƠNG TRÌNH


2. CHUYỂN ĐỔI CHƯƠNG TRÌNH

1. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int A[] = {19, 12, 18, 13, 10, 14, 16, 11, 15, 17}, n=10, count=0;
void Result(int *A, int n){
count++; printf("\n Buoc %d:",count);
for(int i=0; i<n; i++) printf("%4d",A[i]);
}
void main(void){
int i, j;
for(i=0; i<n-1; i++){
for(j=i+1; j<n; j++){
if(A[i]>A[j]){
A[i]=A[i] + A[j];
A[j]=A[i]-A[j];
A[i]=A[i] – A[j];
}
}
Result(A,n);
}
}

2. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int A[] = {19, 12, 18, 13, 10, 14, 16, 11, 15, 17}, n=10, count=0;
void Result(int *A, int n){
count++; printf("\n Buoc %d:",count);
for(int i=0; i<n; i++) printf("%4d",A[i]);
}
void main(void){
int i, j, temp;
for(i=1; i<n; i++){ temp = A[i];
for(j=i-1; j>=0 && temp < A[j]; j--){
A[j+1]=A[j];
}
A[j+1] = temp;Result(A,n);
} getch();
}
3. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int A[] = {19, 12, 18, 13, 10, 14, 16, 11, 15, 17}, n=10, count=0;
void Result(int *A, int n){
count++; printf("\n Ket qua buoc %d:",count);
for(int i=0; i<n; i++) printf("%4d",A[i]);
}
void main(void){
int i, j, temp;
for(i=1; i<n; i++){
for(j=n-1; j>=i; j--){
if (A[j-1] > A[j]){
temp = A[j-1]; A[j-1] = A[j]; A[j] = temp;
}
}
Result(A,n);
} getch();
}

4. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây:

#include <stdio.h>
#include <conio.h>
int B[]={0,0,0,0,0}, n=5, OK=1, count=0;
void Result(void){
printf("\n Ket qua buoc %3d:",++count);
for (int i=0; i<n; i++) printf("%3d",B[i]);
}
void Function1(void){
for(int i=n-1; i>=0 && B[i]; i--)
B[i] =0;
if(i>=0) B[i]=1;
else OK=0;
}
void main(void){
do {
Result();
Function1();
} while(OK);
}
5. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int B[]={0,0,0,0,0}, n=5,k=3, OK=1, count=0;
void Inketqua(void){
printf("\n Ket qua %3d:",++count);
for(int i=0; i<n; i++) printf("%3d",B[i]);
}
void Result(void){
int i, s=0;
for (i=0; i<n; i++) if(B[i]) s++;
if(s==k) Inketqua();
}
void Function1(void){
for(int i=n-1; i>=0 && B[i]; i--)
B[i] =0;
if(i>=0) B[i]=1;
else OK=0;
}
void main(void){
do {
Result();
Function1 ();
} while(OK);
}

6. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int B[]={0,0,0,0,0,0,0}, n=7,k=80, OK=1, count=0;
int A[]={5,10,15,20,25,30,35};
void Inketqua(void){
printf("\n Ket qua %3d:",++count);
for(int i=0; i<n; i++)
if(B[i]) printf("%3d",A[i]);
}
void Result(void){
int i, s=0;
for (i=0; i<n; i++) s = s+A[i]*B[i];
if(s==k) Inketqua();
}
void Function1(void){
for(int i=n-1; i>=0 && B[i]; i--)
B[i] =0;
if(i>=0) B[i]=1;
else OK=0;
}
void main(void){
do {
Result();
Function1();
} while(OK);
}
7. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int B[]={0,0,0,0,0,0,0}, n=7,k=80, OK=1, count=0;
int A[]={5,10,15,20,25,30,35};
void Inketqua(void){
printf("\n Ket qua %3d:",++count);
for(int i=0; i<n; i++) printf("%3d",B[i]);
}
void Result(void){
int i, s=0;
for (i=0; i<n; i++) s = s+A[i]*B[i];
if(s==k) Inketqua();
}
void Function1(void){
for(int i=n-1; i>=0 && B[i]; i--)
B[i] =0;
if(i>=0) B[i]=1;
else OK=0;
}
void main(void){
do {
Result();
Function1();
} while(OK);
}

8. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
int B[]={0,0,0,0,0,0,0}, n=7,k=40, OK=1, count=0;
int A[]={5,10,15,20,25,30,35};
void Inketqua(void){
printf("\n Ket qua %3d:",++count);
for(int i=0; i<n; i++) printf("%3d",B[i]);
}
void Result(void){
int i, s=0;
for (i=0; i<n; i++) s = s+A[i]*B[i];
if(s<=k) Inketqua();
}
void Function1(void){
for(int i=n-1; i>=0 && B[i]; i--)
B[i] =0;
if(i>=0) B[i]=1;
else OK=0;
}
void main(void){
do {
Result();
Function1();
} while(OK);
}
9. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
#include <string.h>
char str[255]="000 001 010 011 100 101 110 111 101 110";
void main(void){ clrscr();
int n, m=0, k=0;n = strlen(str);char s[32];
for(int i=0; i<=n; i++){
switch(str[i]){
case ' ' :
case '\t':
case '\n':
case '\r':
case '\0':
if (k>0) {
s[k]='\0';m++;k=0;
printf("\n Kết quả bước %d:
%s",m,s);
}
break;
default:
s[k++]=str[i]; break;
}
}
}

10. Hãy cho biết kết quả thực hiện đoạn chương trình dưới đây.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int nguyen_to(unsigned long int n){
unisened long int i, k=sqrt(n);
for(i=2; i<=k; i++) if(n%i==0) return(0);
return(1);
}
void main(void){
unsigned long int n =2868336900; int k=0,i=2, OK=1;
do {
if(nguyen_to(n)) { printf("\n Kết quả bước:%d:%ld",++k,n);
OK=0;}
else if (n%i==0){printf("\n Kết quả bước:%d: %d",++k,i); n =
n/i;}
else i++;
} while(OK);
11. }

a) Chứng minh rằng một chương trình được thể hiện bằng tất cả các cấu trúc lệnh (while,
do..while, for, if..else, switch) đều có thể chuyển đổi thành một chương trình mới cho lại
kết quả giống như chương trình cũ nhưng chỉ cần dùng tối thiểu cấu trúc lệnh lặp while
và một số biến phụ.
b) Áp dụng kết quả trên chuyển đổi các chương trình từ bài 1 đến bài 10 sử dụng tối thiểu
cấu trúc lệnh lặp while và một số biến phụ.

You might also like