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
#include <stdio.h>
void print_array(int arr[], int n)
{
for(int i = 0 ; i < n ; ++i)
printf("%d ", arr[i]);
printf("\n");
}
void add_value(int arr[], const int n, const int val)
{
int i;
for(i = 0 ; i < n ; i++)
arr[i] += val;
}
int sum(const int ar[], const int n)
{
int i;
int total = 0;
for(i = 0 ; i < n ; i++)
{
// total += ar[i] ++; // Wrong Implementation
total += ar[i];
}
return total;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
print_array(arr, 5);
add_value(arr, 5, 100);
print_array(arr, 5);
int s = sum(arr, n);
printf("sum is %d\n", s);
print_array(arr, 5);
return 0;
}
Enter to Rename, Shift+Enter to Preview