use*_*283 -1 c multidimensional-array standard-deviation
这是我的代码,它应该计算填充数组填充的随机生成数组的标准偏差。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);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助找出我的错误将不胜感激。它编译得很好,只是我的逻辑或某些东西搞砸了。
提前致谢。
这是输出:
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.
Run Code Online (Sandbox Code Playgroud)
这不是您编码问题的答案,但它是您做错了什么的一个例子。我当然不会期望这会引起太多关注(除了一些反对票和一些尖锐的批评)。
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;
}
Run Code Online (Sandbox Code Playgroud)
输出(地址值会因您的系统而异)
main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934
foo: &a = 0x7fff5fbff90c, &b=0x7fff5fbff908
Run Code Online (Sandbox Code Playgroud)
注意函数中变量的逻辑地址与main(). 该值的a和b推入临时存储(通常是一个“堆栈”,但你可能不知道那是什么没有一个正式的数据结构当然,如果你有你的腰带其中的一个,你可能就不会问这个问题)。然后进行函数调用。
示例 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;
}
Run Code Online (Sandbox Code Playgroud)
输出(地址值会因您的系统而异)
main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934, &c=0x7fff5fbff930
foo: &a = 0x7fff5fbff90c, &b=0x7fff5fbff908, &c=0x7fff5fbff904
c = 0
Run Code Online (Sandbox Code Playgroud)
刚刚发生了什么?在c我们修改的变量foo_two()是不是在变量main()。它们有不同的内存地址。我们修改的那个foo_two()是按值复制(示例#1 中讨论的“临时”变量)。
示例 #3:功能输出参数
如果只有一种方法可以直接告诉我们foo_three()变量的内存地址main()......以某种方式将该地址作为参数的“值”传递,然后使用它来存储我们需要去的结果;在main::c。
这正是指针所做的。在 C 语言中,指针是一个变量,它保存着一个特定类型的其他数据的地址。其中 anint保存整数值, aint*保存int可以找到an 的地址。
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出(地址值会因您的系统而异)
main: &a = 0x7fff5fbff938, &b=0x7fff5fbff934, &c=0x7fff5fbff930
foo: &a = 0x7fff5fbff90c, &b=0x7fff5fbff908, ptr=0x7fff5fbff930
c = 3
Run Code Online (Sandbox Code Playgroud)
这个很重要。仔细查看正在打印的内容foo_three()。该值的的ptr参数,指针,是一样的地址的的c变量main()这给我们修改的能力,int 在该地址直接。
概括
如果你想在 C 中通过地址传递一些东西,那么传递地址正是你必须做的;传递地址并将参数声明为指向变量类型的指针。在函数中使用解引用运算符向/从所述地址写入/读取数据,调用方变量将被适当修改。
做完这一切之后,回顾一下你的程序,看看你是否能理解如何将它应用于你的问题。并学习C指针。它们并不神奇,并且在大多数 C 程序中被 大量使用。值得花时间尽快学习。