I want to get numbers from user in a single line for example:
2 1 2 3 4
The first number
: 2
means that my Matrix
should be with size
of N
and the next 4
numbers should insert into my Matrix
(Matrix
dimension should be N²
).
The thing is that if wrong number of numbers inserted for example: 2 1 2 3
After the number 2
i expected 4
numbers and here i have only 3
so in this case i want to break
and currently this is not the case here.
int dimension, num;
int *mat;
printf("Please enter numbers: ");
scanf("%d", &num);
int matIndex = 0;
/* Set Matrix dimension. */
dimension = num;
if (dimension < 2)
{
printf("Size must be posiitve integer and bugger then 1.");
return 1;
}
else
{
mat = malloc(dimension * dimension * sizeof *mat);
if (mat == NULL) {
fprintf(stderr, "%s\n", "Error in malloc");
exit(1);
}
}
// All the numbers will be taken as elements of the dynamically allocated array,
for (int i = 0; i < dimension*dimension; i++)
scanf("%d", &mat[i]);