-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixSum.c
More file actions
78 lines (65 loc) · 1.51 KB
/
PrefixSum.c
File metadata and controls
78 lines (65 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Aim of the program: Given an array arr[] of size N, find the prefix sum of the array. A prefix
sum array is another array prefixSum[] of the same size, such that the value of prefixSum[i] is
arr[0] + arr[1] + arr[2] . . . arr[i]. */
#include <stdio.h>
#include <stdlib.h>
#define N 100000
int main()
{
FILE *file;
int *numbers;
int *prefix_sum;
int i, n;
file = fopen("C:/Users/KIIT/Desktop/input.txt", "r");
if (file == NULL)
{
perror("Error opening file");
return 1;
}
if (fscanf(file, "%d", &n) != 1)
{
perror("Error reading number from file");
fclose(file);
return 1;
}
numbers = (int *)malloc(n * sizeof(int));
if (numbers == NULL)
{
perror("Error");
fclose(file);
return 1;
}
prefix_sum = (int *)malloc(n * sizeof(int));
if (prefix_sum == NULL)
{
perror("Error");
free(numbers);
fclose(file);
return 1;
}
for (i = 0; i < n; i++)
{
if (fscanf(file, "%d", &numbers[i]) != 1)
{
perror("Error");
free(numbers);
free(prefix_sum);
fclose(file);
return 1;
}
}
fclose(file);
prefix_sum[0] = numbers[0];
for (i = 1; i < n; i++)
{
prefix_sum[i] = prefix_sum[i - 1] + numbers[i];
}
for (i = 0; i < n; i++)
{
printf("%d ", prefix_sum[i]);
}
printf("\n");
free(numbers);
free(prefix_sum);
return 0;
}