- String[] names = {"Kojo", "Ama", "Randall", "Abraham", "Rockson", "Adwoa", "Akua", "Lydia", "Mohammed", "Madyou", "Tsidi", "Mary", "Simone", "Chelsea", "Billy", "Khalid", "Kendrick", "Jermaine", "Hailey", "Naima"};
- int[] ages = new int[20];
- float[] heights = new float[20];
- void setup() {
- size(1200, 600);
- background(125, 125, 125);
- generateData();
- drawUnsortedData();
- sortData();
- drawSortedData();
- }
- void generateData() {
- // Populate arrays with random data
- for (int i = 0; i < 20; i++) {
- ages[i] = int(random(15, 90)); // Random age between 15 and 90
- heights[i] = random(1.5, 2); // Random height between 1.5 and 2 meters
- }
- }
- void sortData() {
- // Apply selection sort algorithm to sort ages and heights arrays simultaneously
- for (int i = 0; i < 19; i++) {
- int minIndex = i;
- for (int j = i + 1; j < 20; j++) {
- if (ages[j] < ages[minIndex]) {
- minIndex = j;
- }
- }
- // Swap values in ages array
- int tempAge = ages[i];
- ages[i] = ages[minIndex];
- ages[minIndex] = tempAge;
- // Swap values in heights array
- float tempHeight = heights[i];
- heights[i] = heights[minIndex];
- heights[minIndex] = tempHeight;
- // Swap values in names array
- String tempName = names[i];
- names[i] = names[minIndex];
- names[minIndex] = tempName;
- }
- }
- void drawUnsortedData() {
- textAlign(CENTER, CENTER);
- textSize(12);
- // Draw unsorted data table
- fill(0);
- String unsortedHeading = "Unsorted Data";
- text(unsortedHeading, width/4, 20);
- text("Names", width/8, 40);
- text("Age", width/8*2, 40);
- text("Height", width/8*3, 40);
- for (int i = 0; i < names.length; i++) {
- text(names[i], width/8, 60 + i*20);
- text(ages[i], width/8*2, 60 + i*20);
- String formattedHeight = nf(heights[i], 0, 2);
- text(formattedHeight, width/8*3, 60 + i*20);
- }
- }
- void drawSortedData() {
- textAlign(CENTER, CENTER);
- textSize(12);
- // Draw sorted data table
- fill(0);
- String sortedHeading = "Sorted Data";
- text(sortedHeading, 3*width/4, 20);
- text("Names", 5*width/8, 40);
- text("Age", 6*width/8, 40);
- text("Height", 7*width/8, 40);
- for (int i = 0; i < names.length; i++) {
- text(names[i], 5*width/8, 60 + i*20);
- text(ages[i], 6*width/8, 60 + i*20);
- String formattedHeight = nf(heights[i], 0, 2);
- text(formattedHeight, 7*width/8, 60 + i*20);
- }
- save("Assignment6_part2.png");
- }