481 views
0 votes
0 votes

Can somebody explain this code line by line. I am unable to get

and what will be it’s output?

#include<stdio.h>
#include<stdlib.h>
 
void transpose(int n, const double *A, double *B,  const int *lda,  int *perm)
{
}
 
int transpose_equal(const double *A, const double*B, int total_size){ 
	int error = 0; 
	const double *Atmp= A; 
	const double *Btmp= B; 
	for(int i=0;i < total_size ; ++i){ 
		double Aabs = (Atmp[i] < 0) ? -Atmp[i] : Atmp[i]; 
		double Babs = (Btmp[i] < 0) ? -Btmp[i] : Btmp[i]; 
		double max =  (Aabs < Babs) ? Babs : Aabs; 
		double diff = (Aabs - Babs); 
		diff = (diff < 0) ? -diff : diff; 
		if(diff > 0){ 
			double relError = (diff/max); 
			if(relError > 1.000000e-10){ 
				error += 1; 
			} 
		} 
	} 
	printf("Number of errors: %d\t",error); 
	return (error > 0) ? 0 : 1; 
}
int main(int argc, char* argv[])
{
	FILE * f;
	int dim[10];
	int permutation[10];
	int i, j;
	if(argc < 2)
	{
		fprintf(stderr, "Please input filename to read data\n");
		exit(0);
	}
	f = fopen(argv[1], "r");
	char line[255];
	char temp[50];
	int ndim;
	while(1)
	{
		size_t total = 1;
		fscanf(f, "%d", &ndim);
		if(feof(f)){
			break;
		}
		for(i = 0; i < ndim; i++)
		{
			fscanf(f, "%d", &dim[i]);
			if(feof(f)){
				break;
			}
			printf("%d ", dim[i]);
			total *= dim[i];
		}
		printf("\t");
		for(i = 0; i < ndim; i++)
		{
			fscanf(f, "%d", &permutation[i]);
			printf("%d ", permutation[i]);
		}
 
		size_t totalsize = total * sizeof(double);
		double* A = (double*) malloc(totalsize);
		double* B = (double*) malloc(totalsize);
		double*	B_trans = (double *) malloc(totalsize);
		for(i=0; i < total ; ++i){
			A[i] = (double)i;
		}
		int r_perm[10], r_dim[10];
		for(i = 0; i < ndim; i++)
		{
			for(j = 0; j < ndim; j++)
			{
				if(permutation[j] == i)
				{
					r_perm[i] = j; 
					//r_dim[i] = dim[j];
					break;
				}
			}
			r_dim[i] = dim[permutation[i]];
		}	
		transpose(ndim, A, B_trans, dim, permutation);
		transpose(ndim, B_trans, B, r_dim, r_perm);
		transpose_equal(A, B, total);
		free(A);
		free(B);
		free(B_trans);
		printf("\n");
	}
}
 

 

Please log in or register to answer this question.

Related questions

2 votes
2 votes
2 answers
2
srestha asked May 13, 2019
909 views
Can someone explain the output of this code? and what (char*) is doing actually?#include<stdio.h struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0',...
0 votes
0 votes
1 answer
3
Ashish Roy 1 asked Apr 11, 2019
579 views
X=2;Y=++x * ++x * ++x ;Printf("%d",Y);In the above question, we have to use the final value of x or it will be evaluated seperately and then multiplied.Ex: Y= 3*4*5; or Y...
0 votes
0 votes
0 answers
4