Sum of all subarray | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Sum of all subarray

int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } int curr=0; for(int j= i;j<n;j++){ curr+=a[j]; cout<<a[j]<<endl; } return 0; } Error: I is not declared in this line(for(int j= i;j<n;j++))

1st Mar 2021, 4:25 PM
soni singh
soni singh - avatar
5 Answers
+ 3
Hey there soni singh , Please add/put your code in the code playground and then link it, so that we cam help you more easily :) https://www.sololearn.com/post/75089/?ref=app
1st Mar 2021, 5:01 PM
Matthew
Matthew - avatar
+ 1
You are trying to assign the value of 'i' to the loop variable 'j', but 'i' is local to its own loop and therefore not accessible on the outside. You could move its declaration out of the loop, but that would still make little sense as it would equal 'n' at the end, resulting in the second loop not running. What elements of the array are you trying to sum up?
1st Mar 2021, 5:01 PM
Shadow
Shadow - avatar
+ 1
soni singh That is not very helpful. We can't assist you if you don't work with us.
1st Mar 2021, 5:22 PM
Shadow
Shadow - avatar
0
J
1st Mar 2021, 5:11 PM
soni singh
soni singh - avatar
0
a[j]
1st Mar 2021, 5:28 PM
soni singh
soni singh - avatar