Digital-Systems-Assignments/Assignment 8/main.c
2019-10-24 22:04:27 +02:00

66 lines
1.4 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 *b[], double n){
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]);
swap(&b[j-1], &b[j]);
printf("%d", b[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, w, num);
for (i = 0; i < num; i++){
for (j = 0; j < dim; j++){
printf("%le ", w[i][j]);
}
printf("\n");
}
return 0;
}