Thread: Standard Deviation of a 2-D array

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    18

    Standard Deviation of a 2-D array

    I'm trying to write a program that randomly populates a 2D array and then calculates the standard deviation. fillArray fills the array, printArray prints the array, stdDev calculates the standard deviation, and otherStats calculates the largest and smallest numbers in the array.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    float fillArray (float array[7][5]);
    float printArray (float array[7][5], float deviation, float largest, float 
    smallest);
    float stdDev (float array[7][5]);
    float otherStats (float array[7][5]);
    
    int main (float deviation, float largest, float smallest)
    {
      float array[7][5];
      fillArray (array);
      stdDev (array);
      otherStats (array);
      printArray (array, deviation, largest, smallest);
    }
    
    float fillArray (float array[7][5])
    {
      int row, column;
      for (row = 0; row < 7; row++)
        {
          for (column = 0; column < 5; column++)
            {
              array[row][column] = (float) rand () / (float) RAND_MAX;
            }
        }
      return array[7][5];
    }
    
    float stdDev (float array[7][5])
    {
      float average, number1, number2,  deviation;
      array[7][5] = fillArray(array);
      int ROw, Col;
      for (ROw = 0; ROw < 7; ROw++)
        {
          for (Col = 0; Col < 5; Col++)
            {
              number1 = array[ROw][Col] + number1;
              average = number1 / 35;
            }
         }
    
          for (ROw = 0; ROw < 7; ROw++)
            {
              for (Col = 0; Col < 5; Col++)
                {
                  number2 = average - array[ROw][Col];
                  deviation = sqrt (number2 / 35);
                }
            }
      return deviation;
    }
    
    float otherStats (float array[7][5])
    {
      array[7][5] = fillArray(array);
      float num1, num2;             //Check which ones largest or smallest.
      float largest, smallest;
      int ROW, COLUMN;
      for (ROW = 0; ROW < 7; ROW++)
        {
          for (COLUMN = 0; COLUMN < 5; COLUMN++)
            {
              num1 = array[ROW][COLUMN];           
              num2 = array[1][1];   
              largest = num2;
              smallest = num1;
              if (num1 > num2)         
                {
                  largest = num1;
                }
              else
                {
                  smallest = num1;
                }
            }
        }
      return largest, smallest;
    }
    
    float printArray (float array[7][5], float deviation, float largest, float smallest)
    {
      int Row, Column;
      for (Row = 0; Row < 7; Row++)
        {
          for (Column = 0; Column < 5; Column++)
            {
              printf ("%4.2f ", array[Row][Column]);
            }
          printf ("\n");
        }
      printf("The standard deviation is %f, the largest is %f, the smallest is %f.\n", deviation, largest, smallest);
    }
    All it does so far is output a very nice array, but then 0s for everything else.

    Any idea what could be causing this? I'm fairly sure the logic is right..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You discard all your calculations. You also don't really have anywhere to store those calculations -- your program claims to take the deviation, largest, and smallest value from the command line rather than computing them, which is untrue. You should declare those variables for reals inside your main function, and then store the result of your function calls in them.

    (EDIT: Yes the variables exist as parameters, but since the variables aren't getting passed in they don't really belong there.)
    Last edited by tabstop; 11-15-2013 at 09:44 PM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    I think you should get rid of all those magic numbers as well. For example 7 and 5.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    okay I think I understand what you guys are saying, so I tried what you said (I think at least, I'm really bad with jargon) but I'm still having the same problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    float fillArray (int a, int b, float array[a][b]);
    float printArray (int a, int b, float array[a][b], float deviation, float largest, float smallest);
    float stdDev (int a, int b, float array[a][b], float deviation, float average);
    float otherStats (int a, int b, float array[a][b], float largest, float smallest);
    
    int main ()
    {
      int a = 7;
      int b = 5;
      float deviation, average, largest, smallest;
      float array[a][b];
      fillArray (a, b, array);
      stdDev (a, b, array, deviation, average);
      otherStats (a, b, array, largest, smallest);
      printArray (a, b, array, deviation, largest, smallest);
    }
    
    float fillArray (int a, int b, float array[a][b])
    {
      int row, column;
      for (row = 0; row < a; row++)
        {
          for (column = 0; column < b; column++)
            {
              array[row][column] = (float) rand () / (float) RAND_MAX;
            }
        }
      return array[a][b];
    }
    
    float stdDev (int a, int b, float array[a][b], float deviation, float average)
    {
      float number1, number2;
      int ROw, Col;
      for (ROw = 0; ROw < a; ROw++)
        {
          for (Col = 0; Col < b; Col++)
            {
              number1 = array[ROw][Col] + number1;
              average = number1 / (a * b);
            }
         }
    
          for (ROw = 0; ROw < a; ROw++)
            {
              for (Col = 0; Col < b; Col++)
                {
                  number2 = average - array[ROw][Col];
                  deviation = sqrt (number2 / (a * b));
                }
            }
      return deviation;
    }
    
    float otherStats (int a, int b, float array[a][b], float largest, float smallest)
    {
      float num1, num2;             //Check which ones largest or smallest.
      int ROW, COLUMN;
      for (ROW = 0; ROW < a; ROW++)
        {
          for (COLUMN = 0; COLUMN < b; COLUMN++)
            {
              num1 = array[ROW][COLUMN];
              num2 = array[1][1];
              largest = num2;
              smallest = num1;
              if (num1 > num2)
                {
                  largest = num1;
                }
              else
                {
                  smallest = num1;
                }
            }
        }
      return largest, smallest;
    }
    
    float printArray (int a, int b, float array[a][b], float deviation, float largest, float
    smallest)
    {
      int Row, Column;
    
      printf("Column #:  ");
    
      for (Column = 0; Column < b; Column++)
        {
          printf ("%d     ", Column);
        }
      printf("\nRow #|________________________________\n");
    
      for (Row = 0; Row < a; Row++)
        {
          printf("%d    |   ", Row);
    
          for (Column = 0; Column < b; Column++)
            {
              printf ("%4.2f  ", array[Row][Column]);
            }
    
          printf ("\n");
        }
      printf("The standard deviation is %f, the largest is %f, the smallest is %f.\n",
    deviation, largest, smallest);
    }
    This is the output:

    Code:
    Column #:  0     1     2     3     4     
    Row #|________________________________
    0    |   0.84  0.39  0.78  0.80  0.91  
    1    |   0.20  0.34  0.77  0.28  0.55  
    2    |   0.48  0.63  0.36  0.51  0.95  
    3    |   0.92  0.64  0.72  0.14  0.61  
    4    |   0.02  0.24  0.14  0.80  0.16  
    5    |   0.40  0.13  0.11  1.00  0.22  
    6    |   0.51  0.84  0.61  0.30  0.64  
    The standard deviation is 0.000000, the largest is 0.000000, the smallest is 0.000000.
    Last edited by lilywest; 11-15-2013 at 10:28 PM. Reason: Whoops. Missed some magic numbers.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You call the stdDev function just fine. It calculates the answer. You then throw the answer away. You should instead store that answer in the variable you created for it (called deviation).

    Note: The return value of the function is considered to be the value of the expression; in other words, the expression "stdDev (a, b, array, deviation, average)" has the value 0.35 (or whatever), so you want to assign that value to your variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-04-2011, 10:33 PM
  2. Standard Deviation, etc.
    By wovenhead in forum C Programming
    Replies: 12
    Last Post: 02-15-2011, 08:15 PM
  3. Standard deviation
    By Dontgiveup in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 01:46 PM
  4. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 11:04 PM
  5. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 10:48 PM

Tags for this Thread