Write a program using Processing that will do the following things: Have at mini
ID: 3921414 • Letter: W
Question
Write a program using Processing that will do the following things:
Have at minimum 2 global variables
Print to the terminal a message and the value of the variable in the same line
Have a function that you send parameters to
Use at least one control structure
Use the keyPressed() event function
Problem: Every time a key on the keyboard is pressed, except for the special keys, you are going to track the number of keys pressed and if a vowel (a, e, i, o, u) was pressed. Then output the result to the terminal to tell how many keys were pressed and how many were vowels after a new key is entered. You can use all lowercase letters for your inputs.
Using the program Processing.
This is the code I have so far, but the terminal keeps saying the output is zero.
char x;
int NumKeys=0; //Counters.
int NumVowels=0;
void setup(){
noLoop(); //Stops infinite loop of the draw function.
}
void draw(){
}
void keyPressed(){
if(x=='b'||x=='c'||x=='d'||x=='f'||x=='g'||x=='h'||x=='j'||x=='k'||x=='l'||x=='m'||x=='n'||x=='p'||x=='q'
||x=='r'||x=='s'||x=='t'||x=='v'||x=='w'||x=='x'||x=='y'||x=='z'){
NumKeys++;
}
else if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'){
NumKeys++;
NumVowels++;
}
println("Total keys pressed is "+NumKeys);
println("Vowel keys pressed is "+NumVowels);
}
Explanation / Answer
You need to change the code something like
#include <stdio.h>
char x;
int NumKeys=0; //Counters.
int NumVowels=0;
void keyPressed(){
if(x=='b'||x=='c'||x=='d'||x=='f'||x=='g'||x=='h'||x=='j'||x=='k'||x=='l'||x=='m'||x=='n'||x=='p'||x=='q'
||x=='r'||x=='s'||x=='t'||x=='v'||x=='w'||x=='x'||x=='y'||x=='z'){
NumKeys++;
}
else if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'){
NumKeys++;
NumVowels++;
}
printf("Total keys pressed is %d"+NumKeys);
printf("Vowel keys pressed is %d"+NumVowels);
}
void setup(int limit){
int i=0;
while(1){
x=getchar();
keyPressed();
i++;
if(i==limit){
break;
}
}
//noLoop(); //Stops infinite loop of the draw function.
}
int main(void) {
// your code goes here
int limit=10;
setup(limit);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.