Thread: Solving Standard Deviation with Arrays

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Solving Standard Deviation with Arrays

    So I have to solve the standard deviation from a array of numbers inputted by the user. The program must also print out the array for the user to see. The user will be allowed to input numbers untill there is 100 values or they input a negative number. The problem I keep on getting is that my function stdDev not all control paths return a value. I believe the problem is that i could equal zero which would make the average divide by zero so it wouldn't pass through. I'm not sure on how to correct this problem, as there could be something else wrong with my code. I appreciate the help beforehand.
    Code:
    #include<iostream>
    #include<cmath>
    usingnamespace std;
    void printarray(double[], int);
    double stdDev(double[], int);
    double average(double[], int);
    constint MAXSIZE = 100;
    int main()
    {
      double array1[100];
      int i = 0;
      double somenumber;
      double S;
      do {
        cout <<
            "Please enter a number to enter into the array or a negative number to stop."
            << endl;
        cin >> somenumber;
        if (somenumber >= 0) {
          array1[i] = somenumber;
          i++;
        }
      } while (somenumber >= 0 && i <= MAXSIZE);
      cout << "There were " << i << " numbers entered into the array." << endl;
      printarray(array1, i);
      S = stdDev(array1, i);
      cout << "The Standard Deviation for your array is " << S << " ." << endl;
      return 0;
    }
    
    void printarray(double numbarray[], int size)
    {
      for (int j = 0; j < size; j++) {
        cout << "Your array will be printed out below." << endl;
        cout << numbarray[j] << endl;
      }
    }
    
    double stdDev(double s[], int size)
    {
      double sum = 0;
      double avg = average(s, size);
      for (int l = 0; l < size; l++) {
        sum += pow(s[l] - avg, 2);
        sqrt(sum / size);
        return sqrt(sum / size);
      }
    }
    
    double average(double s[], int size)
    {
      double sum = 0;
      for (int i = 0; i < size; i++) {
        sum += s[i];
      }
      return sum / size;
    }
    Last edited by Salem; 11-02-2012 at 11:58 AM. Reason: demungled the horrid formatting - please "PASTE AS TEXT"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Please paste as text in future.

    Copying this from your IDE with it's own colouring rules doesn't work well.

    [ FONT=Consolas][ SIZE=2][ COLOR=#0000ff][ FONT=Consolas][ SIZE=2][ COLOR=#0000ff][ FONT=Consolas][ SIZE=2][ COLOR=#0000ff]#include[ /COLOR][ /SIZE][ /FONT][ /COLOR][ /SIZE][ /FONT][ /COLOR][ /SIZE][ /FONT][ FONT=Consolas][ SIZE=2][ COLOR=#a31515][ FONT=Consolas][ SIZE=2][ COLOR=#a31515][ FONT=Consolas][ SIZE=2][ COLOR=#a31515]<iostream>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The problem I keep on getting is that my function stdDev not all control paths return a value.
    So why is the return statement INSIDE the for loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Sorry that made me look stupid. I'm sorry for the colored text as well. Now when I try to start without debugging, I keep getting an errors LNK2019 and LNK1120, which usually means I made a easy mistake somewhere again, but I can't seem to find it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Linker Tools Error LNK2019
    Visual Studio 2005
    Error Message
    unresolved external symbol 'symbol' referenced in function 'function'

    Look in the function printed in blue.
    Look at the symbol printed in red.

    If it's one of your functions, then check the prototype / implementation for spelling and parameter type mis-matches.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard Deviation in C
    By skmightymouse in forum C Programming
    Replies: 6
    Last Post: 04-28-2012, 07:54 PM
  2. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 11:04 PM
  3. standard deviation in need help!!!
    By voltare in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 06:46 AM
  4. arrays and standard deviation
    By bruceness in forum C Programming
    Replies: 1
    Last Post: 10-28-2002, 09:35 PM
  5. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM