How to Find Percentage of a Quiz On Processing

Hi. I am making a quiz using processing. Our teacher instructed us to create the function findPercent, which will find the percent of the quiz using the variable totalCorrect (represents the answers the user got correct on the quiz) and the total number of questions. Since the teacher is away and very busy, I have no clue what to do.[
[https://editor.p5js.org/seerhallow12247/sketches/x7a5hTh5P]

boolean findPercent(){
divide =totalCorrect/numberOfquestions;
percent = ((float)divide);

}
Sorry if I messed up on the formatting, I am very new to Processing.
Thanks

The part of the code with the boolean is the part of the code I am having trouble with. Sorry if I have been confusing.

When the function should return a float or integer number (percentage) then return type is float or int. In your case float.

Now the variable type before the function name is the return type. This is often

  • void (this function doesn’t return anything, like void draw() for example) or
  • here boolean (which returns a boolean, example would be: boolean isTheMouseOverMyButton() { ).

The function findPercent()

So in the line boolean findPercent() {

  • the function name is findPercent
  • the return type is boolean (which is not correct)

So replace boolean with float or int please.

Remark

And at the end of the function write
return percent;

Chrisir

2 Likes

When I tried to put float or int , it said unexpected token. What should I do now?

oops

did i see this wrong?

you link to the p5.js online editor,
with your sketch ( same user name )
but the code is JAVA.

-a- where that code come from? your teacher? in form of a file?
you make that account on p5.js and pasted that code?

-b- you want learn processing?


the error when you try to run the code

SyntaxError: unexpected token: identifier (sketch: line 1)

because already the first line is wrong ( language )

sorry that you lost 4 days ? for doing your homework?
but somehow that mistake slipped even 34 views on this topic??

Sorry for the late answer. I had no idea that the type of code that was supposed to be put in p5.js was not JAVA. My teacher wrote the first line as part of the example code. Here’s the line:
String myText ;
`

The part of the code that I wanted to find the answer was this part:

else{
   text("You are done!", 50,250,700,250);
   text("You got " + totalCorrect  + " correct answers.", 50,300,700,250);
text("You got"+totalCorrect%10, 50,500,700,250);
   
  }
  
  
  //---------------------------------------------------------------------------
}

Sorry, I copied and pasted the code not knowing that the editor is coding in Javascript, not JAVA. I am coding in JAVA.

this is JAVA



int numOfQuestionsTotal = 12; // number Of Questions in Total (e.g. 12)
int totalCorrect = 4;   // number of correct answers (e.g. 9)

void setup() {
  size(300, 300);
  background(0);

  float percent = findPercent( totalCorrect, numOfQuestionsTotal) ;

  println(percent + " %");
}

void draw() {
  //
}

// ----------------------------------------------------------------------------------

float findPercent( int currentValue, int BaseValue ) {

  // The percent value is computed by multiplying the numeric value of the ratio by 100.
  // https://en.wikipedia.org/wiki/Percentage#Calculations

  float ratio =  (float) currentValue / (float) BaseValue; 
  float percent = ratio * 100.0; 

  return percent;
}
//
1 Like

Sorry, but I didn’t make it clear that the percentage is calculated based on user input, not based on preprogrammed numbers. These variables:
int totalCorrect = 0; int totalWrong = 0;
have to start at 0.

only with

int totalCorrect = 0;
int totalWrong = 0;

the percent calc would be

int percent = totalCorrect * 100 / total;

and we would assume

int total = totalCorrect + totalWrong;

but when both are 0
that division does not work.

if you calculate easy way or use @Chrisir good function
anyhow you need

numOfQuestionsTotal

to do it.

1 Like

I just provide function/ you have to insert and apply to your sketch

Thank you. I’ll try to figure it out