4
Contributors
13
Replies
15
Views
7 Years
Discussion Span

I hope you don't expect anyone to do your homework for you.

That's a good task. If you need help, let us know what you need help with and we can help.

Suggestions for your Binary search function:

``````#
int DoBinarySearch(char name[], char czStudentName[][16], int N, int *FoundPointer)
{
int Middlepoint,i=0;
int LowerB= 0;
int UpperB= N-1;
You don't want a pointer set to -1. You want the index of the student, in the array, if found. Middlepoint already gives you that.
//here you shall fullfill the code
//printf ("\nBinary search not ready! Complete it\n");
while (UpperB >= LowerB)
{
Middlepoint=(LowerB + UpperB)/2;
/* just make ONE strcmp() and assign it to a variable. then make
all your comparisons, to that variable
*/
int result = strcmp() ...
if(result <0)
Upperb=Middlepoint-1;
else if(result==0)
return Middlepoint;
else
LowerB=Middlepoint + 1;
}//COMMENT: Also found or not?

I haven't run this binary search function, but the idea is what I'm trying to convey. You need no pointers, at all, since what you want is an index (integer). Middlepoint is that number, if the target value is found. You make just one strcmp() per loop, and use the result it returns to you, to make your logic for the search, work.

You can have this function print up the results, but if you do, watch out. Generally, you get the best looking output if the calling function (the higher function), handles all messages like that.

Thanks i appreciate that idea a lot i will try it and get back to you

below is what i did:-

``````while (UpperB >= LowerB)
{
Middlepoint=(LowerB + UpperB)/2;
int result=strcmp(name, czStudentName[i]);
if (result <0)
UpperB=Middlepoint-1;
else if(result==0)
return Middlepoint;
else
LowerB=Middlepoint + 1;

break;
}//COMMENT: Also found or not?

return -1;
}
``````

Edited by Reverend Jim: Fixed formatting

Let me put some example together. Back in a bit.

Just highlight the code, and click on the [code] icon - then your program will look like a program, instead of being all squashed to the left margin of the window.

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

#define N 25

int DoBinarySearch(char name[], char SName[][N], int rows);

int main() {
int i, rows = 7;
char name[N] = {"\0"};
char SName[7][N] = {
{"Aiden"},
{"Alice"},
{"Bill"},
{"Bob"},
{"Charles"},
{"Cynthia"},
{"Dahlia"}
};

printf("\n\nEnter a name to search for: ");
fgets(name, sizeof(name), stdin);
name[strlen(name) - 1] = '\0';  //remove the newline from name

i = DoBinarySearch(name, SName, rows);
if(i > -1)
printf("\nFound it: %s", SName[i]);
else
printf("\nThat name could not be found.");

name[0] = '\0';

i = getchar();
return 0;

}
int DoBinarySearch(char name[], char SName[][N], int rows)
{
int Middlepoint, result;
int LowerB= 0;
int UpperB= rows-1;
//printf ("\nBinary search not ready! Complete it\n");
while (UpperB >= LowerB)
{
Middlepoint=(LowerB + UpperB)/2;
result = strcmp(name, SName[Middlepoint]);
if(result < 0)
UpperB = Middlepoint-1;
else if(result==0)
return Middlepoint;
else
LowerB = Middlepoint + 1;
}
}``````

When I had the strcmp() ... in the earlier code, I meant for you to finish filling in the ... with your actual array names. ;)

Thanks so much!! its also work with the pointer. but please i need a little explanation if i want to modify the list. what will i do? i mean creating another function for modify.

A modify function has 3 parts:

1) You have to ask which student name they want to modify and
put that new name, into a small auxiliary char array.

2) You have to then call a search function to see if that name exists, and if so, what is the index number. Binary search does that, and any search can do it, also.

3) Then strcpy() the new name, into the array[], at the index that the search function gave you.

Anything you can do by any other means in an array, you can also do with pointers. It just loses clarity for most people, when you start working with pointers.

NOTE: If you add a name to the array being searched by Binary Search, that name has to either be in already sorted order (not likely), or the array has to be resorted, or don't sort the names, but sort an auxiliary array of ints or pointers, and use that to work with the array[] in the binary search function. A binary search only works if the data is in sorted order (as it see's it).

Thanks i appreciate that a lot here is what i did:

``````else if (strcmp(czCmds[3],czCommand)==0 ||
czCmds[3][0]==czCommand[0]) //modify
{

printf("\nModifying: Give the name: ");
GetAnsw(name);
//printf("... repeated: %s ", name);
FindSequentially(name, czStudentName, N, &iFound);
//printf("\nreturn code from finding: %d", iFound);
if (iFound>0){ //old one exists, no insert
printf("\nGive the name you will like to change it to: ");
GetAnsw(name);
strcpy( name,czStudentName[N]);

printf("\nGive the ID: ");
GetAnsw(name);
iStudID[N]= atoi(name);
N= N +1;
printf ("\nModified! There are now %d records!", N);

}
else
//no found,  the proper place for it is at the end
{
printf("\nThe name does not exit! Try again");

}``````

But instead of modifying the name is inserting a new name in the list.

strcpy( destination, source).

How are you handling the sort requirements for the names, so Binary search will keep working?

Thanks a lot. Now i have don the modification but i have a problem. how can i make the program work so that in front of each name it will tell if the name is male or female. And how to detect if there are duplicates of names in the list.

An easy way to find duplicates is to sort the list of names, that puts the duplicates right next to each other in the list. Then run through the list and if list == list[i+1], then you have a duplicate. Stop the search at one less than normal, or else list[i+1] will be out of bounds of the size of the array.

If you need to print up other info on these people, then you need to include that info and put it into a struct along with their other pieces of info, (recommended). If you haven't had structs yet, then you can use parallel arrays for this (works, but it's not nearly as elegant).

Let's start a new thread with a better topic, if you want help with structs or parallel arrays. "Binary Search" is misleading, and that problem is solved.

This topic 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.