Hello once again.

Ive been tasked with writing a program the calculates the number of legos needed to create a pyramid. I have to do this in an array for a specific level ie...level 4 requires 16 legos to complete, as well as the total number of legos to complete a pyramid with 4 levels (a 4 level pyramid requires 30 legos).

I've been able to calculate the specific level here:

``````Try

If TextBox1.Text > 50 Then
MessageBox.Show("Level number must be 50 or less")
End If

For i = 0 To 50
pyramid(i) = TextBox1.Text
Next i

For i = 0 To 50
pyramid(i) = pyramid(i) * pyramid(i)
Next i
For i = 0 To 50
TextBox2.Text = (pyramid(i) & " Legos to build this level")
Next``````

However, I have yet to figure out how to:

1. Clear textbox2.text when the FOR statement is true
2. Calculate the total number of legos needed to create the pyramid to X levels.

I've been at this for 4 days now and regardless of what I try, I cannot get it to sum. We were given notes with the following example:

``````sum = array(0)
for i = 1 to 49
sum = array(i) + sum``````

But, I cannot, for the life of me figure out how to work this into the body of the code.

I apologize of this is an obvious noob type of question but for some reason arrays just have me stumped.

Thanks,

Jason

## All 20 Replies

You are seeing this all wrong. Consider this array as a list in a column of Excel. In your case where you have only 1 column, you don't need to reference the column, so you are only controlling the rows.

I believe that you don't have the calculation correct for each level, as you are calculating the value for the specific level 50 times and then you are outputing the result 50 times. the way you've build your calculation is basically `textbox1.text * textbox1.text` and is equal for all levels of the pyramid, so I'm guessing that this is wrong. If this is an exercise about arrays then I'm guessing that you are supposed to calculate all levels of the pyramid and then display the value for the level requested.
Once you get this done, then you will be able to use the code from the notes you were given.
My suggestion would be to open Excel, emulate what you want it to do and once you've got a clear picture on what you should do then start re-writting your code.

Let us know if you need help.

Edit: If I understand this correctly level 1 should return 1, level 2 should return 4, level 3 should return 9, etc. To figure out what an array is and what you need to do go to Excel and fill column A with numbers from 0 to 50 and B should be the value for each "cell" of your array.

Thanks for the reply but now I'm more lost than I was...LOL. Atleast before I thought I had part of the program right!

The means of finding the number of legos in a level is to square it. If I dont just multiply it by itself, how do I tell it to calculate? Do I have to manually load 1^2 through 50^2 into an array? That seems rather tedious and labor intensive doesnt it...LOL.

Ive attached an image of the program to hopefully help understand how it works.

-J

I don't get it.

It seems it would take 15 legos to create a 5 level pyramid
-
- -
- - -
- - - -
- - - - -

Did you try to figure it out with Excel as I told you? It will really make it easier to understand.

I'm attaching what you should do, and my calculation to get there - I've added your calculation as well just as a contrast.
I can't give you any more hints at this point, so try to figure this out, understand what I am doing and what you should do.

Ok, perhaps I'll break the rules and give you one more hint: After you figure out the calculations for the array, read pyramid(i) with i being the number in your textbox1.

@Unhnd_Exception: You are thinking 2D and your "design" is not a pyramid but a triangle.

@adam_k: your right I was thinking 2D and you could have left it at that.

commented: "just because" :D, will leave at that. +12

Yes, I worked it out in Excel and came to the same conclusion that you did.

At any rate, thanks for your help. Its obvious to me that I'm in over my head...LOL. I just cant believe that suddenly VB just doesnt make sense to me after 8 weeks of this class.

Again, thank you. I do believe this forum and your input is invaluable to those that have 1/2 a clue.

-Jason

Assuming level 1 takes 1 lego and given that level 4 takes 16, I am thinking that each level takes that level squared bricks. The first 4 levels then would take 1+4+9+16 or 30 bricks. Thus, an array is not needed. You start with a sum of zero then create a loop from 1 to the number of levels. At each iteration you add the square of the loop index to the sum.

And don't worry too much about it not making sense. Some times it just take a while before things start to "click". It's like learning to play bridge and figuring out the bidding system. You try to remember the rules until one day they just start to make sense and you stop thinking about them. Programming is like that. At some point you stop struggling and start doing the basic stuff without deliberate thought.

Thanks, Rev. Problem is, the professor wants it in an array :s. I have 4 pages of notes and sample programs to help me and NOTHING seems to work. Maybe its time to reconsider my major...LOL.

-Jason

If you need to use an array then save each calculation inside the loop into the array using the loop variable as the array index. Try to think of the array as a book where each page can hold one value. Instead of writing a value on page 5 of the book you actually code up "book(5) = value". Instead of calling your array "book" you call it something more appropriate like "level"

``````dim level(10) as integer

level(4) = 16``````

It's not that you are not capable of doing this or figuring out the notes, but that you've started in the wrong foot and now you can't see the obvious. I'm pretty sure that you'll soon be banging your head in a wall, for all the time you've been looking into the solution.

Your exercise is pretty simple. For the 1st part your array needs to do the same calculations no matter what. So let's start with that (only the calculation part at this point) :
For each level of the pyramid the number of legos required is level * level.
Go to your

``````For i = 0 To 50
pyramid(i) = pyramid(i) * pyramid(i)
Next i``````

and 1st of all change i to level (just the name of the variable). Is it now a bit more clear what needs to follow the = ?

Then all you have to do is add "sum = 0" before the loop and "sum = sum + pyramid(i)" after line 2. If you look at it as pseudo code

``````set the sum to zero

for each level of the pyramid
number of bricks = the square of the level number
add number of bricks to sum
next``````

@Reverend Jim: The array has been alread calculated. You don't need to do any more calculations on the array. The sum can be calculated by using

``````For level = 1 to user input
add array value for level to sum
next``````

The first line was

Ive been tasked with writing a program the calculates the number of legos needed to create a pyramid.

meaning he/she has to calculate the number of bricks for each layer. No need to write a separate loop to calculate the sum.

OK, I think I have things figured out to a certain point.

So far I have this in the form load:

``````For i = 0 To 50
pyramid(i) = i * i
Next i``````

and this in the on_click

``````If TextBox1.Text > 50 Then
MessageBox.Show("Level number must be 50 or less")
Else
level = TextBox1.Text
TextBox2.Text = (pyramid(level) & " Legos Needed to complete this level")
End If``````

Now, the second half of the problem is to calculate the number of legos to create the entire pyramid. I have the code for a sum:

``````sum = array_name(0)
for i = 0 to 49
sum = array_name(i) + sum``````

But heres the catch, I have to use 2 arrays per his assignment "Fill a 50-element array to represent the number of legos on each level. Fill in a second 50-element array to represent the total number of legos used to make a pyramid for each level."

So, I would need to fill another array, total(i, right? Is it OK to say that

``Pyramid(i) = Total(i)``

? and then

``textbox3.text = whatever I decide the math is to calculate the sum?``

Thanks again.

-Jason

Take a piece of paper and make two columns of numbers. Column 1 will contain the number of bricks required to make just that level (1, 4, 9, 16, etc). Column 2 will contain the running total of column 1 (1, 5, 14, 30, etc). Once you see the two columns side by side the solution should be obvious.

Rev,

Ive done that and thats how I came to my eureka moment and figured out the first half of my problem. However, I'm just not nderstanding how to calculate the total number of legos needed to build a pyramid.

Thanks,

-Jason

Wait...hold the press...I didnt do THAT. So basically, its like a Fibanocci sequence. Might be onto something here...LOL.

Thanks, Rev.

-Jason

No, that didnt work either.

-Jason

Let's start by defining the sum. You need a running sum which means the previous sum value + the current value.
This would mean the sum of the previous level + the number of bricks for the current level (you've already figured this one out and you've got it calculated and ready).
So
step 1. Create an array
step 2. sum = sum up to previous cell + number of bricks for current level
step 3. repeat step until you calculate all 50 levels
step 4. read the value for the level you are interested in (like previously)

Simplify things, use baby steps and remember that you are learning a new way of thinking.

PS: Failure teaches more than success. Share your failed attempt and we'll see what you are thinking wrong. If you don't you are not utilizing the resource called Daniweb.