Math 1281 Unit 4
Math 1281 Unit 4
Sarah Hauya
To calculate the average stock price, you can sum up all the stock prices in the array and then divide the
sum by the number of elements in the array.
float sum = 0;
sum += price;
In order to come up with the maximum stock price, we need to iterate through the array and keep track
of the maximum price encountered in the first place as follows;
maxPrice = price;
return maxPrice;
}
To determine the occurrence count of a specific price, you can iterate through the array and count the
number of times the target price appears as follows;
int count = 0;
if (price == targetPrice) {
count++;
return count;
In order to come up with the computations for the cumulative sum of stock prices, we need to iterate
through the ArrayList and keep adding each price to a running sum. Then, add the running sum to a new
ArrayList at each position as follows;
import java.util.ArrayList;
import java.util.ArrayList;
float sum = 0;
sum += price;
cumulativeSum.add(sum);
return cumulativeSum;
Conclusion
To conclude, above are explanations and programming answers in regards to the question asked in this
week's Programming Assignment for unit 4.