2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

You are confusing characters positions with string positions. Your program is not sorting the string in the list array but attempting to sort the characters in just one of the strings. And it doesn't do that very well either.

1) go back and review the selection sort algorithm and compare it with what you have posted.

2) review your program requirements that you were assigned because I think you probably misunderstood it. Post it here if you want to.

0

Here is my updated code... Can you check where I am wrong.

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

void selection(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      { int min = i;
        for (j = i+1; j <= r; j++) 
            if (a[j] <  a[min]) min = j;

      } 
  };
int binarysearch(char a[], char v[], int l, int r)
  { 
    while (r >= l)
      { int m = (l+r)/2;
        int x;
        if (v[x] == a[m]) return m;
        if (v[x] < a[m]) r = m-1; else l = m+1;
      }
    return -1;
  };
main(void)
{
  int i;
  char *list[ 5 ] = {"the", "a", "1", "some", "any"};  
  char findme[5];
  int found;
  selection(*list,0,5);     
  printf("Enter a character to find: ");
  scanf("%s", &findme);
  found = binarysearch(*list,findme,0,5);
  if (found == -1)
  printf("\n\n%s is not in the list.",findme);
  else printf("\n\n%s is in position %d of the list.",findme,found);
  getch();
};
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.