search function not returning anything at all

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 639 Views - Last Post: 13 May 2013 - 08:23 AM Rate Topic: -----

#16 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 05:45 PM

There is a semicolon here where it shouldn't be. The ampersand is not needed either.

01	int search(char whichLetter[], char &favorite, int n);




Do you see what is incorrect here?

11	        if(whichLetter[mid] = favorite)
12	            return mid;




This won't work either because the array is not sorted.

13	        else if(whichLetter[mid] > favorite)




The search function won't work because the array is not sorted alphabetically - you have stated the letters are jumbled.

You will have to use a linear search - just check each element in turn, probably with a for loop.

~

This post has been edited by #define: 12 May 2013 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#17 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 05:56 PM

Oh, I need the relational operator...==.

After giving it another run, entering J as my favorite, it now printed: "The letter 9 occurs 2010345941 times."
Then I entered d, and it printed: "The letter 3 occurs 2010345941 times"
Both are really not right because the data file being used simply says: "Oh, what a tangled web we weave, When first we practise to deceive! --"Marmion", Sir Walter Scott, 1808"

So J should be 0, and D should be 2

I do have a sort function that is supposed to have the list ordered by the time I enter a favorite letter. Are you saying my sort function must be wrong?

This post has been edited by breezett93: 12 May 2013 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#18 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 06:17 PM

View Postbreezett93, on 13 May 2013 - 01:56 AM, said:

I do have a sort function that is supposed to have the list ordered by the time I enter a favorite letter. Are you saying my sort function must be wrong?


No, you are sorting the other array by highest occurrence. The whichLetter array was originally sorted alphabetically, now is sorted to correspond to the highest occurrence.

The binary search algorithm which is looking for a character, needs the whichLetter array sorted alphabetically, but you don't have that.
Was This Post Helpful? 0
  • +
  • -

#19 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 06:26 PM

Is the linear search the same as the sequential search? I havent heard of the linear search before

This is my knowledge of a sequential search:
int search(const int A[], int n, int value)
{
int index = 0;
while(index < n && A[index] != value)
{
++index;
}
if(index < n && A[index] == value)
return(index);
else
return(-1);
}


This post has been edited by breezett93: 12 May 2013 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

#20 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: search function not returning anything at all

Posted 12 May 2013 - 07:13 PM

Quote

Is the linear search the same as the sequential search


Yes.

Btw, your search does some unnecessary comparisons. You don't have to check if A[index] == value at the end. If index < n, then it is implied that the condition A[index] == value was true and broke the loop.

i.e.
int search(const int arr[], int n, int val) {
   int i = 0;

   for(; i < n && arr[i] != val; i++)
      /* Searching */;
   return i < n ? i : -1;
}


This post has been edited by jjl: 12 May 2013 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#21 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 07:19 PM

ok good. Now the example one has 1 array and 2 int parameters, while my sort has 2 arrays and 1 int value. So would that sort function really change that much??
Was This Post Helpful? 0
  • +
  • -

#22 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 07:41 PM

The search and sort functions do different things.

Your sort function didn't look quite right before.
Was This Post Helpful? 0
  • +
  • -

#23 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 08:03 PM

Well if sorting it by highest occurrence wont work with the search function, then I guess it needs to be sorted alphabetically. I may lose some points for it but oh well.
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: search function not returning anything at all

Posted 13 May 2013 - 05:09 AM

This is why I was suggesting using the struct. You can sort by the frequency, and then output the corresponding letter. Or you can get your parallel arrays approach working.
Was This Post Helpful? 0
  • +
  • -

#25 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 13 May 2013 - 08:23 AM

Ok. Ill have to read up on structures then. Thank you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2