Holey_yan

[C] HW Template

Aug 29th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main() {
  6.     // Enter the bit depth
  7.     int bit = 0;
  8.     printf("Enter the bit depth : ");
  9.     scanf("%d", &bit);
  10.    
  11.     // Calculate the max and min value
  12.     double max = pow(2, bit - 1);
  13.     double min = max / pow(4, bit - 2);
  14.  
  15.     // Set the array width and height
  16.     const int sizex = 2;
  17.     const int sizey = pow(2, bit - 1) / 2;
  18.  
  19.     // Create dynamic array
  20.     double *arr = (double *)malloc(sizex * sizey * sizeof(double));
  21.  
  22.     // Initialization
  23.     for (int i = 0; i < sizex * sizey; i++) {
  24.         arr[i] = max;
  25.     }
  26.  
  27.     // Calculate the solution
  28.     double tMax = max;
  29.     double tMin = min;
  30.     double tLen = sizex * sizey;
  31.     while (tMin <= max) {
  32.         // Create dynamic array (temp)
  33.         double *tArr = (double *)malloc(tLen * sizeof(double));
  34.         for (int i = 0; i < tLen; i++) {
  35.             tArr[i] = 0;
  36.         }
  37.  
  38.         // Get full value
  39.         for (int i = 0; i < tLen; i += 2) {
  40.             if (i == 0)
  41.             {
  42.                 tArr[i + 0] = tMin / 4;
  43.                 tArr[i + 1] = tMin / 4 + (tMax - 1) * tMin;
  44.             }
  45.             else
  46.             {
  47.                 tArr[i + 0] = tArr[i - 2] + tMin;
  48.                 tArr[i + 1] = tArr[i - 1] - tMin;
  49.             }
  50.         }
  51.  
  52.         // Add the value to result
  53.         for (int i = 0; i < sizex * sizey; i++) {
  54.             arr[i] += tArr[i / (int)(sizex * sizey / tLen)];
  55.         }
  56.  
  57.         //
  58.         tMax /= 2;
  59.         tMin *= 4;
  60.         tLen /= 2;
  61.     }
  62.  
  63.     // Display
  64.     for (int i = 0; i < sizex * sizey; i++) {
  65.         printf("%4d = %lf\n", i * 2 + 1, arr[i]);
  66.         printf("%4d = %lf\n", i * 2 + 2, arr[i]);
  67.     }
  68.  
  69.     // Free memory
  70.     free(arr);
  71.  
  72.     // Exit
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment