Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void solve(double**, int, int);
- double ves(double*, int);
- void swapstr(double**, int, int, int);
- void mult(double**, int, int, FILE*);
- void swapstr(double **a, int m, int i, int j)
- {
- int k; double tmp;
- double *pi = a[i], *pj = a[j];
- for (k=0;k<m;k++) {
- tmp = pi[k];
- pi[k] = pj[k];
- pj[k] = tmp;
- }
- }
- double ves(double *str, int m)
- {
- int i; double v = 0;
- for (i=1;i<m;i++) {
- if (fabs(str[i]-str[i-1]) > v)
- v = fabs(str[i]-str[i-1]);
- }
- return v;
- }
- void solve(double **a, int n, int m)
- {
- int i,j;
- for (i=0;i<n;i++) {
- for (j=0;j<n-1-i;j++) {
- if (ves(a[j],m) < ves(a[j+1],m))
- swapstr(a,m,j,j+1);
- }
- }
- }
- void mult(double **a, int n, int m, FILE *out)
- {
- int i,j; double sum = 0;
- for(i=0;i<n;i++) {
- sum = 0;
- for(j=0;j<m;j++)
- sum += a[i][j]*a[n-1][j];
- fprintf(out,"%g ",sum);
- }
- }
- int main(void)
- {
- double **a;
- int i,j,n,m;
- FILE *in, *out;
- in = fopen("input.txt","r");
- if (in == NULL)
- return -1;
- if (fscanf(in,"%d%d",&n,&m)!=2) {
- fclose(in);
- return -1;
- }
- if (m<1 || n<1) {
- fclose(in);
- return -1;
- }
- a = (double**)malloc(n*sizeof(double*));
- for (i=0;i<n;i++)
- a[i]=(double*)malloc(m*sizeof(double));
- for (i=0;i<n;i++) {
- for (j=0;j<m;j++) {
- if (fscanf(in,"%lf",&a[i][j])!=1) {
- fclose(in);
- for (i=0;i<n;i++) free(a[i]);
- free(a);
- return -1;
- }
- }
- }
- fclose(in);
- solve(a,n,m);
- out = fopen("output.txt","w");
- fprintf(out,"%d %d\n",n,m);
- for(i=0;i<n;i++) {
- for(j=0;j<m;j++)
- fprintf(out,"%g ",a[i][j]);
- fprintf(out,"\n");
- }
- fprintf(out,"\n");
- mult(a,n,m,out);
- fclose(out);
- for (i=0;i<n;i++)
- free(a[i]);
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement