C homework help: Searching through arrays -


I am working on a problem where I have given a serial array of integers, and I should take input from the user , Look for the input, and return the first instance of that input (if it exists) and the number of times it appears.

I wrote a program with the following approach: I take an input from the user, then I use binary search to find the value. If it exists, then I store the index as m . After this, I type two while the loop first loop checks for the number of value events on the left and the other does the same, but for example, binary search 5 might be looking for , And finds it, however, it is the third, that is, the land. {.....5,5, ** 5 **, 5 ....} . The first while both loops left and the other will be counted, while the loop one will be counted on the right. Then I will add them all and return the total number of examples. If the input value is not present, then I leave the code already mentioned and return only 1. In the body of the

main function, I then check the return value. If it is -1, then I tell the user that the price is not found. If the return value is> = 0, then I print the necessary information.

Anyway, I have written the code for the program, but I can not get it to work properly. I found a SEG mistake error, I do not know what I'm doing wrong though. Anyway, any help would be appreciated. I have been fasting my head on this problem for a while. It has been interesting and difficult, and I think I have the right logic; But I can not get it to work properly, anyway, here's the code:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; # Include & lt; Stddef.h & gt; / * Function prototype * / integer gate_num_of_in ts (constant int * ar, size_t r, int n, size_t * f, size_t * count); Int main () {int i; Int n / * Input Variable * / int arr [] = {1,1,2,3,4,4,4,4,5,5,6,7,7,7,7,8,9,9}; / * Array of sorted integers * / size_t r = sizeof (ARE [i]) / size (int); / * Correctly bound * / size_tf; / * First Match Index * / Size_T * FPTR; FPtr = & amp; F; Size_t count; / * Total number of matches * / size_t * countPtr; CountPtr = and calculations; Printf ("\ nPlease input the integers you want to find. \ N"); Scanf ("% d", & amp; n); Int a = get_num_of_ints (arr, r, n, fPtr, countPtr); If (a == -1) printf ("% d is not found. \ N", N); Else if (a & gt; = 0) {printf ("The first index is% d. \", F); Printf ("The total number of values ​​is% d. \ N", counting); } Return 0; } / * Function definition * / int light_nim_of_nits (constant int * ar, size_t r, int n, size_t * f, size_t * count) {int l = 0; Int m; Int w = r; Size_t * FPTR; Size_t * countPtr; While (L & T; = R) {M = L + (RL) / 2; If (ARR [M] lieutenant; N) L = M + 1; And if (ARR [M]> N) R = M-1; And if (ARR [M] == N) M = M; break; } If (L> R = 1; If (M> = 0) {int j = m-1; Int L = 0; While (ARR [J] == ARR [M] and J.A.T.T = 0) {L ++; J--; } If (h> = 0 & amp; l> gt; 0) * fPtr = j; Else * fPtr = m; Int h = m + 1; Int r = 0; While (ARR [H] == ARM [M] and H & l; = w) {R ++; H ++; } * CountPtr = (R + L + 1); Return * FPTR; } And if (m == - 1) returns -1; }   <
  Amp;; & Amp; j & gt; = 0)  

You should change the order of the two positions here, or you will try to read arr [1] . The only thing to loop on the other side is the same thing.

Another problem is that the r array size must start at least 1, because arr [array_size] is finally last.

Edit:

A serious problem is that you are writing unchanged pointers on countPtr and fPtr you * Count and * f . This is probably what will be easy in whether segfault is debugger.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -