【发布时间】:2021-11-27 15:01:36
【问题描述】:
我想设计一个 n = 3 个实例的程序。每个实例都指向数组的“实例”位置。每个实例由两个值组成:{error, control}。调用函数“run_instance”时,这两个值会更改为其他值。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int Ki = 6;
int Kp = 4;
int n = 3;
int arr[n][2] = {
{0.1, 11},
{0.001, 21},
{0.0001, 31}
};
double measured_error = 0.3;
int instance = 2;
run_instance(instance, measured_error, Kp, Ki);
}
double run_instance(int instance, double measured_error, int Kp, int Ki)
{
//new value = 21 + Kp * measured_error + (Ki-Kp)*0.001 (last error of this instance)
//update arr[n][2] as{
// {0.1, 11},
// {measured_error, new_value},
// {0.0001, 31}
// }
//return new value = 21 + Kp * measured_error + (Ki-Kp)*0.001 (last error of this instance)
}
【问题讨论】:
-
您的问题似乎缺少问题,这是问题中最重要的部分之一。或者你正在使用 Stack Overflow 作为任务列表,但还有其他应用程序和网站更适合维护任务列表。
-
如果你想改变数组的内容,你需要把它作为参数传递给函数。你最喜欢的 C++ 书籍应该提到如何做到这一点。
-
您使用variable-length arrays 和那些aren't part of C++。将
std::vector用于在编译时不知道大小的动态“数组”。或者std::array,如果你在编译时知道大小,它不会改变。 -
还请记住,您必须在尝试使用符号(变量、函数等)之前声明它们。
标签: c++ arrays algorithm vector