This program counts the positive integer and sums up all the negative integers in an array. I took this program out from my internship examination. I find it good as it's more of array manipulations.
PS. I made a mistake during the exam as I overlook the question, I thought that I have to sum up both positive and negative integers on the other side. The Tech HR laugh on my mistake but the approach is good. I just did overlook the question hehe
These conditions will solve it. Let's assume I already have the array, a variable that contains the sum of negative integers and the count variable for the positive integers.
*If the element is greater than 0, increment the count variable by 1 *If the element is less than 0, add the element to the sum variable that holds all the sum of negative integers
That solves it.
- Create a variable sum (for negative integers)
- Create a variable count (for positive integers)
- if the element is > 0, increment count by 1
- if the element is < 0, add the element to the sum variable
That's it and Happy Coding!