-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiincrement.c
More file actions
70 lines (64 loc) · 1.31 KB
/
Copy pathmultiincrement.c
File metadata and controls
70 lines (64 loc) · 1.31 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
#include <stdio.h>
int n, size;
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
return;
}
void MultiIncrement(int arr[]){
int i,j,d;
printf("\n\nEnter the value of i, j, delta respectively: ");
scanf("%d %d %d", &i, &j, &d);
if(i>j) swap(&i, &j);
i = i+(n-1);
j = j+(n-1);
arr[i] += d; arr[j] += d;
while((i-1)/2 != (j-1)/2){
if(i%2==1) arr[i+1] += d;
if(j%2==0) arr[j-1] += d;
i = (i-1)/2;
j = (j-1)/2;
}
printf("\n\n");
for(i=0;i<size;i++) printf("%d ", arr[i]);
printf("\n");
return;
}
void Report(int arr[]){
int i, ans=0;
printf("\n\nEnter i for element to be reported: ");
scanf("%d", &i);
int j=i;
i = i+(n-1);
while(i>=0){
ans += arr[i];
if(i==0) break;
i = (i-1)/2;
}
printf("\n\nThe %d th element is : %d.\n", j , ans);
return;
}
int main(){
printf("Enter the size of array: ");
scanf("%d", &n);
printf("\nEnter the %d elements of array now\n", n);
int i=0;
size = (2*n)-1; int arr[size];
for(i=0;i<n;i++) arr[i]=0;
i=0; arr[size-1]=0;
while(i<n){
scanf("%d", &arr[n-1+i]);
i++;
}
i=0;
//while(i<size) printf("%d:%d\n", i,arr[i++]);
int repeat = 1;
while(repeat){
printf("\n\nExit-0, Multi-Increment(i,j,delta)-1, Report(i)-2.\nEnter input: ");
scanf("%d", &repeat);
if(repeat==1) MultiIncrement(arr);
else if(repeat==2) Report(arr);
}
return 0;
}