Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main() {
- // Enter the bit depth
- int bit = 0;
- printf("Enter the bit depth : ");
- scanf("%d", &bit);
- // Calculate the max and min value
- double max = pow(2, bit - 1);
- double min = max / pow(4, bit - 2);
- // Set the array width and height
- const int sizex = 2;
- const int sizey = pow(2, bit - 1) / 2;
- // Create dynamic array
- double *arr = (double *)malloc(sizex * sizey * sizeof(double));
- // Initialization
- for (int i = 0; i < sizex * sizey; i++) {
- arr[i] = max;
- }
- // Calculate the solution
- double tMax = max;
- double tMin = min;
- double tLen = sizex * sizey;
- while (tMin <= max) {
- // Create dynamic array (temp)
- double *tArr = (double *)malloc(tLen * sizeof(double));
- for (int i = 0; i < tLen; i++) {
- tArr[i] = 0;
- }
- // Get full value
- for (int i = 0; i < tLen; i += 2) {
- if (i == 0)
- {
- tArr[i + 0] = tMin / 4;
- tArr[i + 1] = tMin / 4 + (tMax - 1) * tMin;
- }
- else
- {
- tArr[i + 0] = tArr[i - 2] + tMin;
- tArr[i + 1] = tArr[i - 1] - tMin;
- }
- }
- // Add the value to result
- for (int i = 0; i < sizex * sizey; i++) {
- arr[i] += tArr[i / (int)(sizex * sizey / tLen)];
- }
- //
- tMax /= 2;
- tMin *= 4;
- tLen /= 2;
- }
- // Display
- for (int i = 0; i < sizex * sizey; i++) {
- printf("%4d = %lf\n", i * 2 + 1, arr[i]);
- printf("%4d = %lf\n", i * 2 + 2, arr[i]);
- }
- // Free memory
- free(arr);
- // Exit
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment