title: Printing dollar amounts using separate arrays for dollars and cents
body: <p>I am trying to take user input, store it in an array for the number, and then take the dollars and cents portion of the numbers, store those in separate arrays, and then print the formatted number.</p>
  
  <p>This is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() 
  {
      int i;
      float amounts[5];
  
  printf("Enter numbers as dollar amounts: \n");
  
  for(i=0; i&lt;5; i++) {
      printf("Value %d: ", i+1);
      scanf("%f", &amp;amounts[i]);
  }
  
  long dollars[5];
  long cents[5];
  
  for (i=0; i&lt;5; i++) {
      dollars[i]=(int)amounts[i];
      cents[i]=(amounts[i]-dollars[i]);
      cents[i]*=100;
  }
  
  for (i=0; i&lt;5; i++) {
      printf("\n$%ld.", dollars[i]);
      printf("%ld", cents[i]);
  }
  
  return 0;
  }
  </code></pre>
  
  <p>I am somewhat unsure as to how I could take the cents and store them in an array as a long type. When executing the program, I get the correct dollars but the cents portion always comes out as 0. Any suggestions?</p>
  
  <p>Edit:</p>
  
  <p>So I attempted to solve some of the issues by doing this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() 
  {
  int i, j;
  float amounts[5];
  
  printf("Enter numbers as dollar amounts: \n");
  
  for(i=0; i&lt;5; i++) {
      printf("Value %d: ", i+1);
      scanf("%f", &amp;amounts[i]);
  }
  
  long dollars[5];
  long cents[5];
  double decimal[5];
  
  for (i=0; i&lt;5; i++) {
      dollars[i]=trunc(amounts[i]);
  }
  
  for (i=0; i&lt;5; i++) {
      printf("\n$%ld.", dollars[i]);
  
      decimal[i]=100*(amounts[i]-dollars[i]);
      cents[i]=(int)decimal[i];
  
      if(cents[i]&lt;10)
          printf("0%ld", cents[i]);
      else
          printf("%ld", cents[i]);
  }
  
  return 0;
  }
  </code></pre>
  
  <p>However, now I'm getting everything that is less than 10 for cents[i] to output 1 less than what it should be. So 3.06 prints as $3.05. The same for 3.12 for some reason. </p>
  
  <p>Also, to some of the critique about what variable types I'm using, this is for an assignment and it dictates what types are to be used.</p>
  
  <blockquote>
    <p>4 Write a program that will read five values from the keyboard (use a loop) and store them in an array of type float with the name amounts. Create two arrays of five elements of type long with the names dollars and cents. Store the whole number part of each value in the amounts array in the corresponding element of dollars and the fractional part of the amount as a two-digit integer in cents (e.g., 2.75 in amounts[1] would result in 2 being stored in dollars[1] and 75 being stored in cents[1]). Output the values from the two arrays of type long as monetary amounts (e.g., $2.75)</p>
  </blockquote>