如何将浮点数存储在数组中以备后用? [英] How to store floats in array to be used later?
问题描述
程序用户如何将信息存储在数组中(例如浮点数),并可以在以后的程序中计算平均值?我正在尝试制作一个程序来计算某人的平均成绩。
how can a user of a program store info in arrays such as a float number and can be calculated for an average later in the program? im trying to make a program to calculate someones grade point average.
推荐答案
从Java过渡到C,这是最大的概念跳跃
Transitting from java to C, the biggest "concept jump" you have to make is Pointers.
尝试以这种方式分配浮动对象:
Try allocating your floats this way:
float *float_array = malloc(amount_of_elemts_in_array * sizeof(float))
然后可以遍历使用
float_array[index]
具有此指针将使您可以通过引用将 float_array
传入和传出函数,这非常方便,因为您不想重新创建
Having this pointer will enable you to pass float_array
in and out of functions by reference which is a great convenience since you don't want to recreate instances over every function call.
使用以下方法将 float_array
传递给函数:
Pass float_array
into functions using:
Function Declaration: void function_that_uses_float_array(float *placeholder);
Function Call: function_that_uses_float_array(placeholder);
将 float_array
传递给以下函数: / p>
Pass float_array
out of functions using:
Return statement: return a_float_pointer;
One level up the stack: float_array = function_that_returns_a_float_pointer();
数组是通过引用自动传递的。
Arrays are automatically passed by reference.
希望这可以帮助您指出正确的方向。
Hope this helps point you in the right direction.
这篇关于如何将浮点数存储在数组中以备后用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!