ilfe98
Utente Èlite
- Messaggi
- 3,083
- Reazioni
- 1,317
- Punteggio
- 134
Salve a tutti ho fatto un programma che doveva essere alquanto banale in c tuttavia lo scambio delle diagonali non avviene correttamente. so che sbaglio nel momento in cui la c viene riazzerata,avreste magari una soluzione più efficiente?
C:
#include"matrix.h"
extern struct matrix *scambia_diagonali(const struct matrix *m) {
struct matrix *ret = malloc(sizeof(struct matrix));
ret->cols = m->cols;
ret->rows = m->rows;
ret->data = malloc(sizeof(double)*ret->cols*ret->rows);
double temp;
for (size_t r = 0; r < m->rows; r++)
for (size_t c = 0; c < m->cols; c++) {
if (c == r) {
temp = m->data[r*m->cols + c];
ret->data[r*m->cols + c] = m->data[(r*m->cols) + (m->cols - c - 1)];
ret->data[(r*m->cols) + (m->cols - c - 1)] = temp;
}
if (c == ret->cols - 1)
break;
else
ret->data[r*m->cols + c] = m->data[r*m->cols + c];
}
return ret;
}