Note that there are some explanatory texts on larger screens.

plurals
  1. POC - Search for same values close to each other in an array?
    primarykey
    data
    text
    <p>I have an array that contains x elements. I want to search through each element in the array to see if it matches my input value. However, there is a rule: The integer to search for must be in at least two elements of the array AND these elements must either be next to each other, or have maximum one element between each other. So this is what I came up with:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { int elements; int input; int i; printf("How many elements in the array? "); scanf("%d", &amp;elements); int array[elements]; printf("Ok, please input %d integer values: ", elements); for(i=0; i &lt; elements; i++) { scanf("%d", &amp;array[i]); } printf("Which integer should I search for? "); scanf("%d", &amp;input); for(i=0; i &lt; elements; i++) { if((array[i] == input &amp;&amp; array[i+1] == input) || (array[i] == input &amp;&amp; array[i+2] == input)) { printf("Match!\n"); break; } else { printf("Match not found!\n"); break; } } getchar(); getchar(); getchar(); return 0; } </code></pre> <p>It doesn't work like intended, because if there are two elements between the integers I search for, it will still find a match.</p> <p>Example of how the program should look:</p> <pre class="lang-none prettyprint-override"><code>Array: 1, 2, 3, 4, 5, 5 Integer to search for: 5 Match found! // 5 and 5 are next to each other Array: 1, 2, 3, 2, 4, 5 Integer to search for: 2 Match found! // 2 and 2 has only one element (3) between each other Array: 1, 2, 1, 1, 2, 5 Integer to search for: 2 Match not found! // 2 and 2 has more than one element between each other </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload