Array Practice Comments
Comments
-
-
ehm - the next FEW challenges? There's only 1 - where are the others?
-
The next few pages of challenges. (Updated sentence to be clearer.)
-
Oh, ok, so I should just go to Next Node.....
I'm like a computer, I only understand what is input in a 100% direct and unambiguous way :) -
-
int evenlength=ar.length+1; int middle=ar[(evenlength/2)-1]; int first=ar[0]; int last=ar[ar.length-1]; int sum =first+last+middle; System.out.println(sum);
-
int mid = (ar.length-1)/2; System.out.println(ar[0]+ar[mid]+ar[ar.length-1]);
-
Other output is incorrect. Is it possible to show me which one is incorrect? That way i can adjust my code accordingly. Now i'm just guessing, thx!
-
An important thing to remember when creating else-if blocks with multiple conditions: always test the more restrictive conditions first, or the less restrictive ones will match both cases and the wrong statement may be run.
-
It works perfectly, thanks for brain teasing!)
-
a little intresting
-
-
Yes, I don't understand why no answers are given. If you are unable to solve, you'll keep guessing, and if you ARE able to solve, you may have found something really clunky and ugly, and you really want to know what is a better way of doing it.
-
You can view featured answers to many problems after you get it correct. We may also allow one to use the "cheat" option to view these answers even earlier.
-
-
I didn't know that you could sort an array automatically... But the code worked none the less... my code
-
how do I print the sum?
public class Main{
static void doStuff(int[] ar){
for (i=1; i<ar.length; i++) {
if (i%2==0) {
System.out.println(sum ar);
}
}
} -
You need to use a variable to keep track of the sum, and print it at the end, after the loop.
-
int j=0;
int n=ar.length;
while(n>0){
n=n-1;
int m=ar[n];
if(m%2==0){
j=m+j;
System.out.println(j);I'm not too sure why my code isn't working? Can I get a hint?
-
@Bernard, make sure you print the sum outside the loop. Also, to make things clearer, you could use a for loop instead of a while loop.
-
-
-
im not sure on how to pull out the least and the greatest from the array. I think I have the setup correct.
-
What did you try so far? You should be able to solve it based off example discussed earlier.
-
Okay I understand how to write the code but I don't understand how it works. The if statement keeps going as long as int c is greater than zero. I still don't get how it picks the largest number.
If the largest number was in the last spot it would make sense because it keeps running till the end of the loop and as long as the if statement is true but in some scenarios that's not the case. Additionally would I just do the reverse to get the smallest?
-
Not sure what you're asking, maybe you can link to the code? When you're not sure what would happen, try walking through the code (with a specific input) with a pencil to keep track of the values at each line. You can also try the Visual Debugger.
-
Okay , I will try. I'm just confused on how the code detects the largest integer. I'll try and break it down step by step.
1. In my code the loop runs through the array from the first position(0)throughthe last position, which is represented by int c.
2. I have ar[c], c increases by 1 each time, starting from 0.
3. Then I have if(ar[c]>a) a=0
4. Then I have a=ar[c]. Which gives me the largest int but I don't understand how. How does it know to print out the largest if the code just has it running through the whole array? Thanks -
That's the basic algorithm given as an example in about programming and the following page. The Java code example is discussed in Arrays and Loops.
Basically it declares a variable (
c
in your code) that will always be equal to the largest number of all the numbers seen so far. How does it ensure this? Since if a bigger number is encountered,c
is immediately set to it. Once it's gone through all the numbers, it knows it's set to the largest value. (If it ever passed a larger number, it would have been set to it.)(It's important to realize that the loop goes through all the numbers., since it was set to only stop once it reached the end of the array.)
-
Okay I guess it's just the way Java operates. I didn't know that it would automatically be able to pick the largest number out of an array. Do the words "largest" and smallest play a role or can the int be defined with anything?
-
You can name your variables anything. Variable names never do anything, they're just named for clarity. The loop is what finds the numbers.
-
int largest=0;
int b=ar.length-1;
for(int c=0;c<=b;c=c+1){
if(ar[c]>largest){
largest=ar[c];
}
}
System.out.println(largest);Then I still don't understand how it knows what the largest number is. My loop just goes through the array one spot at a time. And simultaneously largest changes as the loop goes through the array.
-
In 2 short lines, the solution|:
my code -
@thales I you're more advanced than me, can you answer my question in the previous comment. Since @Leaneroo doesn't understand or hasn't gotten around to responding.
-
@Bernard, I emailed you a long reply 3 days ago. It's copied below, edited to use a for-each loop.
It seems you're trying to understand how the algorithm gets the largest number in an array. Please see the links above that discuss this algorithm. Here is the algorithm step-by-step:
The code to find the largest number:
int[] ar = {4,5,3,6,1}; int largest=ar[0]; for(int n: ar){ //goes through each number in order if(n>largest){ largest=n; } }
largest
is set to 4 in the second line of code.
Let's go through each iteration of the loop.//n=5 if(n>largest){ // 5 > 4 ? yes largest=n; //largest = 5 } //n=3 if(n>largest){ // 3 > 5 ? no largest=n; //largest stays at 5 } //n=6 if(n>largest){ // 6 > 5 ? yes largest=n; //largest = 6 } //n=1 if(n>largest){ // 1 > 6 ? no largest=n; //largest stays at 6 }
so now that the loop is done,
largest
is 6, the largest number in the array. -
@Learneroo thank you for the comment , I didn't receive your email with the long explanation. My apologies if my comment sounded like a slight. I appreciate all the help. Cheers
-
Thanks I understand now!!!! ;)
-
int largest = ar[0]; int smalest = ar[0]; int sum=0; for(int i=0; i < ar.length; i++){ if(ar[i] > largest){ largest = ar[i]; } if(ar[i]< smalest){ smalest= ar[i]; } } sum=largest-smalest;
System.out.println(sum);
} -
at first I misunderstood the challange and was searching for consecutive numbers like 4+5 and 2+3 but didnt realize only the indexes in array need to be consecutive 30+70 wouldnt work...
-
https://www.learneroo.com/user_answers/6874565726
Would someone take a look at my code? What I am specifically having trouble is this: I seem to not be storing the correct value for the biggestpair sum, which I have called "biggest". I've fiddled with the for loop, which I realize might seem a bit unconventional (starting at i = 2 instead of i = 0), but I'm not worried about that part. My loop seems to run to the end and compare values only near the end, so I can't seem to return the correct values for the first two test cases as the correct value is somewhere near the middle. Is there something I could be doing to better update the value for "biggest"?
-
-
This code keeps timing out. Can someone tell me if it would work if it didn't?
https://www.learneroo.com/user_answers/4065805762 -
Your piece
i=i++
should bei++
. -
I don't understand this error message.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Main.doStuff(Main.java:7)
at Main.main(Main.java:27)
my code -
It means you're trying to call an index in an array that is outside the bounds (or size) of the array. What would happen when you reach this line when you're already up to the last item?
sum = ar[i] + ar[i+1];
-
/**
Basically the idea is to compare each value for an index against
//all the values of the rest of indexes, so basically staying fixed
//on one index (i) while iterating through the next (j), an example isthat while i=0 j should have gone from 0 to ar.length, then i=1 and j would do the same, now while the iteration is occurring do a comparison for the value and keep track through a 'count' how many times it repeats, additionally as the comparison will take place multiple times we need to know which one repeats most and when it does make that variable the mode. On this code as the second iterator (j) starts always from 0 it will evaluate the value twice, however this shouldn't matter as it shouldn't affect the end result, meaning that all values will be treated equally. My suspicion was that the 'count' wasnt being handled properly, but it works for the other Outputs. However i still keep getting 4 on the second output rather than 2... I did read the hint but at least particularly for Me it is not so intuitive, i'm still thing over it. Code below: **/ int mode=0; int count=0; int maxCount=count; for (int i=0;i<=ar.length-1; i++){ for(int j=0; j<=ar.length-1; j++){ if(ar[i]==ar[j]){ count++;} }//2nd for if(count>maxCount){ maxCount=count; mode=ar[i];} } System.out.println(mode); }
-
Please update with a second hint with further insight!
-
@David, it looks like your code needs some fixes. E.g. do you reset
count
for each number...
If you want extra support and content, consider membership on learneroo for $25. -
Admin,
Thanks much for your prompt answer, i wasn't resetting the counter, now my mind is more aware for that in the future... At the end i ended up thinking inside the box {1 to 5} rather than outside {random amount of numbers} to fit to this exercise....
Thanks and best regards.
-
-
Can I get another hint on this one I've tried to create another array but I'm not sure about how to keep track of the mode. Thanks
-
@Bernard, all the grades are integers 1 to 5, any way to keep track of them? A solution would be to create an array of size 6 where the index represents the grade and the value represents that
grade's frequency. E.g. for the example
{1,2,3,1}
, you would create an array that at the end would be{0,2,1,1,0,0}
. -
I thought about this earlier but dismissed it , can you have two loops going at the same time, a loop within a loop is what I mean?
-
You can have nested loops (one loops inside another), but that's not used in the hinted-to solution, which uses two separate loops. (You can solve it another way if you prefer.)
-
So I understand that the zeros in the new array are just empty slots right? I still don't understand how you select the number that repeats the most. So far I've done...
int [] a= new int [6]; for(int b=0;b<a.length-1;b++){ int c=ar[b];
-
Your displayed code above doesn't actually do anything. The goal is to have an array at the end that contains the frequency of each number (1-5) from the original array. You can then go through
your new array to get the maximum number (like in earlier challenges). (See a solution if still stuck.)
-
int[] an = new int [6];// an ={0,0,0,0,0,0} int c=0; int d=ar.length-1; while(c<an.length-1){ d=d-1; if(c!=ar[d]){ an[c]=0; }
if(c==ar[d]){ int m=1; m=m+1; an[c]=m; } if(d==0){ d=ar.length-1; c=c+1; } } System.out.println(an);
I'm trying to see if my new array points to the mode can you tell me why I'm getting number and words when I try to print out the array? I feel like my I wrote my if statements incorrectly. Thanks
-
Java doesn't provide a built-in method for printing an Array's contents. You can either use an ArrayList of create your own method like this one:
static void printArray(int[] ar){ for(int n: ar) System.out.print(n+" "); }
-
int[] an = new int [6];// an ={0,0,0,0,0,0} int c=0; int d=ar.length-1; while(c<an.length-1){ d=d-1; if(c!=ar[d]){ an[c]=0; }
else if(c==ar[d]){ int m=1; m=m+1; an[c]=m; } if(d==0){ d=ar[ar.length-1]; c=c+1; } } for(int g: an){ System.out.print(g+" "); }
Two questions...If I have two if statements that are true at the same time how does Java respond to this? Second question since one of my variables is the index of the loop is it affected by an if statement that is within the loop. I've provided my code for reference. And if my questions aren't really relevant to helping me solve this problem, can I get a hint to what I'm doing wrong? Thanks PS my code is only meant to establish an array that will lead me to the mode , not the mode itself.
-
- Once Java reaches a true statement in an if-else-if block, it stops checking the remaining else-if statements (they're only checked if the previous statements were false, that's why it says "else if").
- Any code inside a while-loop can change the value of any variable, including any code in an if-statement.
The solution can be much simpler. Here's a partial solution with the first part solved which you can complete.
-
I understand now, I never would have thought of that , especially not knowing about the +=. Just some feedback I feel like the lessons sometimes jump from do able to extremely hard. I feel like
there should be a moderate group of lessons that prepare the user for problems like this one. Thanks again.
-
what does this error mean?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.doStuff(Main.java:9)
at Main.main(Main.java:33) -
Another solution:
https://www.learneroo.com/user_answers/1695637983 -
Hi. Can you help me?
Look at my submission (number 1.).
http://www.learneroo.com/modules/25/nodes/145/submissionsFor INPUT is shows OK, but for OTHER INPUT is shows incorrect.
Why?
-
@Lukas, the challenge now shows the complete I/O table so you can see what you got wrong.
-
thanks, I see it now.
I did think that I can turn 11 to 1 only one time.
Because there is written : "....you should "convert" ONE 11 to 1 and return a.....".My english is not perfect.
-
Dear Admin,
I believe row 7 is incorrect:
Input is:
11 11 2 11 11 4How can the correct output be 20 if you are only allowed ONCE to change 11 to 1?
Thanks
-
@David, as many 11's as necessary should be converted to 1's to prevent the sum from being exceeded.
-
In row 3, how can you change the 11 to 1 if you only know that the sum will exceed 21 after the next draw?
Does this means that you initially count it as an 11 but then, as 10 and 5 appear,
you come back and count it as a 1? I don't know the real rules of the game but sounds like cheating to me. :P
-
@Pierre, in this version of Blackjack, the Ace always has the optimal value, regardless of order (your program ensures this!)
-
I've come up with a solution that is incorrect when I run the code. However when I go into debug, the outputs are all correct. Could this be looked into please?
-
Your recent submission prints incorrect numbers such as 14 instead of 16 for the third case.
-
-
int [] cards=new int[12]; for(int a:ar){ cards[a]+=1;// might have to switch to +=a; } int z=0; for(int b=1;b<cards.length;b++){ int m=b*cards[b];
z=z+m; if(m>11&&z+m>21){ z=z+((m/b)-1)+11; } else if(m<11&&z+m>21){ z=0; } else if(m==11&&m+z>21){ m=0; z=z+1; } } System.out.println(z);
Can I get a hint to what I'm doing wrong with my code? Thanks
-
It looks like you're making it more complicated than necessary. To figure out the score, you just need the total sum and the number of elevens. You can then determine how many (If any) elevens
should be converted to ones. First make sure you can get the correct sum, and then work on adjusting the elevens.
-
Just one suggestion, As we all know there are number ways we can arrive to a solution, so it would be nice if you guys can show us the best possible solution for the question once it is answered. So that we all can learn and improve our programming skills. Thanks
-
@Pavan, some challenges show an explanation once you get a correct answer. For other challenges, a "Featured Answers" link shows up once you get the correct answer. Click it to view possible answer(s). I just added some featured answers to this module.
-
Ok. Thank you
-
Learneroo addictive!!
-
-
ps. when i solved a excercise i almost never see the 'featured answers'. Here i cant see any?
Is there any ?
-
There aren't featured answers for every question, but this module has been updated, so you can now go back and see them.
Brayden Plush
May 7, 12:46 PMI dont have the answer, help me out