一個c program的問題

2009-04-27 5:29 am
以下這個program有一些不明白的地方,想請教各位
這個program是用來check user input 的數字是否在array中的
總共有4個問題想問一問大家的~~

#include <stdio.h>
#include <stdlib.h>
#define max 3 <-----請問這句的作用是???
int main()
{
int no[max], i, m, j, match=0;
for(i=0; i<max; i++){ <---這個for loop的作用是???
no=i+1;
}
for(i=0; i<max; i++){
printf("Input a no. to check this no. is included or not:\n");
scanf("%d", &m);
for(j=0; j<max; j++){
if(no[j]==m) match=1;
else continue;
}
if(match==1) printf("this no. is included in the array.\n");
else printf("this no. is NOT included in the array.\n");
match=0; <--為何要再set match=0????
}
system("PAUSE"); <--這句則是要電腦做什麼?刪去會如何??

}

回答 (3)

2009-04-27 7:31 am
✔ 最佳答案
#include <stdio.h>
#include <stdlib.h>
#define max 3 <-----請問這句的作用是???
define constant max = 3
int main()
{
int no[max], i, m, j, match=0;
for(i=0; i<max; i++){ <---這個for loop的作用是???
no=i+1;
}
我都唔明,no係一個三位array,no=i+1係錯誤
for(i=0; i<max; i++){
printf("Input a no. to check this no. is included or not:\n");
scanf("%d", &m);
for(j=0; j<max; j++){
if(no[j]==m) match=1;
else continue;
}
if(match==1) printf("this no. is included in the array.\n");
else printf("this no. is NOT included in the array.\n");
match=0; <--為何要再set match=0????
因為你match到已經set咗1,所以要reset先可以做第二次
}
system("PAUSE"); <--這句則是要電腦做什麼?刪去會如何??
唔清楚,冇都可以
}
2009-04-27 1:53 pm
Sorry upstairs, I wanted to publish this as a comment, but cannot because of length limitations.

以下這個program有一些不明白的地方,想請教各位
這個program是用來check user input 的數字是否在array中的
總共有4個問題想問一問大家的~~
#include <stdio.h>
#include <stdlib.h>
#define max 3 <-----請問這句的作用是???
/*
there are no constants in the c-language, this is the way to
define a constant having a value of 3 using the cpp (c-preprocessor).
The c-compiler will see the number three and not a variable max
This allows the programmer to change the maximum number of
values to compare without changing the code
*/
int main()
{
int no[max], i, m, j, match=0;
for(i=0; i<max; i++){ <---這個for loop的作用是???
no=i+1;
}
/*
This is meant to initialize the 3 values of no[] to 1, 2 and 3 respectively. There is a mistake in the coding, it should have read:
no[i]=i+1;
*/
for(i=0; i<max; i++){
printf("Input a no. to check this no. is included or not:\n");
scanf("%d", &m);
// m=0; // best place to set m=0 is here
for(j=0; j<max; j++){
if(no[j]==m) match=1;
else continue;
}
if(match==1) printf("this no. is included in the array.\n");
else printf("this no. is NOT included in the array.\n");
match=0; <--為何要再set match=0????
/*
if is a good idea to reset variables in case there is a second run, although not in this particular case.
It would be even better to reset it just after entering the value to match, this way, no matter how many times the program will be run, the reset will be guaranteed.
*/
}
system("PAUSE"); <--這句則是要電腦做什麼?刪去會如何??
/* many windows based programs will close the window after execution.
This command will keep the window displayed until the user types in a character. Otherwise, the user will not have time to understand the resulting message before the execution window closes.
*/
2009-04-27 7:37 am
你那句在 for loop 中的
no=i+1;
是否有問題
compile 中有 error


收錄日期: 2021-04-13 16:35:26
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090426000051KK02068

檢視 Wayback Machine 備份