フェアリーテイル56~ kadai1
No License C
2022年01月13日
Copy Clone
#include <stdio.h>
#define NUMBER 5
#define FAILED -1
int search(int vc[], int key, int no){
    int i = 0;
    vc[no] = key;
    while(1){
        if(vc[i]==key){
            break;
        }
        i++;
    }
    return(i==no ? FAILED : i);
}

int main(void){
    int i,key,index;
    int vx[NUMBER+1];
    for (int i=0; i<NUMBER; i++) {
        printf("vx[%d]: ", i);
        scanf("%d", &vx[i]);
    }
    printf("探す値:");
    scanf("%d", &key);

    index = search(vx, key, NUMBER);
    
    if(index==FAILED){
        printf("探索失敗");
    }
    else {
        printf("%d is in %d\n", key, index);
    }
    return 0;
}
#include <stdio.h>
#define NUMBER 5
#define FAILED -1
int search(int vc[], int key, int no){
    int i = 0;
    vc[no] = key;
    while(1){
        if(vc[i]==key){
            break;
        }
        i++;
    }
    return(i==no ? FAILED : i);
}

int main(void){
    int i,key,index;
    int vx[NUMBER+1];
    for (int i=0; i<NUMBER; i++) {
        printf("vx[%d]: ", i);
        scanf("%d", &vx[i]);
    }
    printf("探す値:");
    scanf("%d", &key);

    index = search(vx, key, NUMBER);
    
    if(index==FAILED){
        printf("探索失敗");
    }
    else {
        printf("%d is in %d\n", key, index);
    }
    return 0;
}
anonymous
Anonymous
3 日前
test