Write a function solution that, given an array A of N integers,
returns the sum of the first three positive integers in that array
(counting from the left). If there are fewer than three positive
numbers then sum all of the positive numbers that are present.
Examples:
1. Given A = [4,-2,0,5,-2, 1,6], the function should return 10 (the
first three positive integers are 4, 5, 1).
2. Given A = (3,-2,0], the function should return 3. There is only one
positive number (3), so only that number is the component of the
result.
3. Given A = [-9,-4,-2,-6), the function should return O. None of the
numbers is positive.
Assume that:
• N is an integer within the range [1..1,000);
• each element of array A is an integer within the range
(-1,000,000..1,000,000).
In your solution, focus on correctness. The performance of your
solution will not be the focus of the assessment.
Answers
Answered by
15
Answer:
hii
Explanation:
pl bro get me 500 followers and mark me brainliest
Answered by
3
Answer:
#include <iostream>
#define MAX 50
using namespace std;
int main() {
int a[MAX];
int n,i,j=1,sum=0;
cout<<"\nEnter number of elements: ";
cin>>n;
cout<<"\nEnter the elements: ";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
while(j<4)
{
if(a[i]>=0)
{
sum+=a[i];
break;
}
j++;
break;
}
}
cout<<"\n"<<sum;
return 0;
}
Explanation:
- Declare an array to store the numbers and the necessary variables.
- Take the array elements as inputs from the user and store them.
- A for loop is used to check whether the array element is negative or positive.
- We keep a counter to keep the count of the three positive numbers encountered.
- If the number is greater than zero, implying that it is positive, its value is added to the sum initially set to zero.
- Then the counter is incremented by one.
- If the counter is less than 4, the loop is repeated with the next array element.
- Once the counter becomes four, the three positive numbers are added. So, the loop is terminated.
- The sum of the first three positive numbers is displayed as the output.
#SPJ3
Similar questions