2014-04-21 2 views
-4

질문 : 최대 6 쌍의 이름과 연령대를 두 개의 개별 배열로 읽고 선형 검색을 사용하여 대상 이름을 찾고 그 사람의 나이를 인쇄하십시오.선형 검색 구현

#include <stdio.h> 
#define ASIZE 20 
#define RECSIZE 6 

struct record { 
    char name[ASIZE]; 
    int age[ASIZE]; 
}; 
struct record na[RECSIZE]; 

int linearSearch(struct record *a, char *find) 
{ 
int x; 
for(x=0; x<RECSIZE; x++) 
{ 
// if(na[x].name==find[x]) 
if(a->name[x]==find[x]) 
    { 
     return x; 
    } 
} 
return -1; 
} 

int main() 
{ 
    int i; 

    for (i=0; i<RECSIZE; i++) 
{ 
printf("Enter name: "); 
scanf("%s",na[i].name); 
printf("Enter age: "); 
scanf("%i",&na[i].age); 
} 

printf("Enter the Search name: "); 
char temp[ASIZE]; 
scanf("%s",temp[ASIZE]); 


int result; 
result=linearSearch(&na, &temp[]); 
printf("%i", result); 
    return 0; 
    } 

도와주세요 .. 내가 함수로 배열을 전달하는 방법에 대한 모르겠습니다 ..

나는 많은 오류를 얻고있다 : 두 배열은 이름과 나이라고합니다. = 결과 linearSearch (& NA, & 온도 []);

오류가입니다

+0

enter image description here이 우리에게 오류를 표시합니다. – Adam

+1

그래서 우리가 뭘하고 싶니? 오류를 표시하지 않습니다. 코드를 보면서 C 책을 얻거나 네트워크에서 자습서를 찾아서 읽는 것이 좋습니다. – OldProgrammer

+0

result = linearSearch (& na []. name, & na []. age, & temp []); 토큰 – user3541302

답변

0
#include <stdio.h> 
#include <string.h> 

#define ASIZE 20 
#define RECSIZE 6 

struct record { 
    char name[ASIZE]; 
    int age; 
}; 

struct record na[RECSIZE]; 

int linearSearch(struct record *a, char *find){ 
    int x; 
    for(x=0; x<RECSIZE; x++){ 
     if(strcmp(a[x].name, find)==0) 
      return x; 
    } 
    return -1; 
} 

int main(){ 
    int i; 

    for (i=0; i<RECSIZE; i++){ 
     printf("Enter name: "); 
     scanf("%s", na[i].name);//No protection when entered past the buffer 
     printf("Enter age: "); 
     scanf("%i", &na[i].age); 
    } 

    printf("Enter the Search name: "); 
    char temp[ASIZE]; 
    scanf("%s", temp); 

    int result; 
    result=linearSearch(na, temp); 
    printf("%i", result); 
    return 0; 
} 
0

내 시스템에서 컴파일러는 정확한 코드에 대해 이러한 오류를 알려주었습니다. 줄 번호 (귀하의 것과 정확히 일치해야 함)를보고 오류 설명을보고 각 오류를 해결하십시오. 당신이이 해결되면, 당신은 당신의 실행 흐름을 이해하는 경로 아래로 더있을 것입니다 :

+0

나는 내 코드를 수정했다. 단지 다시 질문을 확인한다. 나는 지금 오류를 지정했다 ... 고마워! – user3541302

+0

예, 컴파일 메시지의 첫 번째 줄을 보면 알 수 있습니다. 피연산자에 문제가 있음을 알려줍니다. 이 경우'=='이 아닌'strcmp()'를 사용하여 문자열을 비교해야합니다. – ryyker

+0

또한'char temp [ASIZE];'는 문자열이며 문자열 배열이 아닙니다. strcmp 줄은 다음과 같아야합니다 :'if (strcmp (na [x] .name, find [x]))'' – ryyker