数组减去放入另一个数组的平均值
array minus average put into another array?
我不知道如何启动这个函数。
我希望数组中的值从平均值中减去以生成新数组。
例:
1 2 3 old array
avg 2
new array -1 0 1
之前有一个程序,由于错误而无法运行它。
谁能给我一个提示?
#include <iostream>
#include <cmath>
using namespace std;
const int SIZE = 100;
void readdata (double [], int &);
double findaverage (double [], int);
void howfaraway (double [], int);
int main()
{
int n;
double avg;
double mark[SIZE];
readdata(mark, n);
avg = findaverage(mark, n);
cout << avg;
return 0;
}
void readdata(double numbers[], int&n)
{
cout << "Enter the size> ";
cin >> n;
for (int count = 0; count < n; count++)
{
cout << "Enter the integer> ";
cin >> numbers[count];
}
return;
}
double findaverage (double p[], int n)
{
double sum = 0;
for (int count = 0; count < n; count++)
sum = sum + p[count];
return (double) sum / n;
}
void howfaraway (double r[], double s[], int n)
{
for (int count = 0; count < n; count++)
}
我会写一些伪代码:
length = lengthOf(oldarray);
newarray = new array[length]
avg=sum(oldarray)/length;
for(i=0; i<length; i++){
newarray[i]=oldarray[i]-avg;
}
return newarray;
类似的东西。
我在你的代码中重写了一些东西。我希望这有所帮助:
#include <iostream>
#include <cmath>
using namespace std;
const int SIZE = 100;
//Variables here
int n;
double avg, numbers[SIZE];
//Functions here
void readdata ();
double findaverage ();
void howfaraway ();
int main()
{
readdata(); // Read the input and calculate the avg.
cout << "This is the Average: " << avg << endl; // Printing the avg
cout << "This is the new array: ";
howfaraway(); // Printing the new array
return 0;
}
void readdata()
{
cout << "Enter the size: ";
cin >> n;
avg = 0;
for (int i = 0; i < n; i++)
{
cout << "Enter the integer: ";
cin >> numbers[i];
avg += numbers[i]; // Getting the total sum
}
if(n > 0)
avg /= n; // Getting the avg
}
void howfaraway()
{
for (int i = 0; i < n; i++)
cout << numbers[i] - avg << " "; // Printing each new element
cout << endl;
}
相关文章:
- 按平均值替换数组中的元素
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 浮点格式数组的平均值
- 使用动态数组求和和和求平均值
- 玩数字编程挑战(子数组的平均值)
- 查找数组最后 3 个数字的平均值
- C++数组的最大值、最小值、平均值函数
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- 我需要以尺寸方式计算数组的平均值
- 计算数组中特定线的平均值并存储在另一个数组中
- 尝试在数组(C )中找到平均值时,获取-NAN(IND)输出
- 从数组中获取值并计算平均值,然后存储在数组中
- 如何在数组中的范围内添加所有数字?然后找到该范围的平均值
- C++数组,平均值(初学者)
- 数组元素的奇怪平均值
- c++中const数组的平均值模板
- C++计算2D数组中可变宽度数字的平均值
- 计算数组平均值时出错
- C++数组平均值工作不正常