Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking an array of data for similar elements in C
    text
    copied!<p>I created an "address" structure. Each address (xx.yy.zz.mm) consists of an xx, yy, zz and mm element, all of which are ints. Each address also has a "name" element associated with it.</p> <p>I have an array of up to 100 addresses called "network". Here is an example of some elements in network:</p> <p>186.88.1.21 Tyler<br> 186.88.9.11 Bob<br> 101.21.0.13 Tom<br> 111.11.3.89 Chuck<br> 101.21.5.99 Luke </p> <p>I need to check each address and see if there are other addresses from the same location. Two addresses are from the same location if elements xx and yy are identical. If there are 1 or more addresses from the same location, I need to output this information.</p> <p>Below is some code I wrote to try to do this:</p> <pre><code>char temp[11]; int nameCount; for (i = 0; i &lt; count; i++) { char names[100][10] = {}; strcpy(temp, network[i].name); temp[11] = '\0'; nameCount = 0; for (j = i + 1; j &lt; count; j++) { if (network[i].xx == network[j].xx &amp;&amp; network[i].yy == network[j].yy) { strcpy(names[nameCount], network[j].name); nameCount++; } } if (nameCount == 0) printf("No matches for %s.\n", temp); else { printf("%s ", temp); for (j = 0; j &lt; nameCount; j++) printf("and %s ", names[i]); printf("are from the same location.\n\n"); } } </code></pre> <p>This code works for the first two addresses in the array which are from the same location, but it doesn't work for the rest (although it looks like it almost does -- it's printing blanks instead of names, but it has the right number of blanks). The output for the addresses I listed above is (sorry for the bad formatting):</p> <pre> Tyler and Bob are from the same location. No matches for Bob . Tom and [space] and [space] are from the same location. No matches for Chuck . Luke and [space] are from the same location. No matches for Nick . </pre> <p>It also seems like there is a newline character that has been added to the end of each name. </p>
 

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