Advertisement
999ms

Untitled

Feb 6th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. float find_cub(int dim, float * A){
  5.   float answer = 0, sum = 0;
  6.   for (int i=0;i<dim;i++){
  7.     sum = 0;
  8.     for(int j=0;j<dim;j++){
  9.       sum+=abs(A[i*dim+j]);
  10.     }
  11.     if(answer<sum){
  12.       answer = sum;
  13.     }
  14.   }
  15.   return answer;
  16. }
  17.  
  18. float find_oct(int dim, float * A){
  19.   float answer = 0, sum = 0;
  20.   for(int j=0;j<dim;j++){
  21.     sum = 0;
  22.     for(int i=0;i<dim;i++){
  23.       sum+=abs(A[i*dim+j]);
  24.     }
  25.     if(answer < sum){
  26.       answer = sum;
  27.     }
  28.   }
  29.   return answer;
  30. }
  31.  
  32. float find_euc(int dim, float * A){
  33.   float answer=0;
  34.   for(int i=0;i<dim;i++){
  35.     for(int j=0;j<dim;j++){
  36.       answer+=A[i*dim+j]*A[i*dim+j];
  37.     }
  38.   }
  39.   printf("%d\n", answer);
  40.   return sqrt(answer);
  41. }
  42.  
  43. int main(){
  44.   printf("Данная программа находит различные нормы матрицы\n");
  45.   int dim;
  46.   while(1){
  47.     scanf("%d", &dim);
  48.     if(dim<=0){
  49.       printf("Размерность должна быть положительным целым числом\n");
  50.     }
  51.     else{
  52.       break;
  53.     }
  54.   }
  55.   float * A =  (float *)malloc(dim*dim*sizeof(float));
  56.   for(int i=0;i<dim;i++){
  57.     printf("Введите %d линию\n", i+1);
  58.     for(int j=0;j<dim;j++){
  59.       scanf("%f", &A[i*dim+j]);
  60.     }
  61.   }
  62.   float euclNorm = find_euc(dim,A);
  63.   float cubeNorm = find_cub(dim,A);
  64.   float octNorm = find_oct(dim,A);
  65.   printf("Нормы матрицы равны:\n%.3f - кубическая\n%.3f - октаэдрическая\n%.3f - евклидова", cubeNorm, octNorm, euclNorm);
  66.   return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement