60 lines
1.3 KiB
C
60 lines
1.3 KiB
C
|
/*
|
||
|
* Student: S.K. Soekhlal
|
||
|
* Number: 4860632
|
||
|
* Assignment: 4.6
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <math.h>
|
||
|
|
||
|
void swap(double *p, double *q){ /*Swapping*/
|
||
|
double temp;
|
||
|
temp = *p;
|
||
|
*p = *q;
|
||
|
*q = temp;
|
||
|
}
|
||
|
|
||
|
void sort(double a[], double n){ /*Bubble Sort*/
|
||
|
int i,j;
|
||
|
|
||
|
for (i = 0; i < n - 1; i++){
|
||
|
for (j = n - 1; j > i; --j){
|
||
|
if (a[j-1] > a[j]){
|
||
|
swap(&a[j-1], &a[j]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//length = calloc (num, sizeof (double));
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
sort(length, num);
|
||
|
for (i = 0; i < num; i++){
|
||
|
printf("length = %e \n", length[i]);
|
||
|
}
|
||
|
return 0;
|
||
|
}
|