Hi guys! I have a question I have this problem in my Programming and Logic design class that I just can't seem to figure out
" Create a program that will output the sum of all the odd numbers between 1 and 99 inclusive"
I don't want the answer I would like to understand how to do it because I really need to learn this since it's basic programming and I am using Visual Logic BTW.
Thanks, for your help =)
3 replies to this topic
#1
Posted 23 March 2011 - 06:13 PM
|
|
|
#2
Posted 23 March 2011 - 06:59 PM
I haven't used Visual Logic before, but I can give you the algorithm that you could implement in that language.
You can easily do this with a for loop. Declare a variable at the beginning to hold the running sum as you progress through the loop. Let's call that variable "sum." Initialize it to zero at the start.
Next, form your for loop, beginning at 1 and ending with 99, stepping by 2 each time. Add the value of the current loop iteration to the running sum.
After the loop concludes, simply output the value of sum, and you're done.
If you're unsure about how to implement any of that in your language, look up the language's syntax for "for" loops. It should be relatively straightforward from there.
You can easily do this with a for loop. Declare a variable at the beginning to hold the running sum as you progress through the loop. Let's call that variable "sum." Initialize it to zero at the start.
Next, form your for loop, beginning at 1 and ending with 99, stepping by 2 each time. Add the value of the current loop iteration to the running sum.
After the loop concludes, simply output the value of sum, and you're done.
If you're unsure about how to implement any of that in your language, look up the language's syntax for "for" loops. It should be relatively straightforward from there.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 24 March 2011 - 08:30 AM
I think I understand most of it except for this part "Add the value of the current loop iteration to the running sum." I put sum= 0 than my for loop variable = sum initial value =1 final value = 99 and step = 2 but I am stuck in that part I tried sum = 2 or sum = sum + 2 but neither worked
#4
Posted 24 March 2011 - 11:08 AM
I can only speak for the languages that I know, but in C-style languages, you use a variable called a "loop iterator" to keep track of how many times you've been through the loop. Most people typically use the letter "i" for the loop iterator. Take a look at this example:
for ( initialization of loop iterator, test condition for entering the loop, iterator update statement ) { ... }
The initialization of the loop iterator declares the variable and initializes it at a starting value. Then, the test condition is evaluated. If it is true, the computer executes the statements within the curly braces. At the conclusion of the loop, the computer executes your update statement. This updates the value of your loop iterator based on however you write it. Then, it loops again. It evaluates your test condition to see if it is still true after the iterator was updated, and if it is, it executes the statement block again followed by the update statement at the end.
At some point, your update statement will cause the iterator to change so that the test condition will no longer evaluate to true. When this happens, your program ceases to execute the loop and proceeds with the rest of your program.
In the example above, I begin by setting i (the loop iterator) to 1 (the starting point). Then, I check to see if it is less than or equal to 99 (the end point). In the body of the loop, I take the value of i, add it to sum, and then store the result back into sum. Each time the loop is executed, it adds the value of i to sum. The update statement adds 2 to the value of i (so we only get the odd numbers). So the 2nd time my loop runs, i=3. The third time, i=5, and so on. When i=101, the test condition evaluates to false, and the program exits the loop. So we've added all the odd numbers from 1 to 99.
There will be some type of for loop structure in your language with which you can do the same exact thing as I did above, you'll just have to look up the syntax of for loops for your language.
Is that making more sense now? :)
int sum = 0;
for (int i = 1; i <= 99; i += 2) {
// the variable "i" has scope inside this loop
sum = sum + i; // add the value of "i" to sum, and store the results in sum
}
In C, you declare a for loop in this format:for ( initialization of loop iterator, test condition for entering the loop, iterator update statement ) { ... }
The initialization of the loop iterator declares the variable and initializes it at a starting value. Then, the test condition is evaluated. If it is true, the computer executes the statements within the curly braces. At the conclusion of the loop, the computer executes your update statement. This updates the value of your loop iterator based on however you write it. Then, it loops again. It evaluates your test condition to see if it is still true after the iterator was updated, and if it is, it executes the statement block again followed by the update statement at the end.
At some point, your update statement will cause the iterator to change so that the test condition will no longer evaluate to true. When this happens, your program ceases to execute the loop and proceeds with the rest of your program.
In the example above, I begin by setting i (the loop iterator) to 1 (the starting point). Then, I check to see if it is less than or equal to 99 (the end point). In the body of the loop, I take the value of i, add it to sum, and then store the result back into sum. Each time the loop is executed, it adds the value of i to sum. The update statement adds 2 to the value of i (so we only get the odd numbers). So the 2nd time my loop runs, i=3. The third time, i=5, and so on. When i=101, the test condition evaluates to false, and the program exits the loop. So we've added all the odd numbers from 1 to 99.
There will be some type of for loop structure in your language with which you can do the same exact thing as I did above, you'll just have to look up the syntax of for loops for your language.
Is that making more sense now? :)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









