Keeping value in memory in c programming


Keeping value in memory in c programming



I need some help on my code, i want my automate function to keep in memory its old values to finally return the final value after having studied all conditions. Here is my code :


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//Prototypes
int automate(int tddv_estime, int tddv_worst, int para_1_courant, int para_2_courant, int para_1_max, int para_1_min, int para_2_max, int para_2_min);
int application(int para1_courant, int para_2_courant, int state);
int fct_test(int scenario_1);

/*-----------------------------------------------------------------------
Main()
-------------------------------------------------------------------------*/
int main()
{
int scenario_1[7] = { 15, 20, 21, 22, 22, 20, 12 };
fct_test(scenario_1);
return 0;
}

/*-----------------------------------------------------------------------
FONCTION fct_test
-------------------------------------------------------------------------*/
int fct_test(int scenario_1)
{
int tddv_worst = 20;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int para_1_courant = para_1_max;
int para_2_courant = para_2_max;
int tddv_estime;
int tab_final_1[7] = { 0 };
int tab_final_2[7] = { 0 };

int i;

for (i = 0; i<7; i++) //warning nombre elements scenario (ici 6) !
{
tddv_estime = scenario_1[i];
automate(tddv_estime, tddv_worst, para_1_courant, para_2_courant, para_1_max, para_1_min, para_2_max, para_2_min);
tab_final_1[i] = para_1_courant;
tab_final_2[i] = para_2_courant;
printf("%dn", tab_final_1[i]);
printf("%dn", tab_final_2[i]);
}
}

/*-----------------------------------------------------------------------
FONCTION automate
-------------------------------------------------------------------------*/

int automate(tddv_estime, tddv_worst, para_1_courant, para_2_courant, para_1_max, para_1_min, para_2_max, para_2_min)
{
int state;

if (tddv_estime < tddv_worst)
state = 1; //Etat initial

if (tddv_estime > tddv_worst)
state = 2; //Decrement para1

if (tddv_estime < tddv_worst && para_1_courant < para_1_max)
state = 3; //Increment para1

if (tddv_estime == tddv_worst)
state = 4; //Etat Invariant 1

if (tddv_estime > tddv_worst && para_1_courant <= para_1_min)
state = 5; //Decrement para_2

if (tddv_estime < tddv_worst && para_2_courant < para_2_max)
state = 6; //Increment para2

if (tddv_estime > tddv_worst && para_1_courant <= para_1_min && para_2_courant <= para_2_min)
state = 8; //Etat radar sature


switch (state) {
case 1:
printf("ETAT INITIALn");
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

case 2:
printf("nDECREMENT PARA_1n");
para_1_courant--;
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

case 3:
printf("nINCREMENT PARA_1n");
para_1_courant++;
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

case 4:
printf("nETAT INVARIANTn");
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

case 5:
printf("nDECREMENT PARA_2n");
para_2_courant--;
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

case 6:
printf("nINCREMENT PARA_2n");
para_2_courant++;
printf("Para_1_courant = %dn", para_1_courant);
printf("Para_2_courant = %dn", para_2_courant);
return para_1_courant;
return para_2_courant;
break;

/*case 7:
return("ETAT INVARIANT 2n");
break;*/

case 8:
printf("nERREUR : RADAR SATUREn");
return para_1_courant;
return para_2_courant;
break;

while (state != 8)
automate(tddv_estime, tddv_worst, para_1_courant, para_2_courant, para_1_max, para_1_min, para_2_max, para_2_min);

}
}



When i run my code, it prints the good states but not the right values, i always get 10 and 4 that are the max and min of the initial values.
For example when i get decrement state, i want my value to be decremented, printed, and then re-used for the next loop.
Can anyone help me on this please ?





perhaps you mean static int state = 1; by keeping the state
– Tyker
2 days ago


static int state = 1;





I am not shure what you want to achieve but you look to need a structure passed from main to fct_test ? However when you have two return in a row, only the first one is executed.
– Umaiki
2 days ago


main


fct_test





What do you mean by keep in memory its old values?
– Qubit
2 days ago





No, i mean, for example if i get the state DECREMENT PARA 1, the program decrements para_1_courant (para_1_courant--;) and prints 9 for para_1, but the next step of the loop in fct-test, if i get again the state DECREMENT PARA 1, it will print again 9 but i want it to print 8.
– Mathieu TSH
2 days ago





So, you just want to define your values outside of the function? They're local now so every time you call the function it's created from scratch (and the old one is lost), just create them either global (not too nice) or pass them as a parameter (by pointer, so that you can modify the correct value, don't make a copy).
– Qubit
2 days ago




1 Answer
1



As requested, an example of passing using pointers (this is just a simple example, I'll leave it to you to apply it to your problem):


void function(int* a) { //Takes a pointer to an integer as a parameter.
(*a)++; //Dereference the pointer and increment the value of the integer it points to
}



*a accesses the value pointed to by a


*a


a



So we pass a pointer to an integer we defined in main or somewhere else and then increment the value to which it points. That way the integer doesn't go out of scope (because it was allocated in main) and now stores the new value.


main


main



You can of course pass multiple pointers to a function the same way as you do with any other type.



EDIT



To modify your code, it would look something like this, but I urge you not to just use it, read up a bit on pointers and functions, you had double return statements in your code that do not do what you think they do.


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//Prototypes
void automate(int tddv_estime, int tddv_worst, int* para_1_courant, int* para_2_courant, int para_1_max, int para_1_min, int para_2_max, int para_2_min);
void fct_test(int scenario_1);

/*-----------------------------------------------------------------------
Main()
-------------------------------------------------------------------------*/
int main()
{
int scenario_1[7] = { 15, 20, 21, 22, 22, 20, 12 };
fct_test(scenario_1);
return 0;
}

/*-----------------------------------------------------------------------
FONCTION fct_test
-------------------------------------------------------------------------*/
void fct_test(int scenario_1)
{
int tddv_worst = 20;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int para_1_courant = para_1_max;
int para_2_courant = para_2_max;
int tddv_estime;
int tab_final_1[7] = { 0 };
int tab_final_2[7] = { 0 };

int i;

for (i = 0; i<7; i++) //warning nombre elements scenario (ici 6) !
{
tddv_estime = scenario_1[i];
automate(tddv_estime, tddv_worst, &para_1_courant, &para_2_courant, para_1_max, para_1_min, para_2_max, para_2_min);
tab_final_1[i] = para_1_courant;
tab_final_2[i] = para_2_courant;
printf("%dn", tab_final_1[i]);
printf("%dn", tab_final_2[i]);
}
}

/*-----------------------------------------------------------------------
FONCTION automate
-------------------------------------------------------------------------*/

void automate(int tddv_estime, int tddv_worst, int* para_1_courant, int* para_2_courant, int para_1_max, int para_1_min, int para_2_max, int para_2_min)
{
int state;

if (tddv_estime < tddv_worst)
state = 1; //Etat initial

if (tddv_estime > tddv_worst)
state = 2; //Decrement para1

if (tddv_estime < tddv_worst && *para_1_courant < para_1_max)
state = 3; //Increment para1

if (tddv_estime == tddv_worst)
state = 4; //Etat Invariant 1

if (tddv_estime > tddv_worst && *para_1_courant <= para_1_min)
state = 5; //Decrement para_2

if (tddv_estime < tddv_worst && *para_2_courant < para_2_max)
state = 6; //Increment para2

if (tddv_estime > tddv_worst && *para_1_courant <= para_1_min && *para_2_courant <= para_2_min)
state = 8; //Etat radar sature


switch (state) {
case 1:
printf("ETAT INITIALn");
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

case 2:
printf("nDECREMENT PARA_1n");
(*para_1_courant)--;
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

case 3:
printf("nINCREMENT PARA_1n");
(*para_1_courant)++;
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

case 4:
printf("nETAT INVARIANTn");
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

case 5:
printf("nDECREMENT PARA_2n");
(*para_2_courant)--;
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

case 6:
printf("nINCREMENT PARA_2n");
(*para_2_courant)++;
printf("Para_1_courant = %dn", *para_1_courant);
printf("Para_2_courant = %dn", *para_2_courant);

break;

/*case 7:
return("ETAT INVARIANT 2n");
break;*/

case 8:
printf("nERREUR : RADAR SATUREn");

break;

while (state != 8)
automate(tddv_estime, tddv_worst, para_1_courant, para_2_courant, para_1_max, para_1_min, para_2_max, para_2_min);

}
}





I don't understand how to apply it to my problem :/
– Mathieu TSH
2 days ago





Yes, i was reading some theories about pointers in C and i started exacly like in your edit example. Nevertheless, i get " error: expected ')' before 'int' " in the line : void automate(tddv_estime, tddv_worst, int* para_1_courant, int* para_2_courant ..........) :/
– Mathieu TSH
2 days ago





It compiles just fine for me using gcc or icc. I just copied it and checked again.
– Qubit
2 days ago






Yeah it was my fault, just forgot some braces. Now it works fine ! Thanks you a lot ! now i got the main role of pointers in C aha
– Mathieu TSH
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV