Reputation: 11
#include <stdio.h>
#pragma warning (disable:4996)
void cit_mat(float a[][100], int* m, int* n) {
printf("nr linii =");
scanf_s("%d", m);
printf("\n");
printf("nr coloane =");
scanf_s("%d", n);
printf("\n");
printf("Introduceti matricea: \n");
for (int i = 0; i < *m; i++) {
for (int j = 0; j < *n; j++) {
printf("x[%d][%d]=", i, j);
scanf_s("%f", &a[i][j]);
}
printf("\n");
}
}
void ordonare(float a[][100], int m, int n) {
for (int i = 0; i < m; i++) {
int ok = 1;
while (ok) {
ok = 0;
for (int k = 0; k < n - 1; k++) {
if (a[i][k] > a[i][k + 1]) {
int aux = a[i][k + 1];
a[i][k + 1] = a[i][k];
a[i][k] = aux;
ok = 1;
}
}
}
}
}
void afis_mat(float a[][100], int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%5.6f ", a[i][j]);
}
printf("\n");
}
}
void main() {
int m, n;
float a[100][100];
cit_mat(a, &m, &n);
ordonare(a, m, n);
printf("Matricea cu liniile ordonate crescator arata astfel: \n");
afis_mat(a, m, n);
}
Hi guys, so whenever I input decimals into this matrix (like 1.23, 1.45, 5.44), on the console, through the presentation of the matrix in the end there, only some numbers have the decimals, while some may appear as 4.0000, even though I have put it to be 4.55. What could cause this problem? I cannot figure it out. The code appears in Visual Studio 2019 btw.
Upvotes: 1
Views: 69
Reputation: 154592
In ordonare
, a
is a collection of floats. However you then do int aux = a[i][k+1];
. When you store a float
in an int
, it is truncated to its integral part and the value after the decimal point is discarded.
Upvotes: 1