这是我的代码,它应该计算fillArray填充的随机生成的数组的标准偏差。应该使用stdDev计算标准偏差。 otherStats应该在数组中找到最大值和最小值。到目前为止,唯一生成的是0,最小和最大偏差。这是代码:

#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 deviation, float average);
float otherStats (float array[7][5], float largest, float smallest);

int main ()
{
  float deviation, average, largest, smallest;
  float array[7][5];
  fillArray (array);
  stdDev (array, deviation, average);
  otherStats (array, largest, smallest);
  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 deviation, float average)
{
  float number1, number2;
  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], float largest, float smallest)
{
  array[7][5] = fillArray(array);
  float num1, num2;             //Check which ones largest or 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;

  printf("Column #:  ");

  for (Column = 0; Column < 5; Column++)
    {
      printf ("%d     ", Column);
    }
  printf("\nRow #|________________________________\n");

  for (Row = 0; Row < 7; Row++)
    {
      printf("%d    |   ", 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);
}


找出我的错误的任何帮助将非常感激。它编译很好,只是我的逻辑或某些东西弄乱了。

提前致谢。

这是输出:

Column #:  0     1     2     3     4
Row #|________________________________
0    |   0.53  0.04  0.44  0.93  0.93
1    |   0.72  0.28  0.74  0.64  0.35
2    |   0.69  0.17  0.44  0.88  0.83
3    |   0.33  0.23  0.89  0.35  0.69
4    |   0.96  0.59  0.66  0.86  0.44
5    |   0.92  0.40  0.81  0.68  0.91
6    |   0.48  0.22  0.95  0.92  0.15
The standard deviation is -0.000000, the largest is 0.000000, the smallest is 0.000000.

最佳答案

这不是您的编码难题的答案,但这只是您做错事的一个示例。我当然不会期望这会引起太多关注(除了一些否决票和一些尖锐的批评之外)。

C是一种按值传递语言。尽管大多数经验者不容易理解,但这包括所有内容(数组不能承受,但如果您正确使用术语“值”,即使是数组也可以)。

例1:值参数

这个简单的例子说明了传递给函数的参数的地址与提供给函数的参数的地址不同。

#include <stdio.h>

int foo_one(int a, int b)
{
    printf("foo:  &a = %p, &b=%p\n", &a, &b);
    return a+b;
}

int main()
{
    int a = 1, b = 2;
    printf("main: &a = %p, &b=%p\n", &a, &b);
    foo_one(a,b);
    return 0;
}


输出(地址值在您的系统上会有所不同)

main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934
foo:  &a = 0x7fff5fbff90c, &b=0x7fff5fbff908


请注意,函数中变量的逻辑地址与main()中的变量不同。 ab的值被放入临时存储区(通常是“堆栈”),但是如果没有正式的数据结构课程,以及如果您拥有其中的一个,您可能不知道那是什么您可能不会问这个问题)。然后进行函数调用。



例2:功能失调的参数外

那你为什么在乎。好吧,现在考虑一下:

#include <stdio.h>

void foo_two(int a, int b, int c)
{
    printf("foo:  &a = %p, &b=%p, &c=%p\n", &a, &b, &c);
    c = a + b;
}

int main()
{
    int a = 1, b = 2, c = 0;
    printf("main: &a = %p, &b=%p, &c=%p\n", &a, &b, &c);
    foo_two(a,b,c);
    printf("c = %d\n", c);
    return 0;
}


输出(地址值在您的系统上会有所不同)

main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934, &c=0x7fff5fbff930
foo:  &a = 0x7fff5fbff90c, &b=0x7fff5fbff908, &c=0x7fff5fbff904
c = 0


刚刚发生了什么?我们在c中修改的foo_two()变量不是main()中的变量。它们具有不同的内存地址。我们在foo_two()中修改的一个是按值复制(示例1中讨论的“临时”变量)。



例3:功能外参数

如果只有一种方法可以直接告诉foo_three()变量在main()中的内存地址...以某种方式将该地址作为参数的“值”传递,然后使用它将结果存储在需要的地方走;在main::c中。

指针正是这样做的。在C语言中,指针是一个变量,该变量持有一个特定类型的其他数据的地址。 int保存一个整数值,而int*保存一个可以找到int的地址。

#include <stdio.h>

void foo_three(int a, int b, int* ptr)
{
    printf("foo:  &a = %p, &b=%p, ptr=%p\n", &a, &b, ptr);
    *ptr = a + b; // note the dereference operator
}

int main()
{
    int a = 1, b = 2, c = 0;
    printf("main: &a = %p, &b=%p, &c=%p\n", &a, &b, &c);
    foo_three(a,b,&c); // note the address of c being passed
    printf("c = %d\n", c);
    return 0;
}


输出(地址值在您的系统上会有所不同)

main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934, &c=0x7fff5fbff930
foo:  &a = 0x7fff5fbff90c, &b=0x7fff5fbff908, ptr=0x7fff5fbff930
c = 3


这个很重要。仔细查看foo_three()中正在打印的内容。 ptr参数的值(指针)与cmain()变量的地址相同,这使我们能够直接在该地址处修改int



摘要

如果要通过C中的地址传递某些内容,那么传递地址正是您要做的;传递地址并声明参数为指向变量类型的指针。在函数中使用解引用运算符向/从所述地址写入/读取数据,然后将适当修改调用方变量。

现在,接下来,请回顾您的程序,看看是否可以理解如何将其应用于您的问题。并研究C指针。它们不是魔术,并且在大多数C程序中都大量使用。值得花时间尽快学习。

关于c - 计算C中2D阵列的标准偏差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20014430/

10-13 00:08