title: CS50 Course - Volume -
body: <p>I would like to ask a question regarding a one lab task from CS 50's course - Introduction to CS. I do not like to consult my &quot;homework&quot; as I am trying to do everything on my own, but I would like to be honest - I hit a wall over here. My code compiles well and produces an expected outcome. The aim of this task is to create a short c program, that will open a wmv file, then copy a header to the output file, and then multiply each sample by provided factor. My program deals with all of these tasks - as far as I can test (I have even compared input and output files with Audacity). However, tests indicate that audio is not correctly altered compared to the provided factor and expected outcome. I am providing a prt of source code below. You can just indicate what I should be looking for, as honestly - I have no idea. For me - everything is just fine.</p>
  <pre><code>float factor = atof(argv[3]);
  
  uint8_t HEADER [HEADER_SIZE]; //const int HEADER_SIZE = 44;
  int16_t buffer;
  int16_t* bufferp = &amp;buffer;
  fread(HEADER, sizeof(uint8_t), HEADER_SIZE, input);
  fwrite(HEADER, sizeof(uint8_t), HEADER_SIZE, output);
  
  while(feof(input) == 0)
  {
      fread(&amp;buffer, sizeof(int16_t), 1, input);
      if (*bufferp * factor &gt;= MAX) //const int MAX = 32767; &lt;&lt;Prevents overflow&gt;&gt;
          {
              *bufferp = MAX;
          }
      else
          {
              *bufferp = *bufferp * factor;
          }
      fwrite(&amp;buffer, sizeof(int16_t), 1, output);
  }
  
  // Close files
  fclose(input);
  fclose(output);
  return 0;
  </code></pre>
  <p>Thanks for all your help!</p>
  <p>EDIT 30.01.2021
  : Properly working version no. 1:</p>
  <pre><code>while (fread(&amp;buffer, sizeof(int16_t), 1, input) != 0)
  {
      if (buffer * factor &gt;= MAX) //This deals with integral overflow.
      {
          buffer = MAX;
      }
      else
      {
          buffer = buffer * factor;
      }
      fwrite(&amp;buffer, sizeof(int16_t), 1, output);
  }
  </code></pre>