C ++数组(入门) [英] C++ array (Beginner)
问题描述
我错过了向量数组,抱歉(数组在瑞典语中是vektor)
I misstook arrays for vectors, Sorry (array is vektor in swedish)
我需要一些正在制作的程序的帮助.这是一项任务,所以我真的需要了解我如何做到这一点,而不仅仅是获得代码:P
I would need some help with a program I'm making. It is a assignment so I really need to understand how I do this and not just get the code :P
我需要制作一个包含10个数字"的数组(我希望在程序运行时使它们可编辑). 完成此操作后,我需要使程序计算所有数字"/
I need to make a array containing 10 "numbers" (I would like to make them editable when the program is running). After I'v done this I need to make the program calculate the "average value" of all the numbers "/
如果您还可以选择多少个数字作为平均值,如果有人可以分享一些有关如何使用该数字的知识,那将是非常整洁的:P 无论如何,我已经尝试了一些代码来制作无效的向量,我不妨在这里添加它:
Would be pretty neat if you could pick how many numbers you wanted the average value of as well, if anyone could share some knowledge in how I should to that :P Anyways, I'v tried some code to make the vector that didn't work, I might as well add it here:
int vector[10];
和 vector [0]数字1:"; 等等,以便在向量中输入数字.
and vector[0] "number 1: "; and so on for the input of numbers in the vector.
int sum = vector[0] + vector[1] + ...
cout << "average value is: " << sum/5;
应该可以获取平均值(对吗?)
should work for getting the average value though (right?)
我应该补充:
float average(int v[], int n)
对于这件事,真的也无法做到.
to this thing as well, can't really se how though.
任何帮助/知识都太棒了!干杯.
Any help/knowledge at all would be awesome! Cheers.
推荐答案
如下所示的函数可用于计算包含n个元素的数组的平均值.
A function like following would work for computing average of an array containing n elements.
float average(int v[], int n)
{
float sum = 0;
for(int i = 0 ; i < n ; i++)
{
sum += v[i]; //sum all the numbers in the vector v
}
return sum / n;
}
您可以像完成操作一样声明数组,但是我建议您将其命名为vector,以避免混淆.关于更改数组中数字的游览问题,您可以执行以下操作,例如,将循环从1改为10,然后让用户输入所有字段的值.
You can declare your array as you have done however i do recommend you to name it something else then vector to avoid confusion. About tour issue with changing the numbers in the array you can do this by for example maning a loop going from one to 10 and then make the user enter values for all the fields.
vektorpåsvenska =数组p engelska(矢量ärnågotannat:))
Vektor på svenska = array på engelska (vector är något annat :))
这篇关于C ++数组(入门)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!