2019-10-24 14:17:22 +00:00
|
|
|
/*
|
|
|
|
* Student: S.K. Soekhlal
|
|
|
|
* Number: 4860632
|
|
|
|
* Assignment: 4.6
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2019-10-24 20:04:27 +00:00
|
|
|
void swap(double **p, double **q){ /*Swapping*/
|
|
|
|
double *temp;
|
2019-10-24 14:17:22 +00:00
|
|
|
temp = *p;
|
|
|
|
*p = *q;
|
|
|
|
*q = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-24 20:04:27 +00:00
|
|
|
void sort(double *a[], double *b[], double n){
|
|
|
|
int i,j;
|
2019-10-24 14:17:22 +00:00
|
|
|
for (i = 0; i < n - 1; i++){
|
|
|
|
for (j = n - 1; j > i; --j){
|
|
|
|
if (a[j-1] > a[j]){
|
2019-10-24 20:04:27 +00:00
|
|
|
swap(a[j-1], a[j]);
|
|
|
|
swap(&b[j-1], &b[j]);
|
|
|
|
printf("%d", b[j]);
|
2019-10-24 14:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:04:27 +00:00
|
|
|
|
2019-10-24 14:17:22 +00:00
|
|
|
int main(){
|
|
|
|
int dim, num;
|
|
|
|
int i,j;
|
|
|
|
double **w;
|
|
|
|
double *length, temp;
|
|
|
|
|
|
|
|
scanf ("%d %d", &dim, &num); /*read N and M*/
|
|
|
|
w = calloc (num, sizeof (double *)); /*allocate array of M pointers*/
|
|
|
|
for (i = 0; i < num; i++){
|
|
|
|
/*allocate space for N dimensional vector*/
|
|
|
|
w[i] = calloc (dim, sizeof (double));
|
|
|
|
/*read the vector*/
|
|
|
|
for (j = 0; j < dim; j++){
|
|
|
|
scanf ("%le", &w[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:04:27 +00:00
|
|
|
length = calloc (num, sizeof (double));
|
2019-10-24 14:17:22 +00:00
|
|
|
for (i = 0; i < num; i++){
|
|
|
|
temp = 0;
|
|
|
|
for (j = 0;j < dim; j++){
|
|
|
|
temp = temp + pow(w[i][j], 2);
|
|
|
|
length[i] = sqrt(temp);
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 20:04:27 +00:00
|
|
|
sort(length, w, num);
|
2019-10-24 14:17:22 +00:00
|
|
|
for (i = 0; i < num; i++){
|
2019-10-24 20:04:27 +00:00
|
|
|
for (j = 0; j < dim; j++){
|
|
|
|
printf("%le ", w[i][j]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2019-10-24 14:17:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|