Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You’ve gone to work for a consulting firm that specializes in converting existin

ID: 3769431 • Letter: Y

Question

You’ve gone to work for a consulting firm that specializes in converting existing rooms into recording studio. They have asked you to write a program that inputs a set of loudness measurements for a room and prints out basic statistics. The measurements are made by a playing a series of 12 different tones, and recording the reading from a sound level meter onto a file. The meter reading range from 50 to 126 decibels(a measure of loudness). Your program, however, is to output the measurements relative to the first tone—that is , to show how much each individual reading differs from the first. After all the data have been read, the program is to print out the highest and lowest reading. I'm missing something but i'm not sure what it is this is what i have so far:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
   // Declare and open input file
   ifstream inputFile;
   inputFile.open ("acoustic.dat");       // Input file being called
  
  
   // Declarations
   double Actual_value;               // Holds the input during each iteration
   double Relative_value;               // Holds subtraction current and initial_value
   double Highest_value;               // Holds subtraction high and initial_value
   double Lowest_value;               // Holds subtraction low and initial_value
   double Initial_value;               // Holds first reading
   double high;                       // Holds the highest value input
   double low;                           // Holds the lowest value input
   int high_num;                       // Holds the number that is highest in sequence
   int low_num;                       // Holds the number that is lowest in sequence
   int Reading_num;                   // Holds the current reading of measurement

  
   if (!inputFile)
   {
       // This will terminate program if the file does not open
  
       cout << "Can not open input file." << endl;
                              
   }

   // Initializing variables and output
   Reading_num = 1;
   Relative_value = 0.0;

   cout << setw(15) << "Reading Number" << setw(16)
       << "Actual Reading" << setw(20) << "Relative Reading"
       << endl;

   inputFile >> Initial_value;           // Input initial value

   // Write first line of output
   cout << fixed << showpoint << setprecision(4) << setw(8)
       << Reading_num << setw(20) << Initial_value << setw(18)
       << Relative_value << endl;

   // Starting process
   high_num = 1;
   Highest_value - Initial_value;
   high = 0.0;
   low_num = 1;
   Lowest_value - Initial_value;
   low = 0.0;
   Reading_num = 2;

   while (Reading_num <= 12)
   {
       inputFile >> Actual_value;
       Relative_value = Actual_value - Initial_value;
       cout << setw(8) << Reading_num << setw(22) << Actual_value
           << setw(18) << Relative_value << endl;

       if (Actual_value > high)
           high = Actual_value;
           high_num = Reading_num;
           Highest_value - Relative_value;

   }

   if (Actual_value < low)
   {
       low = Actual_value;
       low_num = Reading_num;
       Lowest_value - Relative_value;
   }

   Reading_num++;

   // Print high and low readings
   cout << "Highest reading number is" << Highest_value << endl;
   cout << "Highest reading is " << high << endl;
   cout << "Highest relative value is " << high_num << endl;
   cout << "Lowest reading number is " << Lowest_value << endl;
   cout << "Lowest reading is " << low << endl;
   cout << "Lowest relative value is " << low_num << endl;

   inputFile.close();
   return 0;

}

Explanation / Answer

if (Actual_value < low)
   {
       low = Actual_value;
       low_num = Reading_num;
       Lowest_value - Relative_value;
   }

   Reading_num++;

This piece of code is out of the while loop. So Reading_num never incremented. So it went into a infinite loop.

Correct code:

while (Reading_num <= 12)
{
inputFile >> Actual_value;
Relative_value = Actual_value - Initial_value;
cout << setw(8) << Reading_num << setw(22) << Actual_value
<< setw(18) << Relative_value << endl;
if (Actual_value > high){
high = Actual_value;
high_num = Reading_num;
Highest_value - Relative_value;
   }
   if (Actual_value < low)
   {
   low = Actual_value;
   low_num = Reading_num;
   Lowest_value - Relative_value;
   }
   Reading_num++;
   }