• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

First question. I am generating an error after the program executes that states the following: It is referring to the line after the else statement runs. "alpha[i] = 3 * i;"
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
at Alpha.main(Alpha.java:23)

I am not entirely sure why that is. I've shown the output below.

Second, I am not entirely sure I have satisfied all the requirements, but do somewhat. Can someone please proof and acknowledge whether or not I am missing an aspect of the requirements?

The instructions are shown below...I appreciate any and all comments.....



Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. S Output the array so that 10 elements per line are printed.

To solve this programming problem, you’ll need an array with a specific number of elements, an accumulator variable, a for loop, and if-else statement.

Create a double array to hold 50 elements.

Create an accumulator variable with an initial value of 0. This will be used to track the number of elements printed on a single line.

Use a for loop to multiply the first 25 control variable values by themselves, and then assign the result to an array element in every iteration of the loop. Inside the loop use an if statement to check if the current value of the control variable is less than 25. If this is true, assign the square of the control variable value to the current array element.

Below is an example assuming the control variable is named i, and it starts from 0.

if(i<25)
alpha[i] = i*i;

Next use an else statement to triple the remaining control variable values over 25 like this:

else
alpha[i] = i*i*i;

Print out each element value on a single line using System.out.print. This needs to be written only once since you're in a loop

Increment the accumulator variable by 1

Create an if statement that checks if the accumulator is greater than 9. If this is true, reset the accumulator to 0, and then print a new line using System.out.println








0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
at TestClass.main(TestClass.java:20)
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an array contains 50 elements, they are elements numbered from 0 to 49. But the error message says your code tried to access element number 50, which doesn't exist.

So you should look at the line of code which threw that exception to see why it's using 50 as an array index.

(Hint: there's a standard Java idiom for processing the elements of an array one at a time. You may already have seen it in the course materials you're using, but you haven't reproduced it faithfully.)
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made the change. And the exception is taken care of. Question is whether or not that is what you were referring too. Aside from that, does it fulfill the requirements.





0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50

 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that wasn't what I was referring to. And anyway your code doesn't satisfy this requirement:

Use a for loop to multiply the first 25 control variable values by themselves, and then assign the result to an array element in every iteration of the loop.


It only does that for the numbers from 0 to 23, which aren't the first 25. And then there's this requirement:

Next use an else statement to triple the remaining control variable values over 25 like this:

else
alpha[i] = i*i*i;


That doesn't actually triple the control variable; your code does actually triple it but on the other hand for some unknown reason it assigns the value to some other array element.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright. I see a couple of things I can change. Then I will see if I can sort out what I am overlooking....Thanks!!

I suspect I will post the modifications later for scrutiny....

 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an updated version. It seems to me that I have fulfilled the requirements except for maybe one thing. That would apply to the final line in the instructions that say:

Create an if statement that checks if the accumulator is greater than 9. If this is true, reset the accumulator to 0, and then print a new line using System.out.println.

Aside from that I don't know at this point. Can someone scrutinize for me....



 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it looked to me like you were supposed to use a separate "accumulator" variable, whereas you've hijacked the index variable to do that work. But the output comes out right, doesn't it?

As for your correction of the loop code:



I mentioned a standard Java idiom for going through an array or some other indexed object, and here's what it looks like:



That's equivalent to your version, but you should use it as opposed to what you came up with because that's how Java programmers everywhere do it, and it will be recognized immediately. Your version... it does the same thing but a programmer would have to inspect it suspiciously before accepting it as correct.

 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . a standard Java idiom . . . that's how Java programmers everywhere do it, and it will be recognized immediately. . . .
And because it always works for array of any size.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does. I'm sure some of my confusion stems from the fact that the program works fine as it is. Isn't it cleaner and easier to do it this way to avoid unnecessary clutter if you will. For the sake of the assignment, I suspect I have to satisfy the requirements, so I will have to change it...I appreciate all the dialog...Thanks!
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funny thing too. Our text book shows code as I did it when creating those statements.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my revision in an effort to comply with the assignment.

My question is can I combine the two System.out.print statements into one at the end of the program leaving the output as it currently is? I have read pretty heavily and cannot find an answer to this question. Any guidance with this would be great.....Thanks!




0.00000 1.00000 1.41420 1.73210 2.00000 2.23610 2.44950 2.64580 2.82840 3.00000
3.16230 3.31660 3.46410 3.60560 3.74170 3.87300 4.00000 4.12310 4.24260 4.35890
4.47210 4.58260 4.69040 4.79580 4.89900 15625 17576 19683 21952 24389
27000 29791 32768 35937 39304 42875 46656 50653 54872 59319
64000 68921 74088 79507 85184 91125 97336 103823 110592 117649

----operation complete.
 
Carey Brown
Bartender
Posts: 3003
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:
This program declares an array "alpha" of 50 elements of type "double". And initializes the array so that the
first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three
times the index variable. Output the array so that 10 elements per line are printed.

Your program does not do what this comment says it should do. First off, "i * i * i" is not three times the index variable. Also, you created a variable "product" when you are asked to fill in the array values. There is no mention of square root in the instructions.

To be clearer, you could create two loops, the first to fill in the alpha array (and nothing more), and a second one that only prints what is in the alpha array.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that it is not following the instructions to the tee. While working with the assignment I added the sqrt code to gain insight on how to concatenate two totally different strings of information and produce results displayed as the information I posted is displayed. Something I have a tendency to do is create scenarios I don't fully understand in an effort to learn something more than just what an assignment might be. I have a finished product not posted that follows the instructions. Thank you for your time!
 
Carey Brown
Bartender
Posts: 3003
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:I have a finished product not posted that follows the instructions. Thank you for your time!

Could you post your completed assignment?
It's hard for us to help when the stated requirements differ from your intentions.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me preface by saying, in my mind the complete explanation of requirements is a bit confusing. I will try to list the statements that seem to contradict one another. It could very well just be me...Anyway!
The first paragraph states to square the first 25 elements of the index variable, and the last 25 elements are three times the index variable. Scroll down the page to where it says: Next use an else statement to triple the remaining control variables over 25 like this:

else

alpha[i] = i * i * i; This is not 3 times the index variable, is it not?

Keep in mind that the first example: if(i<25)
alpha[i] = i * i; squares the value of the first 25 elements or indexes.

First of all, I couldn't get the program to produce the results using the alpha[i]. I am asked to create the accumulator, so I did that and used it to produce the results. Whether or not I will loose points for that is yet to be seen.

Through all of this, as I stated, it could just be a misplaced interpretation on my part. B




I appreciate any and all feedback regardless if it is a lambasting......I am here to learn.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0 1 4 16 25 36 49 64 81
100 121 144 169 196 225 256 289 324 361
400 441 484 529 576 15625 17576 19683 21952 24389
27000 29791 32768 35937 39304 42875 46656 50653 54872 59319
64000 68921 74088 79507 85184 91125 97336 103823 110592 117649

----operation complete.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:
alpha[i] = i * i * i; This is not 3 times the index variable, is it not?

If we're using standard algebraic notation, "3 times x" is written as 3 * x or simply 3x.

x * x * x is NOT equal to 3x, it's equal to x³

3x = x + x + x
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for pointing that out. This is about an error in instruction. Thank you for your time....
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sooner you get comfortable with the zero-based idiom that Paul pointed out, the better for you and others reading your code.

Using for (int i = 0; i <= limit; i++) not only invites confusion but it also opens the door for bugs. The inclination to use <= instead of < is strong if you're coming from a language that normally uses 1-based indices, like Pascal. Java, however unfortunate or inconvenient it may seem, uses 0-based indices and that's generally consistent across the board, especially in the Collections API. So if you're going to be programming in Java, it's in your best interest for you to get with the program already

BTW, your approach to "fixing" the problem with the indices while still using <= instead of <, namely by doing things like alpha[i - 1] instead of just leaving it as alpha[i] is like walking around with your body leaning a few inches over to one side because you've buttoned your shirt one button out of alignment. It's awkward and it takes more effort than it would to just go back and do it the normal way.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The text I read from suggests using the -1 to prevent the exception that was generated. I read the entire chapter and either overlooked what Paul spoke, which I don't think was the case. Or what Paul spoke of was not in the chapter. I mention this because I am not sure of an alternative to writing this. I tried quite a few other ways of producing the desired result without using -1, but was unable to do so. Please share the preferred way.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scratch that. I got ya...I wasn't paying attention to the < without the =....Thank you..
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:Scratch that. I got ya...I wasn't paying attention to the < without the =....Thank you..


Such a small difference; such an incomprehensible problem if you don't notice it. But programming is always like that. Which is why Homer Simpson invented the word "D'oh!" for our use as programmers.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that the truth. Life surely is grand!!

 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! Adding to the code below I am to initialize the array with 10 values that add up to 26. The instructions follow. I am so lost with this part of it. After reading, I am thinking a sequential search or linear search might be how I approach this, but I am not clear. If someone could perhaps, guide me in the right direction it would be great.....Thanks in advance!




Practice Problem 5-5 Description: Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. S Output the array so that 10 elements per line are printed.

Practice Problem 5-5 Guidelines: The tricky part of this problem is initializing the array with 10 values that add up to 26 miles. Assume you were to add up 3 values to make up 10 miles, the array can be initialized with the values 1, 2, and 7. Notice that the second value (2) is a small increment to the first value (1). Also notice that when you add (1 and 2), the result (3) never exceeds the target value (10). Calculating the difference between 10 and 3 produces 7 (the third value).







 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your current code indentation is terrible. Showing how it should be looking after you fix it. Try to compare yours and mine, and see where you did incorrect.
Before we move forward, could you please explain exactly what you code does at:
1. line 9
2. line 24
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is suppose to be an accumulator, but I don't think it is done entirely correct. It is suppose to cause the program to display 10 items on each line except the first, which is 9.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:
Well, comment says something different than you just explained.
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay....Is the statement correct?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Rothgeb wrote:Okay....Is the statement correct?
Don't know, one or another is wrong, maybe both. Anyway, you failed to do correctly first tasks.
Practice Problem 5-5 Description: Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. S Output the array so that 10 elements per line are printed.

1. Write a program that declares an array "alpha" of 50 elements of type "double".
2. Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable.
3. Output the array so that 10 elements per line are printed.

Basically you managed to do correctly only task 1 so far. I'd suggest for you to remove code from lines 1, 7, 9, 10, 12, 24, 25, 26, 27. So you would end up with:
Now you need to change something on 2 lines, so you'd get task 2 done. What is that?

How you access array element at certain position? Here is example:
 
Mike Rothgeb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better?

 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I told you - you haven't got task 2 right. Why you're trying to do task 3? Delete everything from lines 24, 25, 26, 27 - you don't need them now. Work on the rest of the code.
  • Read task 2 once again, what it says?
  • For your own curiosity print any element in your array and see what value is there
  •  
    Mike Rothgeb
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This answers task two. Displays the square of the first 25 and three times the last 25.


     
    Mike Rothgeb
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Task three is to output 10 items per line. So the next post will do that.
     
    Mike Rothgeb
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Liutauras Vilda
    Marshal
    Posts: 4657
    319
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No!

    1. Read out loud:
    Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable.

    2. Read out loud:
    What lines 15 and 18 do?
     
    Carey Brown
    Bartender
    Posts: 3003
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Rothgeb wrote:
    1. Write a program that declares an array "alpha" of 50 elements of type "double".
    2. Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable.
    3. Output the array so that 10 elements per line are printed.

    You are still jumping ahead to 3 without doing 2 correctly.

    You introduced a "product" variable that is not necessary and you are missing an accumulator required for step 3.
     
    Mike Rothgeb
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay! Now we should be here.

    The tricky part of this problem is initializing the array with 10 values that add up to 26 miles. Assume you were to add up 3 values to make up 10 miles, the array can be initialized with the values 1, 2, and 7. Notice that the second value (2) is a small increment to the first value (1). Also notice that when you add (1 and 2), the result (3) never exceeds the target value (10). Calculating the difference between 10 and 3 produces 7 (the third value).
     
    Mike Rothgeb
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm sorry. Then I am not understanding the accumulator. I have seen examples done similar to this. And I realize they might not be right. I have also read much more in depth information about accumulators, so it is safe to say I am lost with that as well.
     
    Carey Brown
    Bartender
    Posts: 3003
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Rothgeb wrote:I'm sorry. Then I am not understanding the accumulator. I have seen examples done similar to this. And I realize they might not be right. I have also read much more in depth information about accumulators, so it is safe to say I am lost with that as well.

    Well, it doesn't have to be called "accumulator". In fact, I think that's a misleading name. You might just call it "count" and increment it every time you print a number and test to see if it's a multiple of 10 for deciding when to output a new-line.
     
    Liutauras Vilda
    Marshal
    Posts: 4657
    319
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to read your tasks carefully mate. Programming is not an easy discipline. You can't get any program right if you do not follow instructions.
    My dad always used to say, if you don't understand instructions from the very first time, read them twice, if that doesn't help, read until you get them, even 100 times if needed, otherwise to start doing something is basically waste of time.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!