Facebook
From Adjowa Asamoah, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 155
  1. String[] names = {"Kojo", "Ama", "Randall", "Abraham", "Rockson", "Adwoa", "Akua", "Lydia", "Mohammed", "Madyou", "Tsidi", "Mary", "Simone", "Chelsea", "Billy", "Khalid", "Kendrick", "Jermaine", "Hailey", "Naima"};
  2. int[] ages = new int[20];
  3. float[] heights = new float[20];
  4.  
  5. void setup() {
  6.   size(1200, 600);
  7.   background(125, 125, 125);
  8.  
  9.   generateData();
  10.   drawUnsortedData();
  11.  
  12.   sortData();
  13.   drawSortedData();
  14. }
  15.  
  16. void generateData() {
  17.   // Populate arrays with random data
  18.   for (int i = 0; i < 20; i++) {
  19.     ages[i] = int(random(15, 90)); // Random age between 15 and 90
  20.     heights[i] = random(1.5, 2); // Random height between 1.5 and 2 meters
  21.   }
  22. }
  23.  
  24. void sortData() {
  25.   // Apply selection sort algorithm to sort ages and heights arrays simultaneously
  26.   for (int i = 0; i < 19; i++) {
  27.     int minIndex = i;
  28.     for (int j = i + 1; j < 20; j++) {
  29.       if (ages[j] < ages[minIndex]) {
  30.         minIndex = j;
  31.       }
  32.     }
  33.     // Swap values in ages array
  34.     int tempAge = ages[i];
  35.     ages[i] = ages[minIndex];
  36.     ages[minIndex] = tempAge;
  37.    
  38.     // Swap values in heights array
  39.     float tempHeight = heights[i];
  40.     heights[i] = heights[minIndex];
  41.     heights[minIndex] = tempHeight;
  42.    
  43.     // Swap values in names array
  44.     String tempName = names[i];
  45.     names[i] = names[minIndex];
  46.     names[minIndex] = tempName;
  47.   }
  48. }
  49.  
  50. void drawUnsortedData() {
  51.   textAlign(CENTER, CENTER);
  52.   textSize(12);
  53.  
  54.   // Draw unsorted data table
  55.   fill(0);
  56.   String unsortedHeading = "Unsorted Data";
  57.   text(unsortedHeading, width/4, 20);
  58.   text("Names", width/8, 40);
  59.   text("Age", width/8*2, 40);
  60.   text("Height", width/8*3, 40);
  61.  
  62.   for (int i = 0; i < names.length; i++) {
  63.     text(names[i], width/8, 60 + i*20);
  64.     text(ages[i], width/8*2, 60 + i*20);
  65.     String formattedHeight = nf(heights[i], 0, 2);
  66.     text(formattedHeight, width/8*3, 60 + i*20);
  67.   }
  68. }
  69.  
  70. void drawSortedData() {
  71.   textAlign(CENTER, CENTER);
  72.   textSize(12);
  73.  
  74.   // Draw sorted data table
  75.   fill(0);
  76.   String sortedHeading = "Sorted Data";
  77.   text(sortedHeading, 3*width/4, 20);
  78.   text("Names", 5*width/8, 40);
  79.   text("Age", 6*width/8, 40);
  80.   text("Height", 7*width/8, 40);
  81.  
  82.   for (int i = 0; i < names.length; i++) {
  83.     text(names[i], 5*width/8, 60 + i*20);
  84.     text(ages[i], 6*width/8, 60 + i*20);
  85.     String formattedHeight = nf(heights[i], 0, 2);
  86.     text(formattedHeight, 7*width/8, 60 + i*20);
  87.   }
  88.  
  89.   save("Assignment6_part2.png");
  90. }
  91.  
  92.