Mathieu TSH
Mathieu TSH

Reputation: 69

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("%d\n", tab_final_1[i]);
        printf("%d\n", 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 INITIAL\n");
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

    case 2:
        printf("\nDECREMENT PARA_1\n");
        para_1_courant--;
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

    case 3:
        printf("\nINCREMENT PARA_1\n");
        para_1_courant++;
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

    case 4:
        printf("\nETAT INVARIANT\n");
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

    case 5:
        printf("\nDECREMENT PARA_2\n");
        para_2_courant--;
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

    case 6:
        printf("\nINCREMENT PARA_2\n");
        para_2_courant++;
        printf("Para_1_courant = %d\n", para_1_courant);
        printf("Para_2_courant = %d\n", para_2_courant);
        return para_1_courant;
        return para_2_courant;
        break;

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

    case 8:
        printf("\nERREUR : RADAR SATURE\n");
        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 ?

Upvotes: 0

Views: 412

Answers (1)

Qubit
Qubit

Reputation: 1255

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

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.

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("%d\n", tab_final_1[i]);
        printf("%d\n", 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 INITIAL\n");
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

    case 2:
        printf("\nDECREMENT PARA_1\n");
        (*para_1_courant)--;
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

    case 3:
        printf("\nINCREMENT PARA_1\n");
        (*para_1_courant)++;
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

    case 4:
        printf("\nETAT INVARIANT\n");
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

    case 5:
        printf("\nDECREMENT PARA_2\n");
        (*para_2_courant)--;
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

    case 6:
        printf("\nINCREMENT PARA_2\n");
        (*para_2_courant)++;
        printf("Para_1_courant = %d\n", *para_1_courant);
        printf("Para_2_courant = %d\n", *para_2_courant);

        break;

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

    case 8:
        printf("\nERREUR : RADAR SATURE\n");

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

    }
}

Upvotes: 2

Related Questions