Информатика, вопрос задал Hoopy01 , 1 год назад

Написать программы для решения задач на языке Visual Basic (массив от 1 до 15)

16. Найти сумму из отрицательных элементов массива A(n,m)
17. Найти произведение положительных элементов массива A(n,m)
18. Найти произведение из нечетных элементов массива A(n,m)
19. Найти среднее арифметическое всех четных элементов массива A(n,m), которые больше K
20. Найти сумму всех отрицательных элементов массива A(n,m), не кратных K

Ответы на вопрос

Ответил dmitrijdenisov359
1

16. Find the sum of negative elements of the array A(n,m)

Sub SumNegativeElements()

'Declare variables

Dim A(15, 15) As Integer

Dim sum As Integer

'Initialize array

For i = 0 To 14

For j = 0 To 14

A(i, j) = i + j

Next j

Next i

'Find sum of negative elements

sum = 0

For i = 0 To 14

For j = 0 To 14

If A(i, j) < 0 Then

sum = sum + A(i, j)

End If

Next j

Next i

'Print sum

Console.WriteLine("The sum of negative elements is: " & sum)

End Sub

17. Find the product of positive elements of the array A(n,m)

Sub ProductPositiveElements()

'Declare variables

Dim A(15, 15) As Integer

Dim product As Integer

'Initialize array

For i = 0 To 14

For j = 0 To 14

A(i, j) = i + j

Next j

Next i

'Find product of positive elements

product = 1

For i = 0 To 14

For j = 0 To 14

If A(i, j) > 0 Then

product = product * A(i, j)

End If

Next j

Next i

'Print product

Console.WriteLine("The product of positive elements is: " & product)

End Sub

18. Find the product of odd elements of the array A(n,m)

Sub ProductOddElements()

'Declare variables

Dim A(15, 15) As Integer

Dim product As Integer

'Initialize array

For i = 0 To 14

For j = 0 To 14

A(i, j) = i + j

Next j

Next i

'Find product of odd elements

product = 1

For i = 0 To 14

For j = 0 To 14

If A(i, j) Mod 2 <> 0 Then

product = product * A(i, j)

End If

Next j

Next i

'Print product

Console.WriteLine("The product of odd elements is: " & product)

End Sub

19. Find the arithmetic mean of all even elements of the array A(n,m) which are greater than K

Sub ArithmeticMeanEvenElements()

'Declare variables

Dim A(15, 15) As Integer

Dim mean As Double

Dim count As Integer

'Initialize array

For i = 0 To 14

For j = 0 To 14

A(i, j) = i + j

Next j

Next i

'Find arithmetic mean of even elements which are greater than K

mean = 0

count = 0

For i = 0 To 14

For j = 0 To 14

If A(i, j) Mod 2 = 0 And A(i, j) > K Then

mean = mean + A(i, j)

count = count + 1

End If

Next j

Next i

'If no even elements are greater than K, then print "No even elements found"

If count = 0 Then

Console.WriteLine("No even elements found")

Else

'Print arithmetic mean

Console.WriteLine("The arithmetic mean of even elements greater than K is: " & mean / count)

End If

End Sub

20. Find the sum of all negative elements of the array A(n,m) that are not divisible by K

Sub SumNegativeElementsNotDivisibleByK()

'Declare variables

Dim A(15, 15) As Integer

Dim sum As Integer

'Initialize array

For i = 0 To 14

For j = 0 To 14

A(i, j) = i + j

Next j

Next i

'Find sum of negative elements that are not divisible by K

sum = 0

For i = 0 To 14

For j = 0 To 14

If A(i, j) < 0 And A(i, j) Mod K <> 0 Then

sum = sum


Hoopy01: Пишет subscription out of range
dmitrijdenisov359: Где именно?
Hoopy01: Я точь в точь переписал вашу программу и при запуске пишется эта ошибка
dmitrijdenisov359: Я вообще не шарю за VB
dmitrijdenisov359: щас, подумаю
dmitrijdenisov359: Я думаю можно попробовать вместо A(i, j) = i + j
Написать

If A(i, j) < 0 And A(i, j) Mod K <> 0 Then
sum = sum + A(i, j)
End If
dmitrijdenisov359: Хотя стоп, там же и так это было
Hoopy01: К сожалению не работает
Новые вопросы