0

I was just watching a video about how to find the mode of an array and just came across this error, this is my code

int common = 0;
    int count = 0;
    for ( int i=0; i<marksVal.length; i++){
        int tempCommon = marksVal[i];
        int tempCount = 0; 
        for (int p = 0; p<marksVal.length;p++);
            if(marksVal[p] == tempCommon);
                tempCount++;
        if (tempCount > count){
            common = tempCommon;
            count = tempCount;

The p that can't find the symbol is the second line that has a p in it

if(marksVal[p] == tempCommon);

Is it because its in a for then if statement? I didn't think that would cause this issue. Edit: As it was pointed out to me, I made a silly mistake, I just needed to remove a ; new code looks like this:

for (int p = 0; p<marksVal.length;p++)
  • 3
    Well, you have a semi-colon after the declaration of the second loop. I'd say try to replace it with brackets like the first loop and see what happen. – Nicolas Aug 18 '20 at 23:29
  • you're terminating your for loops and if statements with semi colons ie. empty expressions – Matt Coubrough Aug 18 '20 at 23:31
  • @Nicolas Thank you so much, Such a silly mistake! – anonaccount Aug 18 '20 at 23:40
  • The semicolon at the end of the `for` line says "this loop is finished, so discard any temporary variables that were set up in the loop". The `p` variable gets discarded here. So get rid of the semicolon at the end of that line. You'll also need to get rid of the semicolon at the end of the next line. – Dawood ibn Kareem Aug 18 '20 at 23:40
  • 1
    As a general rule, whenever you have a `for`, `if`, `else`, `while` or `do`, it's a good idea to use `{` and `}` to delimit the scope of the associated loop or branch. This reduces the potential for this kind of error. – Dawood ibn Kareem Aug 18 '20 at 23:42

0 Answers0