Advertisement
PadmaJS

C week 09 lab 4

Nov 12th, 2022
3,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "utils.c"
  6. #include <sys/time.h>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.   struct timeval tim;
  11.   gettimeofday(&tim, NULL);
  12.   double t1 = tim.tv_sec + (tim.tv_usec / 1000000.0);
  13.  
  14.   int row, col, count = 0;
  15.  
  16.   int printVals = 0;
  17.   double r, t;
  18.  
  19.   for (int i = 0; i < argc - 1; i++)
  20.   {
  21.     if (strcmp(argv[i], "-r") == 0)
  22.     {
  23.       r = strtof(argv[i + 1], NULL);
  24.     }
  25.     else if (strcmp(argv[i], "-t") == 0)
  26.     {
  27.       t = strtof(argv[i + 1], NULL);
  28.     }
  29.     else if (strcmp(argv[i], "-v") == 0)
  30.     {
  31.       printVals = 1;
  32.     }
  33.   }
  34.  
  35.   scanf("%d", &row);
  36.   scanf("%d", &col);
  37.   double **rows = (double **)malloc(row * sizeof(double *));
  38.   for (int i = 0; i < row; i++)
  39.   {
  40.     rows[i] = (double *)malloc(sizeof(double) * col);
  41.   }
  42.  
  43.   for (int i = 0; i < row; i++)
  44.   {
  45.     for (int j = 0; j < col; j++)
  46.     {
  47.       scanf("%lf", &rows[i][j]);
  48.     }
  49.   }
  50.   for (int i = 0; i < row; i++)
  51.   {
  52.     for (int j = 0; j < col; j++)
  53.     {
  54.       if (approxEqual(rows[i][j], r, t))
  55.       {
  56.         count++;
  57.         if (printVals == 1)
  58.         {
  59.           fprintf(stdout, "r=%d, c=%d: %.6f\n", i, j, rows[i][j]);
  60.         }
  61.       }
  62.     }
  63.   }
  64.   fprintf(stdout, "Found %d approximate matches.\n", count);
  65.  
  66.   gettimeofday(&tim, NULL);
  67.   double t2 = tim.tv_sec + (tim.tv_usec / 1000000.0);
  68.   printf("%.6lf seconds elapsed\n", t2 - t1);
  69.  
  70.   return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement