0
In a one- dimensional array consisting of n real elements, calculate: The sum of elements with odd numbers.
5 Réponses
+ 1
It works, but you can make some improvements. For example, imagine that you have to apply the logic above to another list. Then you would have to go to the function body, and change the data of the list everytime you want to check other results.
A more practical way, is to pass a list as a parameter of the function. Would be something around this:
def odd_sum(list):
total = 0
for i in range(0, len(list)):
if list[i] % 2 != 0:
total += list[i]
print(total)
mass = [3, 7, 4, 5]
if __name__ == '__main__':
odd_sum(mass)
0
I'll firstly give you hints of what you have to do.
You need a variable to store the sum, a for loop, to iterate through the array, and an if statement inside the for loop, to check if n element is odd
0
Marco Antonio Alonso
def func():
mass = [3, 7, 4,]
num = 0
for i in range(0, len(mass)):
if mass[i] % 2 != 0:
num += mass[i]
print(num)
if __name__ == '__main__':
func()
Please check if it is correct
0
Marco Antonio Alonso thank you very much, can you help more?
In a one- dimensional array consisting of n real elements, calculate: Product of elements located after the maximum element.
0
Gulnur No worries, I'm glad I could be of some help
I think the logic is similar to the other exercise. You have to enter with a maximum element, iterate through the list, and check if list[I] > list[max_element], then calculate the product inside the if statement.