MOD question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What would be the correct code when entering a number between 102 and 676 and having it find the average of all the even numbers between 1 and the supplied number using the looping construct.

Also how do I code the program to use the Escape Key to exit and the Enter Key to perform a computation?

Thanks
 
Try this:

If number MOD 2 = 0 then
result = (number + 2 ) / 2
Else
result = ( number + 1 ) / 2
End If




Edward Mogel said:
What would be the correct code when entering a number between 102 and 676
and having it find the average of all the even numbers between 1 and the
supplied number using the looping construct.
 
Thanks. I entered that into my click procedure and it still does not
perform a calculation when I click the compute button. I'm really not
sure what is wrong.

Do you have any suggestions about the escape and enter key I inquired
about in my last post.

Any help is greatly appreciated. This is my first time using VB.NET and
I'm struggling to learn how it works. Your time and assistance is
greatly appreciated.

Edward Mogel
 
Edward Mogel said:
What would be the correct code when entering a number between 102 and 676
and having it find the average of all the even numbers between 1 and the
supplied number using the looping construct.
Also how do I code the program to use the Escape Key to exit and the
Enter Key to perform a computation?

Of course, you may check your work by using the closed formula.

If x is input then the answer 2 * ( (x/2) * ( x/2 +1)).

in psuedocode:

input x
while (x was not terminated with ESC key)
if x < 102 or x > 676
report error
else
sum = 0
count = 0
for i = 2 to x
if x is even ' (use the mod function here)
sum = sum + i
count = count + 1
endif
endfor
average = sum/count
report average
endif
input x
endwhile
 
Put this in your keypress event:

If e.KeyValue = Keys.Enter Then
'-- Call Averages
Averages()
End If

Put this in your button click event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'-- Call Averages
Averages()
End Sub

Private Sub Averages()
If number MOD 2 = 0 then
result = (number + 2 ) / 2
Else
result = ( number + 1 ) / 2
End If
End Sub
 
Edward....

Uhhh, what click procedure did you enter it into? You wrote about using the
enter and escape keys not a button. And I'm curious why you chose the range
102 and 676? If it is homework you can't use Brian's code in any case. You
mentioned a loop and he gave you a formula and while his formula should work
it wasn't the assignment.

Somebody might post the answer yet and I can't speak for everybody but
typically we like to help when somebody posts "something in the way of code"
and asks how to correct it. What do you have so far? If it is a text box
and a button on a form let me suggest you're going about it the wrong way.
You wrote the user interface first and anybody can drag components out of a
toolbox. Forget the UI for now and work on the process.

Post the code to simply sum the values from 1 to "max" where max is a
variable you've assigned a value to. Just hard code the assignment of the
variable there is no need for a UI when the basic process hasn't been
completed.

When you've posted that code we can work on how to have it only sum the even
values.

Tom
 
Correction - the answer is even easier than that. The average will be x/2
+1.

So if you input 102 the answer will be 51+1 = 52.

You can handle the keyup event and see if the key that just went up was the
Enter key or the Escape key.

if it is escape, then you can do application.exit.

if it was enter then you can validate the input to be a number and that
that number is from 102 to 676. If it fails to validate output an error
message. If it validates, make your computation and output the result.
 
What if someone enters in 103 instead of 102? Your formula won't work.
(103 / 2) + 1 = 52.5
Better yet 103 \ 2 +1 = 52 or 102 \ 2 +1 = 52

Final formula:
(number \ 2 ) + 1
 
2 remarks inline

Brian said:
What if someone enters in 103 instead of 102? Your formula won't work.
(103 / 2) + 1 = 52.5

He's using int calculations so 103 / 2 = 51 not 51.5 like you assume.
Better yet 103 \ 2 +1 = 52 or 102 \ 2 +1 = 52

Final formula:
(number \ 2 ) + 1

why doing this extra if loop, the for statement has a step part which you
can use for this kind of things

Yves
 
Regarding the if embedded in the loop: The original post was titled "mod
question" -- couldn't think anywhere else to use the mod function. Maybe
that wasn't really part of the assignment.
 
Back
Top