Inline Code Example HereI REVISED MY PROGRAM

#include <iostream>
#include <cmath>
using namespace std;
int main ()


{
    const int num=100;
    float volt[num], sum=0, sum2=0 ;
    double ave2,standrddev1, standrddev2, standrddev;
    float average;
    int i, k=0 ;

    {
        cout<<"Enter the number of voltages to be analyzed:";
        cin>>k;

        if (k>100)

        {
            cout<<"Maximum of 100 voltages can only be analyzed.";
        }
    }

    while (k>100);

    for (i=0; i<k; i++)

    {
        cout<<"ENTER VOLTAGE "<<i+1<<":";
        cin>>volt[i];
        sum=sum+volt[i];

    }

        average=sum/k;
        cout<<endl<<"The average of the voltage is:"<<average<<endl;
        cout<<endl;



        for (i=0; i<k; i++)

        {
            volt[i]=pow((average-volt[i]),2);
            sum2=sum2+volt[i];
            ave2=sum2/k;
            standrddev1=(sqrt,ave2);
            standrddev2=standrddev1/k;
            standrddev=standrddev2*2;
            cout<<endl<<"The standard deviation of the voltages is:"<<standrddev<<endl;




    return 0;

        }

}
I'm just not sure if I got the correct answer/value to the question about the STANDARD DEVIATION. Is my computation correct?

Dani AI

Generated

Short answer: no — the posted revision does not compute the standard deviation correctly. Key problems are input validation, accidental overwrites, misuse of sqrt, extra/dividing steps that do not match the formula, and misplaced braces/return that can exit the program early.

Common mistakes in the thread:

  • The while (k>100); has a stray semicolon and does not validate input as intended. Also you must reject k==0 to avoid division by zero.
  • Overwriting volt[i] when computing (x-mean)^2 destroys the original data. Accumulate squared differences into a separate variable.
  • standrddev1 = (sqrt, ave2); uses the comma operator; the correct call is sqrt(ave2).
  • The final steps standrddev1/k then *2 are incorrect. Use the formula below.
  • Use double for sums/mean for better precision. Avoid pow(x,2) if x*x is simpler and faster.

A short, robust approach (conceptual steps)

  1. Validate k (1..MAX).
  2. Read k values into a vector<double>.
  3. Compute mean = sum(x_i)/k.
  4. Compute ssd = sum( (x_i - mean)*(x_i - mean) ).
  5. Population std dev = sqrt(ssd / k). Sample std dev = sqrt(ssd / (k-1)) (only if k>1).

Example C++ (concise, corrected):

#include <iostream>
#include <vector>
#include <cmath>
#include <limits>

int main() {
    const int MAX = 100;
    int k = 0;
    std::cout << "Enter number of voltages (1-" << MAX << "): ";
    while (!(std::cin >> k) || k <= 0 || k > MAX) {
        std::cout << "Please enter an integer between 1 and " << MAX << ": ";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    std::vector<double> volt(k);
    for (int i = 0; i < k; ++i) {
        std::cout << "Voltage " << (i+1) << ": ";
        while (!(std::cin >> volt[i])) {
            std::cout << "Invalid number, try again: ";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }

    double sum = 0.0;
    for (double v : volt) sum += v;
    double mean = sum / k;

    double ssd = 0.0;
    for (double v : volt) {
        double diff = v - mean;
        ssd += diff * diff;
    }

    double pop_std = std::sqrt(ssd / k);
    double sample_std = (k > 1) ? std::sqrt(ssd / (k - 1)) : 0.0;

    std::cout << "\nMean: " << mean << "\nPopulation std dev: " << pop_std;
    if (k > 1) std::cout << "\nSample std dev: " << sample_std;
    std::cout << "\n";
    return 0;
}

Quick test (as recommended): for {2,4,4,4,5,5,7,9} the mean is 5. Population std dev = 2.0; sample std dev ≈ 2.13809. Use that to verify the program.

Dude, I told you once to stick to a single thread. Initially you had two threads for this...now you've made 3!

As far as the calculations are concerned, why don't you calculate for some test cases and see if your code is giving you the same result or not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.