-2

I want to design a program of n = 3 instances. Each instance is pointing to an 'instance' position of an array. Each instance is composed of two values: {error, control}. When calling a function "run_instance" these two values change to other ones.

#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)
    
}
v4lerO
  • 5
  • 2
  • 1
    Your question appears to be lacking a question, which is one of the most important parts of the question. Or else you are using Stack Overflow as a task list, but there are other applications and websites that are much more suitable for maintaining a task list. – Eljay Oct 07 '21 at 14:02
  • 2
    If you want to change the array's contents, you need to pass it as an argument to the function. Your favourite C++ book should mention how to do that. – molbdnilo Oct 07 '21 at 14:05
  • 3
    You use [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) and those [aren't part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) for dynamic "arrays" where you don't know the size at compile-time. Or [`std::array`](https://en.cppreference.com/w/cpp/container/array) if you know the size at compile-time, and it will not change. – Some programmer dude Oct 07 '21 at 14:06
  • 1
    Also remember that you *must* declare symbols (variable, functions, etc.) before you attempt to use them. – Some programmer dude Oct 07 '21 at 14:06

1 Answers1

0

There are a few problems:

n should be const or constexpr:

const int n = 3;

or

constexpr int n = 3;

arr should be an array of doubles:

double arr[n][2] { ... };

An array should be added in the parameter list in run_instance:

double run_instance(double** arr, int instance, double measured_error, int Kp, int Ki)

And finally (whew!) run_instance should be forward declared above main:

double run_instance(double**, int, double, int, int);
Captain Hatteras
  • 490
  • 3
  • 11