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?
I don't know why Netbeans told you that - maybe you've got some strange settings turned on. 0144 is an octal integer literal. So it's actually exactly equivalent to using 100 - though I'd only expect it to be used in a context where octal was particularly relevant. 100 is obviously much easier for humans to read.
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.
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.
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.
That step by step thing helped a little Fred. Except I really don't know what I did. I just took out some signs and randomly switched stuff around until I got it to display 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
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.
Hello! If you want that the program shows all squares of number in interval 1...10. Or, the program shows:
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!"
it is important to remember that code is modular - you can usually separate things out into distinct components, and then swap them in and out with each other. So, once you have it printing the values 1-10, you should have a fairly obvious line that prints the current value.
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: