Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
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

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!
 
Share this answer
 
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.
 
Share this answer
 
Comments
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.
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.
 
Share this answer
 
Comments
Bharti Saiinii 18-Aug-22 20:12pm    
Thankyou
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)
 
Share this answer
 
v2
Change your initialization code to:
C++
int n = nums.size();
   int sum=0;
   vector <int> ans; // do not reserve space in the answer
 
Share this answer
 
Comments
Bharti Saiinii 18-Aug-22 20:10pm    
thankyou it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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