Home > Net >  Changing array value through a function
Changing array value through a function

Time:10-08

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)
    
}

CodePudding user response:

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);
  • Related