Computer Applications
Write a code segment to compute the sum of all positive real numbers stored in the following array.
double numb[] = new double[50];
Java Arrays
18 Likes
Answer
double sum = 0;
for (int i = 0; i < 50; i++) {
if (numb[i] > 0) {
sum += numb[i];
}
}
System.out.println("Sum of positive real numbers = " + sum);
Answered By
14 Likes
Related Questions
Given the following declarations:
final int SIZE = 20; char[] name = new char[SIZE];
i. Write an assignment statement that stores 'D' into the first element of the array name.
ii. Write an output statement that prints the value of the tenth element of the array name.
iii. Write a for statement that fills the array name with spaces.
Suppose x is an array of type int[] with 50 elements. Write a code segment that will count and print the frequency of number 42 in the array.
A student wrote the following code segment, intending to print 11 22 33 44:
int arr[] = {11, 22, 33, 44}; for (int i = 1; i <= 4; i++) System.out.println(arr[i]);
However, the program crashed with a run-time error. Can you explain the reason for this?
Write a code segment that finds the largest integer in this two-dimensional array.
int data[][] = new int[5][5];