Is the assignment to program a game in Excel? If it is, it doesn't necessarily need to have graphics. You can code up a board game in it. I know I've seen Puerto Rico in Excel.
Is the assignment to program a game in Excel? If it is, it doesn't necessarily need to have graphics. You can code up a board game in it. I know I've seen Puerto Rico in Excel.
I took that very class a few years ago. OPMA 413 if I remember correctly. Let me see if I can locate my project. We made a hockey pool that pulled stats through a web query. It was pretty simple, but we had solid code and it looked nice so we ended up getting a good mark.
Feel free to go to Cam for ideas and help. He was one of the best instructors I had at U of C.
I took that very class a few years ago. OPMA 413 if I remember correctly. Let me see if I can locate my project. We made a hockey pool that pulled stats through a web query. It was pretty simple, but we had solid code and it looked nice so we ended up getting a good mark.
Feel free to go to Cam for ideas and help. He was one of the best instructors I had at U of C.
That'd be great to see.
And yeah he's very approachable and helpful for sure.
I'm attempting to make a code for the following situation: "Write a subroutine that requests the user to enter a negative even integer using an input box. The subroutine needs to sum all the even integers between one hundred and twenty-one (121) and the inputted number and display the result in a message box. Your message box must let the user know what they are seeing and must include the number entered into the input box as part of the message. This routine must Include error checking that validates the initial inputted number as negative, as odd, and as an integer. You may not use the spreadsheet to accomplish any aspect of the task except to hold the form control button that invokes the subroutine."
Here's what I have so far:
Sub enternumber()
Dim mynum As Integer, Sum As Double, i As Double, isValid As Boolean
mynum = InputBox("Please enter a negative even integer")
Do Until isValid = True
If mynum >= 0 Then
MsgBox ("Please enter a negative even integer")
isValid = False
mynum = InputBox("Please enter a negative even integer")
Else
isValid = True
End If
Loop
Sum = 0
For i = mynum + 1 To 0 Step 2
Sum = Sum + i
Next
MsgBox ("The sum is " & Sum)
End Sub
Last edited by flamesrule_kipper34; 10-15-2010 at 06:44 PM.
Well first off - you should divide the inputted number by 2 and check to see that it's even. If there's a remainder then you know it's an odd number. This will also guarantee it's an integer since there will be no precision. Then make it another error check on it's own. You should display error messages for the error they are for.
second - In your second loop, don't inc by 2, do it by one and just divide by 2. If there's a remainder then don't add the number to sum. Once you're out of you second loop, add sum to my num and display them.
Your program doesn't check to see if the integer entered is odd or not, only if it's negative. That might lose you some marks. The way the question is worded is also kind of hard to understand. If I've got it right, it wants you to sum all of the even integers between the (odd, negative) number entered and 121?
I'm not sure how this language works but it seems to me that if you want to do it the way you're doing it, all you want to do is change the for loop to:
For i = mynum + 1 To 121 Step 2
Also, why are sum and i double? They're both always going to be integers. Sum could potentially be a big number, though, so you might want to use a different type (like long) if that exists.
The description says to sum all *even* numbers from 121 to your number (a negative, even integer), so you've got it backwards.
Quote:
The subroutine needs to sum all the even integers between one hundred and twenty-one (121) and the inputted number and display the result in a message box.
Code:
For i = 120 To mynum Step -2
I don't see the point of starting at 121 if we're using even numbers all the way. It's also fine to start at mynum and go up to 121, but assignments like this are known for their pedantry. It's probably safer (from an academic perspective) to check the starting number and adjust it down if it's odd so it doesn't break your loop. Something like:
Code:
Start = 121
If Start Mod 2 <> 0 Then
Start = Start - 1
End If
For i = Start To mynum Step -2
...
The argument being "your code doesn't work if I give you a different starting number, or ask the user for a starting number", etc. I always encountered silly technicalities like that.
There's no need to add 1 to mynum to start the loop if you've already verified that a) it's negative, and b) it's even. You've already got negative numbers, so you should be able to modify your data entry check to account for non even numbers. I don't know if this is syntactically correct, but you need the modulus operator.
Code:
If mynum >= 0 Or mynum Mod 2 <> 0 Then
Essentially, reject the input if it's a) non-negative or b) the remainder when dividing by two is non-zero (i.e. it's an odd number).
Last edited by Bob; 10-16-2010 at 02:29 PM.
The Following User Says Thank You to Bob For This Useful Post: