Question

I'm working doing a planificatin for a company that makes car parts. I need to loop among a lot of things that are maybe a bit difficult to explain by text. The aim is to get a final optimum order of the parts to have the biggest profits.

This is the entire code, I don't think you need to understand everything as I think it's a pointers or & problem.

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 8
using namespace std;

int cmp(const void *a, const void *b);
void replacerow(int a[SIZE][SIZE], int row);
void replacecol(int a[SIZE][SIZE], int col);
float calculate_time(float tiempo_piezas, int piezas_almacen);
float calculate_planification(int* arrayNumeros);
int* grasp(char* piezadeseada);

typedef struct pair {
        int value, column;
} Pair;

char a[] = {0,1,2,3,4,5,6,7}, x;
int array_grande[5040][8];
int cont = 0;
int precio[8] = {5, 8, 10, 3, 1, 2, 10, 6}; 
double tiempo[8] = {1, 2, 1, 3, 1, 2, 3, 1}; //Pa hacer 100 piezas necesitan 1h, Para hacer 100 piezas necesitan 2h...
int almacen[8] = {1200, 500, 700, 850, 900, 450, 950, 900}; 
int numerodepiezas = 8;
int pedido[5] = {300, 2, 3, 4, 7};
int piezasdisponibles[3] = {0, 3, 7};

int main(int argc, char** argv) {

    float optimo = 0.0;
    int* arrayoptimo;
    char nombres_pieza[][20] = {"pieza0", "pieza1", "pieza2", "pieza3", "pieza4", "pieza5", "pieza6", "pieza7"};


    for (int i = 0; i < 100; i++) {
        int r = rand() % 8;
        int* arrayNumeros = grasp(nombres_pieza[r]);
        float precio = calculate_planification(arrayNumeros);
        if (precio > optimo) {
            memcpy (  arrayoptimo, arrayNumeros, sizeof(arrayNumeros) );
            optimo = precio;
        }
    }

    printf("Optimum sequence found\n");
    printf("total profit: %d euros", precio);
    printf("\n");
    for(int kl=0; kl< sizeof(arrayoptimo)/sizeof(int);kl++);
        printf( " %d ", arrayoptimo[kl]);
}

int* grasp(char* piezadeseada) {
    int f, c, q, myrow, r;
    int FILAS = 8;
    int array_aleatorio[8] = {0, 1, 2, 3, 4, 5, 6, 7};

    printf("El orden aleatorio es: ");
    for (int i = 0; i < 8; i++)
        printf(" %d ", array_aleatorio[i]);
    printf("\n\n");

    int piezas[8][8] = {
        -5, 2, 2, 5, 3, 2, 1, 1,
        0, -5, 5, 2, 4, 3, 0, 0,
        0, 4, -5, 2, 1, 2, 3, 2,
        0, 3, 1, -5, 1, 2, 3, 4,
        2, 5, 6, 5, -5, 1, 2, 7,
        8, 2, 0, 0, 0, -5, 1, 1,
        1, 2, 2, 1, 1, 6, -5, 4,
        0, 1, 3, 2, 0, 0, 0, -5,
    };

    char nombres_pieza[][20] = {"pieza0", "pieza1", "pieza2", "pieza3", "pieza4", "pieza5", "pieza6", "pieza7"};
    Pair data_pair[FILAS];

    // Prints matrix
    printf("\t");
    for (q = 0; q < FILAS; q++) {
        printf("%s", nombres_pieza[q]);
        printf("\t\t");
    }
    printf("\n");
    for (f = 0; f < FILAS; f++) {
        printf("%s", nombres_pieza[f]);
        printf("\t");
        for (c = 0; c < FILAS; c++) {
            printf("%d", piezas[f][c]);
            printf("\t\t");
        }
        puts("");
    }

    int iter = 0;
    int columna_a_borrar;
    int barras_perdidas = 0;
    int arrayNumeros[8];   

    for(int i=0;i<=8;++i) {
        if (!strcmp(piezadeseada, nombres_pieza[i])) {
            myrow = i;
        }
    }


    while (iter<sizeof (piezas) / sizeof (int)) {
        if (iter == 0) {
            arrayNumeros[0] = myrow;
        }
        for (int i = 0; i < FILAS; ++i) {
            if (piezas[myrow][i] != -5 && piezas[myrow][i] != -10) {
                data_pair[i].value = piezas[myrow][i];
                data_pair[i].column = i;
            } else {
                data_pair[i].value = INT_MAX;
                data_pair[i].column = i;
            }
        }
        if (iter < (numerodepiezas - 3)) {
            qsort(data_pair, FILAS, sizeof (Pair), cmp);
            for (int i = 0; i < 3; ++i)
                //printf("\nvalue = %d, column = %d", data_pair[i].value, data_pair[i].column);
                srand(time(NULL));
            int randNum = (rand() % 3);
            columna_a_borrar = data_pair[randNum].column;
            barras_perdidas = barras_perdidas + data_pair[randNum].value;


        } else if (iter < (numerodepiezas - 1)) { 
//printf("\n\nLos dos valores minimos de esta fila son:");
            qsort(data_pair, FILAS, sizeof (Pair), cmp);
            for (int i = 0; i < 2; ++i)
                //printf("\nvalue = %d, column = %d", data_pair[i].value, data_pair[i].column);

                // Generamos un número aleatorio para elegir la columna a borrar
                srand(time(NULL));
            int randNum = (rand() % (numerodepiezas - (iter + 1)));
            columna_a_borrar = data_pair[randNum].column;
            barras_perdidas = barras_perdidas + data_pair[randNum].value;

        } else if (iter == (numerodepiezas - 1)) {
            break;
        }

        replacerow(piezas, myrow);
        replacecol(piezas, myrow); 
        myrow = columna_a_borrar;
        strcpy(piezadeseada, nombres_pieza[myrow]);
        arrayNumeros[iter + 1] = myrow;
        iter = iter++;
    } // while end

    printf("\n\n\nSalgo del while y acambo. Array de numeros;\n");
    for (int p = 0; p < 8; p++)
        printf(" %d ", arrayNumeros[p]);
    printf("\n\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf("++++++  El orden de introduccion de las piezas es:  +++++\n");
    for (int p = 0; p < 8; p++)
        printf("++++++    %d - %s \n", p + 1, nombres_pieza[arrayNumeros[p]]);
    printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf("++++++  Barras perdidas en este proceso: %d\n", barras_perdidas);
    printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");

    return arrayNumeros;    
}


int cmp(const void *a, const void *b) {
        Pair *pa = (Pair *)a;
        Pair *pb = (Pair *)b;
        return pa->value - pb->value;
}
void replacerow(int a[SIZE][SIZE], int row) {
        for (int i=0;i<SIZE;++i) {
                a[row][i] = -10;
        } 
}

void replacecol(int a[SIZE][SIZE], int col) {
    int r;
        for(r=0;r<SIZE;++r) {
                a[r][col] = -10;
        }
}


float calculate_time(float tiempo_piezas, int piezas_almacen) {
        int time = (piezas_almacen * tiempo_piezas) / 100;
        return time;
}

int calcular_piezas(float tiempo_piezas, int tiempo_disponible){
    int piezas = ( tiempo_piezas / 100) * tiempo_disponible;
    return piezas;
}

float calculate_planification(int* arrayNumeros) {
    printf("start planification!\n");
    float horas_dia = 24;
    float tiempo_pieza;
    int horas_dia_restantes, tiempo_pieza_dia, num_dia = 0;
    float precio_total = 0.0;

    int p = 0;

    while (num_dia > 5 || p < sizeof (arrayNumeros) / sizeof (int)) {
        tiempo_pieza = calculate_time(tiempo[arrayNumeros[p]], almacen[arrayNumeros[p]]);

        if (tiempo_pieza > 24) {
            if ( ( ((num_dia + (int)(tiempo_pieza / 24) )*24 ) + ((int)tiempo_pieza % 24) ) < (5 * 24) ) 
            {
                num_dia += (int) (tiempo_pieza / 24);
                horas_dia_restantes = horas_dia - ((int)tiempo_pieza % 24);
                precio_total += almacen[arrayNumeros[p]] * precio[arrayNumeros[p]];
            } else {
                int piezas = calcular_piezas(almacen[arrayNumeros[p]], (5 * horas_dia) - num_dia * horas_dia - horas_dia_restantes);
                precio_total += piezas * precio[arrayNumeros[p]];
            }
        } else {
            if (horas_dia - tiempo_pieza > 0) {
                horas_dia_restantes = horas_dia - tiempo_pieza;
                precio_total += almacen[arrayNumeros[p]] * precio[arrayNumeros[p]];
            } else {
                num_dia++;
                horas_dia_restantes = horas_dia - tiempo_pieza;
                horas_dia_restantes = horas_dia + horas_dia_restantes;
                precio_total += almacen[arrayNumeros[p]] * precio[arrayNumeros[p]];
            }

        }
        p++;
    }
        printf("end planification\n");

}

And here's the part where I'm having errors. It's when I try to print the optimum array (if you comment this part, the code works):

printf("Optimum sequence found\n");
printf("total profit: %d euros", precio);
printf("\n");
for(int k=0; k< sizeof(arrayoptimo)/sizeof(int);k++);
    printf( " %d ", arrayoptimo[k]);

And the error is:

main.cpp:55: error: name lookup of 'k' changed for new ISO 'for'
scoping main.cpp:54: error: using obsolete binding at 'k'

I've invested all my afternoon trying to fix the code but it's impossible and I'm going crazy. Any help is very appreciated.

Was it helpful?

Solution

You have an unnecessary (in fact incorrect) semicolon at the end of the first line of this code:

for(int k=0; k< sizeof(arrayoptimo)/sizeof(int);k++);
    printf( " %d ", arrayoptimo[k]);

The for loop terminates with an empty body at the semicolon, so the printf is actually not part of the loop, despite indentation suggesting otherwise. The compiler then complains because you are trying to use a variable, k, which was defined inside the for loop and went out of scope when it ended.

To fix the problem just remove the extraneous semicolon.

OTHER TIPS

I can't see where you allocate any memory for arrayoptimo. I see where you use memcpy to copy content into it, but at that point it is just an uninitialized integer pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top