title: C Program - adding, removing, and printing dominos from an array of dominos
body: <p>Here are the program specs:</p>
  
  <p>This assignment will be to add and remove dominos from an array of dominos. Your structure will be a domino. It will hold 3 fields (used, number1 and number2).
  The used field will either hold Y or N. If used is a Y, then number1 and number 2 are the two numbers for the domino. Your main function will only allocate the array
  of dominos and read the option for which action to take. Actions are print, add, remove and quit (exits the program).</p>
  
  <p>You must write at least the following four functions.</p>
  
  <pre><code>int getIndex();
  void addDomino(Domino * d, int size);
  void removeDomino(Domino * d, int offset);
  void print(Domino * d, int size);
  </code></pre>
  
  <p>The getIndex function will get the index to be removed.</p>
  
  <p>The addDomino function will find a free spot, and then read in number1 and number2 for that domino. It will also set that domino as being used (used = ‘Y’).</p>
  
  <p>The removeDomino function will remove the domino at the location index. This can be done by setting the used flag for that index to ’N’);</p>
  
  <p>Problem: I am not getting the removeDomino function to work properly. It only removes one element and wont work again.</p>
  
  <p>Problem: I am not getting the addDomino function to write over the removed domino index and then continue as normal. My function will only add to the end of the array.</p>
  
  <p>Please Help.</p>
  
  <p>Here is the program I wrote:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct
  {
      int number1;
      int number2;
      char used;
  
  }domino;
  
  int getIndex();
  void addDomino(domino * d, int size);
  void removeDomino(domino * d, int offset);
  void print(domino * d, int size);
  
  int main()
  {
      int size = 0;
      int index = 0;
      int offset = 100;
      int counter = 0;
      int i = 0;
  
      char * dummy = malloc(sizeof(char));
  
      printf("Enter the max number of dominoes: ");
      scanf("%d%c", &amp;size, dummy);
  
      domino * d = malloc(sizeof(domino) * size);
      domino current[size];
  
      char * input = malloc(sizeof(char));
  
      (*input) = ' ';
  
      while((*input) != 'q')
      {
          printf("Enter (p) to print, (a) to add, (r) to remove, and (q) to quit: ");
          scanf("%c%c", input, dummy);
  
          switch(*input)
          {
              case 'p':
  
              index = counter;
  
              printf("Printing Dominoes\n");
  
              for(index = 0; index &lt; counter; index++)
              {   
  
                  if(current[index].used == 'y')
                  {
  
                      print(&amp;current[index], index);
  
                  }
                  else
                  {
  
                  }
  
              }
  
              break;
  
              case 'a':
  
  
                  addDomino(&amp;current[index], index);
  
                  index++;
  
                  counter = index;
  
  
              break;
  
              case 'r':
  
                  getIndex();
  
                  removeDomino(&amp;current[offset], offset);
  
              break;
  
              case 'q':
  
                  printf("All done\n");
  
              break;
  
              default:
  
              break;
          }   
      }
  
      return 0;
  }
  
  
  int getIndex()
  {
      int offset = 0;
      char dummy = ' ';
  
      printf("Enter the index:");
      scanf("%d%c", &amp;offset, &amp;dummy);
  
      return offset;
  }
  
  void addDomino(domino * d, int size)
  {
      char * dummy = malloc(sizeof(char));
  
      printf("Enter number 1\n:");
      scanf("%d%c", &amp;d-&gt;number1, dummy);
  
      printf("Enter number 2\n:");
      scanf("%d%c", &amp;d-&gt;number2, dummy);
  
      d-&gt;used = 'y';
  }
  
  void removeDomino(domino * d, int offset)
  {
      d-&gt;used = 'n';
  }
  
  void print(domino * d, int index)
  {
  
      printf("Domino %d (%d,%d)\n", index, d-&gt;number1, d-&gt;number2);
  }
  </code></pre>