a program that uses an ID number or name as a search key to search for results in a binary search

Asked 1 years ago, Updated 1 years ago, 234 views

I would like to create a program that reads the number of data, inputs the ID and name for the data, inputs the ID in ascending order or alphabetical order by name, and outputs the result, but the result is strange depending on how I type it.

This is because the results of the execution are shown first, but for example,

$./a.out
Number of Data: 3
a[0]
  ID: 1
  Name: suzuki
a[1]
  ID: 2
  Name: kato
a[2]
  ID: 3
  Name: sano
1) ID, 2) Name: 2

Element ID Name
a[0]—2 kato           
a[1]—3sano           
a[2]—1 suzuki         
Search key (name): sano
Search key (name) sano, ID 3 found in a[1]

This is how the name is alphabetically named, and the ID that it is present in is also displayed, but

when:
$./a.out
Number of Data: 3
a[0]
  ID: 2
  Name: kato
a[1]
  ID: 1
  Name: sano
a[2]
  ID: 4
  Name: suzuki
1) ID, 2) Name: 2

Element ID Name
a[0]—2 kato           
a[1]—1 sano           
a[2]—4 suzuki         
Search key (name): suzuki
US>Search key (name) UU not found

The results will be strange.

I created the program as follows, but I didn't know what was wrong, so
Thank you for your cooperation.

#include<stdio.h>
# include <string.h>
#define MAX_SIZE100

US>structure student {
    intid;
    char name [20];
};
typeedef structure student student;

/* Replace Student type values indicated by two pointer arguments */
void swap_student(structure student*a, structure student*b)
{
    structure student t=*a;
    *a=*b;
    *b=t;
}

/* Each element of the Student array a[ ] of length size is:
   Sort by id (ascending) */
void ssort_id (Student a[], intn)
{
    inti,j;
    for(i=n-1;i>=1;i--){
     for(j=0;j<i;j++){
        if(a[j].id>a[j+1].id){
             swap_student(&a[j], &a[j+1]);
        }
      }
    }
}

/* Each element of the Student array a[ ] of length size is:
   Sort by name (ascending) */
void ssort_name (Student a[], int size)
{
    inti,j;
    for(i=0;i<size;i++){
     for(j=size-1;j>i;j--){
      if(strcmp(a[j-1].name, a[j].name)>0){
       swap_student(&a[j-1], &a[j]);
      }
    }
  } 
}

/* Decide whether member id of array a[] has search key (ID) x by binary search*/
int bsearch_id (Student a[], intp, intq, intx)
{
    int = (p+q)/2;
    if(p>q){
      return-1;
    }
    if(a[t].id==x){
      return;
    }
    if(a[t].id>x){
      q = t-1;
    } else{
      p = q+1;
    }
    return bsearch_id(a,p,q,x);
}

/* Determine if member name of array a[] has search key (name) x by binary search */
int bsearch_name (Student a[], intp, intq, char x[])
{
    int = (p+q)/2;
    if(p>q){
      return-1;
    }
    if(!strcmp(a[t].name,x)){
      return;
    }
    if(strcmp(a[t].name,x)>0){
      q = t-1;
    } else{
      p = q+1;
    }
    return bsearch_name(a,p,q,x);
}

/* Store user input in each element of array a[] of length size */
void get_students (Student a[], int size)
{
    inti;
    for(i=0;i<size;i++){
        printf("a[%d]\n", i);
        printf("ID:"); scanf("%d", & a[i].id);
        printf("Name:"); scanf("%s", a[i].name);
    }
}

/* Display array a[ ] of length size */
void put_students (Student a[], int size)
{
    inti;

    printf("\nElement ID Name\n";
    for(i=0;i<size;i++){
        printf("a[%d]: %2d%-15s\n", i, a[i].id, a[i].name);
    }
}

int main (void)
{
    int size,horw,pos,key;
    char keystr [20];
    Student a [MAX_SIZE];

    printf("Number of data:"); scanf("%d", & size);
    get_students(a,size);
    printf("1)ID, 2)Name:");
    scanf("%d", &horw);

    switch(horw){
    case1:
        ssort_id(a,size); /* Sort by ID number in ascending order */
        put_students(a,size);
        printf("Search Key (ID):"); scanf("%d", & key);
        pos=bsearch_id(a,0,size-1,key);
        if(pos>=0){
            printf("Search key(ID)%d, name %s found in a[%d]\n", key, a[pos].name, pos);
        } else{
            printf("Search key(ID)%d not found\n", key);
        }
        break;
    case2:
        ssort_name(a,size); /* Sort by name in ascending order */
        put_students(a,size);
        printf("Search Key (Name):"); scanf("%s", keystr);
        pos = bsearch_name(a,0, size-1, keystr);
        if(pos>=0){
            printf("Search key (name)%s, ID %d found in a[%d]\n", keystr, a[pos].id, pos);
        } else{
            printf("Search key(name)%s not found\n", a[pos].name);
        }
        break;
    }

    return 0;
}


c

2022-09-30 21:54

1 Answers

With this, it might work.
Not verified, but

 int bsearch_name (Student a[], intp, intq, char x[])
{
    int = (p+q)/2;
    if(p>q){
      return-1;
    }
    if(!strcmp(a[t].name,x)){
      return;
    }
    if(strcmp(a[t].name,x)>0){
      q = t;
    } else{
      p=t;
    }
    return bsearch_name(a,p,q,x);
}


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.