# we are assigned to develop a c++ program that accepts a list of a maximum of 100 voltages as input, determine both the average and standard deviation of the input voltages, and then displays the results. There are still errors and it says it requires array or pointer type. Can someone help me with this? :(((((

#

// project
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
double average, deviation, squareddev, standarddev, dev;
int num, volt, val, sum=0;
int i, j, k;
{
cout<<"Enter the number of voltages to be analyzed:";
cin>>num;

for(i=0; i=num; i++)

{
cout<<endl;
for(j=0; j<num; j++)

{
cout<<"Enter voltage"<<val[j]<< ":" ;
cin>>volt;

if(volt>100)
volt=num[i];
{
cout<<endl;
cout<<"Sorry; The volt you entered to be added should be at 100 only...";
return 0;
}

sum=sum+volt;
}
}
average=sum/num;

for(k=0; k=num; k++)

{
dev=volt [i]-average;
deviation=deviation+dev;

cout<<"The average of the voltage is:"<< average<<endl;
cout<<"The standard deviation of the voltage is:"<<standarddev<<endl;

return 0;

}

4
Contributors
5
Replies
6
Views
4 Years
Discussion Span

This code is a total mess. I mean, by reading it we can't understand what you are trying to do....

Many things have gone wrong. You have not initialized any arrays, still you want to save values to a array.

So please write down the algorithm (Steps), then we can know what your code want to do, then help u.....

Eg.
1. Get Number (say N) of voltages to be added.
2. Get N number of voltages...
3..... so on.....

int num[100], volt, val, sum=0; \\num should be a array....


Then

num[i]=volt; \\Value of volt get saved to num[i]! Notice how it differs from your code....


Then just use a single loop.....

  for(i=0; i=num; i++)
{
cout<<endl;
cout<<"Enter voltage "<<i+1<< ":" ;
cin>>volt;


After which i didn't get what you were trying to do........

If your class has covered functions by now then please use them. Structure your code so that you don't mess up your main. Its like delegating works to helpers. Have separate functions for average, standard eviation and others (if you can think up some) and call those methods from inside main.

As some of the other members pointed out, you could divide your code into multiple functions, making it more clear, and also, more easy to handle when error/modiffications occures.

Start by thinking what you need as basic functions:

int getInput(){
//cout
//cin
}


Would be one basic one.
Another one would be initializing variables:

int* createArray(int max){
//creates the array
//returns it.
}

//in main

int main(){
//variables
int* array=createArray(max);
}


And so on and so forth.

# I 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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.