0

In a one- dimensional array consisting of n real elements, calculate: The sum of elements with odd numbers.

13th Apr 2022, 11:11 AM
Gulnur
5 odpowiedzi
+ 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)
14th Apr 2022, 4:01 AM
Marco Antonio Alonso
Marco Antonio Alonso - avatar
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
13th Apr 2022, 4:36 PM
Marco Antonio Alonso
Marco Antonio Alonso - avatar
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
14th Apr 2022, 3:44 AM
Gulnur
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.
14th Apr 2022, 4:08 AM
Gulnur
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.
14th Apr 2022, 4:20 AM
Marco Antonio Alonso
Marco Antonio Alonso - avatar