15,741,892 members
1.00/5 (1 vote)
See more:
int n = nums.size();
int sum=0;
vector <int> ans(n);

for(int i =0; i!= n; i++){
sum += nums[i];
ans.push_back(sum);
}
return ans;
}
};

What I have tried:

according to me, its right solution but giving me ans [0,0,0,0,1,3,6,10] instead of [1,3,6,10]. I am not able to understand why???
Posted
Updated 19-Oct-22 9:05am

## Solution 1

With no idea what the question or data actually look like, we can't even begin to guess what the problem might be.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

## Solution 3

The problem is vector <int> ans(n), which creates an empty vector of n elements, all of which will be 0. You then append the running sums to that vector. Just use vector <int> ans.

Bharti Saiinii 18-Aug-22 20:09pm
thankyou it worked.
Greg Utas 18-Aug-22 20:55pm
You're welcome. But like some of the other answers suggested, learn to use the debugger. It is the only way to find problems in more complicated software, so it's a critical skill that every developer must learn.

## Solution 4

Quote:
I am not able to understand why???

You already have been told the answer, there is a tool that can help you to understand what is going on by yourself.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Bharti Saiinii 18-Aug-22 20:12pm
Thankyou

## Solution 5

int n = nums.size();
int sum=0;
vector <int> ans;

for(int i =0; i!= n; i++){
sum += nums[i];
ans.push_back(sum);
}
return ans;
}
};
//Don't use size of ans vector :-i.e ans(n)

v2

## Solution 2

C++
int n = nums.size();
int sum=0;
vector <int> ans; // do not reserve space in the answer

Bharti Saiinii 18-Aug-22 20:10pm
thankyou it worked.

Top Experts
Last 24hrsThis month
 Richard MacCutchan 60 OriginalGriff 35 Gerry Schmitz 25 Rick York 20 Valery Possoz 10
 OriginalGriff 1,568 Richard MacCutchan 1,318 merano99 480 Rick York 430 Dave Kreskowiak 276

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900