53.9k views
5 votes
Using selection structures and at least one loop, create a flowchart that determines and displays the sum of the positive elements of a vector of numbers and the sum of the negative elements of the vector. Write a program that implements your algorithm

User Karesh A
by
8.4k points

1 Answer

2 votes

Final answer:

To create a program that determines the sum of the positive and negative elements of a vector, you can use selection structures and loops. Here's an example code in Python.

Step-by-step explanation:

To create a program that determines the sum of the positive and negative elements of a vector using selection structures and loops, you can follow these steps:

  1. Initialize two variables, sumPositive and sumNegative, to 0.
  2. Use a loop to iterate through each element in the vector.
  3. Inside the loop, check if the element is positive or negative.
  4. If the element is positive, add it to the sumPositive variable. If it is negative, add it to the negative variable.
  5. Display the values of sumPositive and sumNegative.

Here's an example code in Python:

vector = [3, -2, 5, -1, 7]
sumPositive = 0
sumNegative = 0

for element in vector:
 if element > 0:
sumPositive += element
 elif element < 0:
sumNegative += element

print('Sum of positive elements:', sumPositive)
print('Sum of negative elements:', sumNegative)

User Dondi
by
8.6k points