- ArrayList<Integer> temperatures = new ArrayList<Integer>(); // Dynamic array to store temperatures
- ArrayList<Integer> occurrences = new ArrayList<Integer>(); // Dynamic array to store occurrences of temperatures (4 to 20)
- int sum = 0; // Variable to store the sum of temperatures
- int cellWidth = 100; // Width of each cell in the table
- int cellHeight = 40; // Height of each cell in the table
- void setup() {
- size(800, 600);
- int sizeOfArray = 10; // Define the size of the array (change as needed)
- // Initialize occurrences ArrayList with zeros
- for (int i = 0; i <= 17; i++) {
- occurrences.add(0);
- }
- // Populate the array with random integers between 4 and 20
- for (int i = 0; i < sizeOfArray; i++) {
- int temp = (int) random(4, 21); // Generate random integer between 4 and 20
- temperatures.add(temp); // Add temperature to the ArrayList
- sum += temp; // Calculate sum
- occurrences.set(temp - 4, occurrences.get(temp - 4) + 1); // Update occurrences array
- }
- // Display the table on the canvas
- background(255);
- fill(0);
- textAlign(CENTER, CENTER);
- // Draw table headers
- text("Temperature", width/4 - cellWidth/2, 50);
- text("Occurrences", 3*width/4 - cellWidth/2, 50);
- // Draw the temperatures and occurrences in the table
- for (int temp = 4; temp <= 20; temp++) {
- int rowY = 100 + (temp - 4) * cellHeight;
- text(temp, width/4 - cellWidth/4, rowY);
- text(occurrences.get(temp - 4), 3*width/4 - cellWidth/4, rowY);
- }
- // Draw horizontal lines
- for (int temp = 4; temp <= 20; temp++) {
- int rowY = 100 + (temp - 4) * cellHeight;
- line(width/4 - cellWidth/2, rowY + cellHeight/2, width/4 + cellWidth/2, rowY + cellHeight/2);
- line(3*width/4 - cellWidth/2, rowY + cellHeight/2, 3*width/4 + cellWidth/2, rowY + cellHeight/2);
- }
- // Draw vertical lines
- line(width/2, 50, width/2, height);
- line(width/4 - cellWidth/2, 50, width/4 - cellWidth/2, height);
- line(3*width/4 - cellWidth/2, 50, 3*width/4 - cellWidth/2, height);
- // Display the sum of temperatures
- text("Sum of temperatures: " + sum, width/2, height - 50);
- save("Activity19.png");
- }