You will run two small labs using the statements; discuss the attributes of the programs; and write your own program using the for-or while statements. This lab is part of your discussion grade for the unit.
Write a program that squares the numbers from 1 to 10 using a for or while statement. Afterwards, print the results to the screen. Discuss how you used the statements learned in the lab to complete your program in the discussion box for this unit.
That second part is what I need help on. I had this in another thread but we got side tracked and did something else and was recommended on making another thread. So this is that thread. Here is what I have:
I honestly have no clue what I'm doing. The "<=0144" is there because NetBeans decided that was better than "<=100". This probably looks like a bunch of stupid mistakes but I have no idea what to do. Help?
Another question, given yous statement of the problem: why 100 and not 10?
Edit: anyway, what your current code is doing is multiplying all the squares of numbers between 0 and 99 inclusive, and writing out the final answer. However, since you start with zero, and 0*anything is still zero, the final answer is always going to be zero.
Matthew Brown wrote:Another question, given yous statement of the problem: why 100 and not 10?
I put 100 because that would be the square root of 10. Would the program be reading the square or the root before it squares it? If that makes any sense.
Matthew Brown wrote:anyway, what your current code is doing is multiplying all the squares of numbers between 0 and 99 inclusive, and writing out the final answer. However, since you start with zero, and 0*anything is still zero, the final answer is always going to be zero.
Whenever I run the test with it as (done =1; done <=10; done++) it just says that the square is 0.
Campbell Ritchie wrote:That is what comes from coding before you have worked out the algorithm. Get some paper a pencil and an eraser (preferably a large one ) and write down how you plan to do that on paper. When you have it down to words of one syllable, then you will find it easy to turn into code.
Well I know how to do that.
Basically it's start*start=displayed answer.
1*1=1 (which needs to be displayed)
2*2=4 (which needs to be displayed)
And keep going until Start>10
My problem is the converting over to code from that.
To me, that says "Do something with the numbers from 1 to 10".
So, I'd start with a for loop (since i know EXACTLY how many times I want my loop to run) that simply prints the numbers 1 to 10.
Once I had that working, I'd go back and see what I'm supposed to do with the numbers from 1-10...in this case, square it.
Once I had that working, I'd look at the next step...
But I have no idea how to square it.
My problem isn't necessarily writing the code, but it's not knowing what to write. I don't know how I'm supposed to put "square this if this is true". I have the formula and everything perfectly set, but I just don't know how to implement it into the system.
This is what I have so far.
You should think about the algoritm.
"Which variable will increase and multiply?"
"Which variable will show my multiply results?"
When I read your program, I understood that your program will repeat "1".
You said, in your program:
"The initial value is start=1;
If done=1 and done <=10, increases done. So, print it on screen!
Ah! Start and Done is the same thing.
Print done or start on screen."
But, you want:
"The initial value is start=0;
If start=0, start <=10, increase start.
So, print done on screen!"
you would then need to replace that one line with other code that prints the square of a number. so if you have something like:
Line 3 clearly prints the value of x. I/you can delete that one line and replace it with 1 (or 10, or 1000) line(s) that do something else. We know that on that line, x will have some value, and i want to print the square of that value.
In order to print it, i would probably need to calculate it, yes?
so i can change it to
So now i have calculated the square, so now i can print it:
So you can see, i have now replaces the original single line with two new lines. I could add in more lines if needed, and none of the rest of the code is effected.
there are caveats...if in my new code I change the value of x, it would mess up my loop, but sometimes thats what you need.
My point is, you need to separate out the various tasks in your code, and work on them one at a time. get one part working. Stick in dummy code if you need to...for example, I might have on my first go done this:
For one thing start is set to 0 so start * anything will stay zero.
Next your for loop is going to run 100 times not 10.
Also the for loop starts at 0 so no matter what you set start to initially it will be zero and stay zero.
Now even if we fix those and set start and done to 1 to begin with what's going to happen?
1 * 1 = 1
1 * 2 = 2
2 * 3 = 6
6 * 4 = 24
So you'll be calculating factorials not squares, which by the way will overflow Integers before you get to 100.
Campbell Ritchie wrote:Often, if I find somebody doesn't understand what I have told them, I worry that I have explained it badly.
All human communication fails, except by accident