ne_viens
Advanced Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
int matrix() { int ret = 0, row, col, rows, cols; float tmp, sred, **matrica;
//-------------------------------------------------create matrix printf("Vvedite razmernost massiva(m*n): "); scanf("%d %d", &rows, &cols); matrica = (float**)malloc(rows * sizeof(void*));
for(row = 0; row < rows; ++row) matrica[row] = (float*)malloc(cols * sizeof(float));
//-------------------------------------------------init and print srand(time(0)); for(row = 0; row < rows; ++row) { for(col = 0; col < cols; ++col) { tmp = (rand() % 2001 - 1000) / 100.; if(col == cols - 1) tmp = -fabs(tmp); matrica[row][col] = tmp; printf("%7.2f", tmp); } puts(""); }
//-------------------------------------------------locate all negatives column for(col = 0; col < cols; ++col) { for(row = 0; row < rows; ++row) if(matrica[row][col] >= 0) break;
if(row == rows) break; }
if(row != rows) { puts("no column with all negative members found"); ret = -1; goto L0; }
//-------------------------------------------------calculate an average for(tmp = row = 0; row < rows; ++row) tmp += matrica[row][col]; sred = tmp / rows; printf("Znachenie dlya vychitania %.2f\n", sred);
//-------------------------------------------------subtract average and print for(row = 0; row < rows; ++row) { for(col = 0; col < cols; ++col) { tmp = matrica[row][col] - sred; matrica[row][col] = tmp; printf("%7.2f", tmp); } puts(""); }
//-------------------------------------------------destroy matrix L0: for(row = 0; row < rows; ++row) if(matrica[row]) free(matrica[row]); if(matrica) free(matrica); return ret; }
|