• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays question  RSS feed

 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was having trouble printing the product of the array I am getting runtime errors do you have any tips on what I might be doing wrong?

Here is part of my code



(Moderator edit: added code tags)
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell wrote:
for (int i= 0; i<= grades [i]; ++i)


How many times do you want your loop to iterate? If you look at this expression, you're saying to continue the loop while i is less than or equal to grades[i], which is 100. I'm guessing you only want your loop to iterate 10 times, which is the length of the array rather than a value in the array.

Inside your loop, you want to be getting values from your array, rather than just indexes. So you can't just use i. You must use grades[i].
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joel. I updated it to




now the numbers are off the final product should be 908 instead of 765 somehow it stops counting correctly around value 4 which should be 83 and somehow it is counting 32 instead

(Moderator edit: added code tags)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell wrote:
now the numbers are off the final product should be 908 instead of 765 somehow it stops counting correctly around value 4 which should be 83 and somehow it is counting 32 instead


Following the program, the product for the last iteration should be 9 times 85 yielding a result of 765.

Henry
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell, now that you've spent a little bit of time at the Ranch, you should learn how to UseCodeTags (←click on that link for all you need to know)
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will do that thank you
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell wrote:I updated it to

now the numbers are off the final product should be 908 instead of 765 somehow it stops counting correctly around value 4 which should be 83 and somehow it is counting 32 instead

Try to step through your code manually; using a pen and paper is the quickest. Build up a table like this:

In the desired column, write down the values that you wanted. In the actual column, perform the calculations that you programmed and see if the result matches the desired value. This should help you understand what you're doing wrong.

Edit: When calculating the actual results, perform each line of code as if you were the computer. Do EXACTLY what the code is doing, not what you think it should be doing. For your case, I would actually add a column to that table:
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell wrote: . . . the final product should be 908 instead of 765 . . .
Where do you get 908 from? You should find it impossible to predict the final product because you will suffer an overflow error long before you get to the 85.
Do you really mean product, rather than sum?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your looks like this:

then it will do something totally different than you expect.

Carefully think about what your program means, line by line. Translate it into English for yourself and be very careful, to understand what it is really doing. It is not multiplying all the elements in the array together.

Something else: you can create and initialize the array in a much more compact way than you did above:
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original post had
… product = 1; …
so I presumed OP meant to write 1 and hit the i key by mistake. I make that mistake all the time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!