Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <sstream>

int main() {
    const int numReadings = 6;
    std::vector<double> voltages(numReadings);
    double sum = 0.0;
    
    // Input voltage readings
    std::cout << "Enter 6 voltage readings: ";
    for (int i = 0; i < numReadings; ++i) {
        std::cin >> voltages[i];
        sum += voltages[i];
    }
    
    // Calculate average voltage
    double average = sum / numReadings;
    double tenPercent = average * 0.10;
    double fifteenPercent = average * 0.15;
    
    // Display average voltage
    std::cout << std::fixed << std::setprecision(1);
    std::cout << "The average is " << average << " volts." << std::endl;
    std::cout << "10% = " << tenPercent << " volts." << std::endl;
    std::cout << "15% = " << fifteenPercent << " volts." << std::endl;
    
    // Check for anomalies
    std::ostringstream problems;
    
    for (int i = 0; i < numReadings - 1; ++i) {
        float voltage_check = std::abs(voltages[i] - average);
        if (voltage_check > tenPercent) {
            problems << "The voltage at hour " <<  i + 1 << " was " << voltages[i] 
                     << " volts (difference of " << voltage_check << " volts)." << std::endl;
        }       
    }
    
    for (int i = 0; i < numReadings - 1; ++i) {
        float voltage_check = std::abs(voltages[i + 1] - voltages[i]);
        if (voltage_check > fifteenPercent) {
            problems << "Voltage change from hour " << i + 1 << " to hour " << i + 2
                      << " was " << voltage_check << " volts." << std::endl;
        }
    }    
    
    
    if (problems.str().empty()) {
        std::cout << "No problems were encountered." << std::endl;
    }
    else {
        std::cout << "The following problems occured:" << std::endl << problems.str();
    }
    
    return 0;
}
Leave a Comment