Hello! Ive been working on this code we got for homework, we can’t change anything about it but we can just add in between two comments.
Its supposed to count the letters and then show the result as a text underneath for example
AABBAAA would be 2xA 2xB 3xA
I think I got it to count the letters but the text isn’t showing up. I don’t know what im supposed to do without changing the rest of the code. If someone could help me with this that would be really nice
Just an idea of what I have to do or change.
// Run-Length Encoding Exercise
// Please add your code to the computeRunLengthEncoding function
// It is strictly forbidden to change any other line of code.
char letters; // Input array for random letters
// Open window and initialize letter array
void setup() {
size(1000, 150);
fillArray();
}
// Initializes the letter array with a
// random length between 10 and 20 and with a
// random sequence of ‘A’ and ‘B’ letters
void fillArray() {
int sizeOfInputString = (int)random(10, 21);
letters = new char[sizeOfInputString];
for (int i=0; i<letters.length; i++)
letters[i] = random(0, 1)<0.5 ? ‘A’ : ‘B’;
}
// Whenever a key is pressed, the input array
// becomes reinitialized to test more input configurations.
void keyPressed() {
fillArray();
}
// Method for reading the global letter array and computing
// an output string that represents a run-length encoded version
// of the input data. The output string is returned.
// Example input : AAABBBBAA
// Desired output : 3xA , 4xB , 2xA
String computeRunLengthEncoding() {
String result = “”;
// Start to add your code below this line
int count = 1;
for (int i = 0; i < letters.length; i++) {
if (i < letters.length - 1 && letters[i] == letters[i + 1]) {
count++;
} else {
System.out.println(count);
System.out.println(letters[i]);
count=1;
}
}
// Add your code above this line
return result;
}
// Main drawing method that displays input and output
void draw() {
background(0, 0, 150);
textSize(40);
textAlign(CENTER);
// Draw input string:
text(new String(letters), width/2, 60);
textSize(20);
// Draw run-length encoded string:
text(computeRunLengthEncoding(), width/2, 100);
}