Array/Loop Question

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Array/Loop Question

Post by Xaos »

So I'm doing some problems in Codingbat in Java and the problem is basically "Loop through this array, if two numbers are the same, return true. Else,return false. It works, as long as the array doesn't have a number before the two paired numbers. For example, it doesn't work if the array is [1,100,100] (I'm finding for 100). Here is my current code.

Code: Select all

public boolean scores100(int[] scores) {
  for(int i=0; i >= scores.length; i++){
     for(int j=0; j >= scores.length; j++){
        if(scores[i] == 100 && scores[j] == 100){
          return true;
        }else{
         return false;
         }
     }
  }
  return false;
}
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Array/Loop Question

Post by a_bertrand »

That would be my solution if I understood right. Keep in mind you will check multiple times the same values, it could be optimized ;)

Code: Select all

public boolean scoresChecks(int[] scores)
{
  for(int i=0; i >= scores.length; i++)
     for(int j=0; j >= scores.length; j++)
        if(scores[i] == scores[j] && i != j)
          return true;
  return false;
}
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Array/Loop Question

Post by Xaos »

Code: Select all

public boolean scores100(int[] scores) {
  for(int i=0; i <= scores.length-1; i++){
     for(int j=i+1; j <= scores.length-1; j++){
        if(i != j && scores[i] == 100 && scores[j] == 100){
          return true;
        }
     }
  }
  return false;
}
Now I get an error if there are more than one 100s in the array. Ie [1,100,1,100].
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Array/Loop Question

Post by a_bertrand »

oooops I didn't checked your loop condition... it was wrong. Here you are! Check mine and let me know.

Code: Select all

public boolean scoresChecks(int[] scores)
{
  for(int i=0; i < scores.length; i++)
     for(int j=0; j < scores.length; j++)
        if(scores[i] == scores[j] && i != j)
          return true;
  return false;
}
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Array/Loop Question

Post by Xaos »

I forgot to include the part where it says the row numbers must be right next to each other. Smh. Here is my final, and correct,code.

Code: Select all

public boolean scores100(int[] scores) {
  for(int i=1; i <= scores.length-1; i++){
        if(scores[i] == 100){
          if(scores[i-1] == 100){
          return true;
          }
     }
  }
  return false;
}
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Array/Loop Question

Post by a_bertrand »

But that would check only if the number is 100? That's no checking any 2 numbers nearby
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Array/Loop Question

Post by Xaos »

a_bertrand wrote:But that would check only if the number is 100? That's no checking any 2 numbers nearby
Well the problem just asked specifically for 100. If I did want to check for any number, I could just do the same thing, but always check if i-1== i
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Array/Loop Question

Post by a_bertrand »

Then why didn't you stated it? Sorry but you was by far not clear ;)
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Array/Loop Question

Post by Xaos »

a_bertrand wrote:Then why didn't you stated it? Sorry but you was by far not clear ;)
I know :(
Post Reply

Return to “Beginner Help and Support”