Question

C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with...

C++ ONLY!!! NOT JAVA.

The standard deviation is calculated from an array X size n with the equation

std=sqrt(sum((xi-mean)2/(n-1)))

Sum the square of difference of each value with the mean. Divide that sum by n-1.

Take the square root and you have the standard deviation.

//System Libraries
#include <iostream> //Input/Output Library
#include <cstdlib> //Srand
#include <ctime> //Time to set random number seed
#include <cmath> //Math Library
#include <iomanip> //Format Library
using namespace std;

//User Libraries

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const float MAXRAND=pow(2,31)-1;

//Function Prototypes
void init(float [],int);//Initialize the array
void print(float [],int,int);//Print the array
float avgX(float [],int);//Calculate the Average
float stdX(float [],int);//Calculate the standard deviation

//Execution Begins Here!
int main(int argc, char** argv) {
//Set the random number seed
srand(static_cast<unsigned>(time(0)));
  
//Declare Variables
const int SIZE=20;
float test[SIZE];
  
//Initialize or input i.e. set variable values
init(test,SIZE);
  
//Display the outputs
print(test,SIZE,5);
cout<<"The average = "<<avgX(test,SIZE)<<endl;
cout<<"The standard deviation = "<<stdX(test,SIZE)<<endl;

//Exit stage right or left!
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.

Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.

Thank You !!
====================================================================================

//System Libraries
#include <iostream> //Input/Output Library
#include <cstdlib> //Srand
#include <ctime> //Time to set random number seed
#include <cmath> //Math Library
#include <iomanip> //Format Library
using namespace std;

//User Libraries

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const float MAXRAND=pow(2,31)-1;

//Function Prototypes
void init(float [],int);//Initialize the array
void print(float [],int,int);//Print the array
float avgX(float [],int);//Calculate the Average
float stdX(float [],int);//Calculate the standard deviation

void init(float arr [],int length){
   for(int i=0;i<length;i++){
       arr[i]=rand()/MAXRAND;
   }
}

void print(float arr [],int length, int columns){
       for(int i=0;i<length;i++){
       cout<<arr[i]<<" ";
       if((i+1)%columns==0){
           cout<<endl;
       }
   }
   cout<<endl;
  
}

float avgX(float arr [],int length){
   float total=0.0;
   for(int i=0;i<length;i++){
       total += arr[i];
   }
   return total/length;
}

float stdX(float arr [],int length) {
  
   float average = avgX(arr,length);
   double squareSum=0.0;
       for(int i=0;i<length;i++){
       squareSum += pow((arr[i]-average),2);
   }
   squareSum /=(length-1);
   return pow(squareSum,0.5);
  
}

//Execution Begins Here!
int main(int argc, char** argv) {
//Set the random number seed
srand(static_cast<unsigned>(time(0)));
  
//Declare Variables
const int SIZE=20;
float test[SIZE];
  
//Initialize or input i.e. set variable values
init(test,SIZE);
  
//Display the outputs
print(test,SIZE,5);
cout<<"The average = "<<avgX(test,SIZE)<<endl;
cout<<"The standard deviation = "<<stdX(test,SIZE)<<endl;

//Exit stage right or left!
return 0;
}

====================================================================================

thanks !

Add a comment
Know the answer?
Add Answer to:
C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this problem is to sort a 2 dimensional array of characters. Specify the...

    The purpose of this problem is to sort a 2 dimensional array of characters. Specify the size of the array and input the array. If the array is longer than specified, output if it is larger or smaller. Utilize the outputs supplied in main(). Example supplied in 2 test cases. Input 3↵ 3↵ 678↵ 567↵ 456↵ Expected Output Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵ Input·the·number·of·rows·<=·20↵ Input·the·maximum·number·of·columns·<=20↵ Now·input·the·array.↵ The·Sorted·Array↵ 456↵ 567↵ 678↵ Input 3↵ 5↵ Ted↵ Mary↵ Bobby↵ Expected Output Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵ Input·the·number·of·rows·<=·20↵ Input·the·maximum·number·of·columns·<=20↵ Now·input·the·array.↵ The·Sorted·Array↵ Bobby↵...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Please do a flowchart which will be on the computer not on paper so i can...

    Please do a flowchart which will be on the computer not on paper so i can read it and be able to take screenshot for this c++ code this code for MasterMind //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> #include <ctime> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //main int main(int argc, char** argv) { //Set the random number seed srand(time(0)); int randomint = (rand()%5)+1;...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • // This program will read in a group of test scores (positive integers from 1 to...

    // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // #include <iostream> using namespace std; float findAverage (const int[], int); // finds average of all grades int findHighest (const int[], int); // finds highest of all grades int findLowest (const int[], int); //...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT