|
-
Oct 29th, 2008, 08:04 PM
#1
Thread Starter
New Member
If statements drivin me crazy!
i need this program to display the 2 highest marks, but it only displays the highest mark twice, please help!
Code:
Option Strict On
Option Explicit On
Public Class Form1
Dim highscore1, highscore2 As Double
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim score As Double
score = CDbl(txtInput.Text)
If score > highscore1 Then
highscore1 = score
If score > highscore2 Then
highscore2 = score
Else
If highscore1 - score < 0 Then
highscore1 = score
Else
If highscore2 - score < 0 Then
highscore2 = score
End If
End If
End If
End If
txtInput.Text = "0"
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
ListBox1.Items.Add("The two highscores are " & highscore1 & " and " & highscore2 & ".")
End Sub
End Class
-
Oct 29th, 2008, 08:20 PM
#2
Re: If statements drivin me crazy!
You don't fix code just by reading it. You need to learn how to debug your code. Start by placing a breakpoint at the top of the btnAdd_Click method. You do that by either licking in the left-hand margin of the code window or else clicking the line and pressing F9. Now, when you run your project, execution will break at that line. You can now step through your code one line at a time by pressing F10. At every step you can test the values of all your variables and properties, evaluate expressions and many other things. What you need to do is check that all your variables contain the values you expect, then determine what you think will happen when you step. You then step and see if what you expected actually happened. As soon as that is not the case you have found a bug.
Having said all that, you shouldn't even be writing code if you don't have a working algorithm. How can you write code that works if you don't even know exactly what it's supposed to do? It might be that you can debug your code and it will run through and do exactly what you think it should and still end up with the wrong answer. In that case you have a logic error, not a code bug.
Do you actually have a series of steps that you can run through on paper and end up with the correct answer? If not then how can you possibly hope to get code that works? If you haven't confirmed your logic then you should be writing any code at all.
-
Oct 29th, 2008, 08:36 PM
#3
Thread Starter
New Member
Re: If statements drivin me crazy!
wow thanx for that, thanks for telling me nothin to help me, i pretty much got the code all worked out, and i have spent like an hour and a half on it, maybe i am not the best coder but im just starting out. all i asked for was some help, not a lecture.
-
Oct 29th, 2008, 08:45 PM
#4
Re: If statements drivin me crazy!
 Originally Posted by Jester-666
wow thanx for that, thanks for telling me nothin to help me, i pretty much got the code all worked out, and i have spent like an hour and a half on it, maybe i am not the best coder but im just starting out. all i asked for was some help, not a lecture.
And he's given you more than just the answer, he's given you a "coding problem solving method" which as you master it will become a more confident programmer, not to mention a quicker one too.
-
Oct 29th, 2008, 08:53 PM
#5
Re: If statements drivin me crazy!
 Originally Posted by Jester-666
wow thanx for that, thanks for telling me nothin to help me, i pretty much got the code all worked out, and i have spent like an hour and a half on it, maybe i am not the best coder but im just starting out. all i asked for was some help, not a lecture.
I could give you the answer but then what would you learn? I post here to help people become better developers, not to solve their problems for them. If you follow the advice I provided then you'll either find the issue or find that your logic is flawed. Either way, you're better off. You can sit there and wait for someone to hand you a fish if you want but I suggest that you put my fishing lesson to use and then you will improve your skills. That's the best help you can receive.
-
Oct 29th, 2008, 09:33 PM
#6
Re: If statements drivin me crazy!
following his lead, i won't give you the answer either, but i will ask you to consider this:
how do you store the two highest scores? If you find a score higher than a previous high score, the old high score is automatically going to be the 2nd highest. Think about it and then apply it to your current code. As jmclhinley said, you need to go over your code logic. If your logic was correctly implemented then obviously it would work. It doesn't so therefore you've not implemented it correctly. I suggest also you step through it line by line as he suggested and watch the values assigned to highscore 1 and 2 and when they are assigned.
-
Oct 29th, 2008, 10:42 PM
#7
Re: If statements drivin me crazy!
This is a forum, not a school. Most schools are understaffed btw so there are enough venues for patronizing. Besides, such an algorithm is more than simply reassigning ex-highest scores. Every new value has to be tested on both highest marks so far. A beginner has to learn by example, sorry to disagree.
Code:
If score > highscore1 Then
'there is a catch, try entering 3,5,7, and 1 - it won't work correctly
'so uncomment the following and try to figure out why is it working
'If highscore1 > highscore2 Then
' highscore2 = highscore1
'End If
'if score is bigger than highscore1, assign that bigger value to highscore1
highscore1 = score
Else 'the Else part gets executed only if score is not bigger than highscore1
'so getting here means that score is not bigger than highscore1
'but we still want to check if score is bigger than highscore2
If score > highscore2 Then
'if it is, assign that bigger value to highscore2
highscore2 = score
End If
End If
-
Oct 29th, 2008, 10:59 PM
#8
Re: If statements drivin me crazy!
 Originally Posted by Half
A beginner has to learn by example, sorry to disagree.
And by what example does a beginner learn to use the debugger? I'm not saying you shouldn't provide a code solution, in this case or in others, but most people will simply copy and paste the code without ever really developing any sort of understanding of what it does and how they could have arrived at it themselves. The sooner a developer learns to use the debugger the better for them, so they can start solving their own problems. Every time you solve a problem for them you deny them a chance to learn how to solve the next one for themselves.
-
Oct 29th, 2008, 11:40 PM
#9
Re: If statements drivin me crazy!
 Originally Posted by Half
This is a forum, not a school. Most schools are understaffed btw so there are enough venues for patronizing. Besides, such an algorithm is more than simply reassigning ex-highest scores. Every new value has to be tested on both highest marks so far. A beginner has to learn by example, sorry to disagree.
Code:
If score > highscore1 Then
'there is a catch, try entering 3,5,7, and 1 - it won't work correctly
'so uncomment the following and try to figure out why is it working
'If highscore1 > highscore2 Then
' highscore2 = highscore1
'End If
'if score is bigger than highscore1, assign that bigger value to highscore1
highscore1 = score
Else 'the Else part gets executed only if score is not bigger than highscore1
'so getting here means that score is not bigger than highscore1
'but we still want to check if score is bigger than highscore2
If score > highscore2 Then
'if it is, assign that bigger value to highscore2
highscore2 = score
End If
End If
What happens if highscore2 was originally bigger than highscore1? I see nothing in the original code layout that says it can't be. But if it is always that highscore1 is bigger, ...
Code:
if score > highscore2 and score < highscore1 then
highscore2 = score
elseif score >= highscore1 then
highscore2 = highscore1: highscore1 = score
end if
notice the >=. It is of course possible for the two highest scores to be exactly the same.
Last edited by Lord Orwell; Oct 29th, 2008 at 11:49 PM.
-
Oct 30th, 2008, 12:14 AM
#10
Thread Starter
New Member
Re: If statements drivin me crazy!
Thanks for they help guys, and btw i dont just copy and paste the answer, it helps me learn what i was doing wrong. Thanx for the help.
-
Oct 30th, 2008, 12:28 AM
#11
Re: If statements drivin me crazy!
Half's code is implementing completely different logic to yours, so what you were doing wrong was not getting your logic straight before you tried to write code to implement that logic. VB code is not a universe unto itself. VB code is an implementation of an algorithm. If your algorithm is flawed then of course code that correctly implements that algorithm will not produce the correct result. If you don't even have an algorithm then getting the correct result will be pure luck.
If I gave you three numbers and asked you to tell me what the two largest were I bet you could do it. That means that you must be able to follow a set of steps to come up with the result. That means that you must be able to describe those steps. That's the algorithm. That has nothing whatsoever to do with programming. Once you have the algorithm, then you can think about writing code to implement it.
In summary, what you did wrong was going straight from the spec to the code with no design in between. It's a common beginner mistake and the sooner you learn to do the design part the sooner you will become a software developer. You'll realise that after the fact, but I can live with the lack of appreciation until then.
-
Oct 30th, 2008, 09:07 AM
#12
Re: If statements drivin me crazy!
 Originally Posted by Lord Orwell
Code:
if score > highscore2 and score < highscore1 then
highscore2 = score
elseif score >= highscore1 then
highscore2 = highscore1: highscore1 = score
end if
Yes no doubt it can be made more compact but Jester needs to get the hang of the simple IF first. The debugger is a more advanced topic than this and it can be very frustrating having to learn more to do less. I just thought stepping through is better for out of bounds exceptions at the beginner's level and an overkill here.
-
Oct 30th, 2008, 09:54 AM
#13
Re: If statements drivin me crazy!
"That means that you must be able to follow a set of steps to come up with the result. That means that you must be able to describe those steps. That's the algorithm. That has nothing whatsoever to do with programming. Once you have the algorithm, then you can think about writing code to implement it." - GREAT ADVICE!
my first computer class(long,long, ago...) the instructor(W. Don Marsh) made us write algorithm's that described simple things, like directions to our home.
some people never got that the devil was in the details.
-
Oct 30th, 2008, 05:32 PM
#14
Re: If statements drivin me crazy!
 Originally Posted by Half
Yes no doubt it can be made more compact but Jester needs to get the hang of the simple IF first. The debugger is a more advanced topic than this and it can be very frustrating having to learn more to do less. I just thought stepping through is better for out of bounds exceptions at the beginner's level and an overkill here.
it's not overkill. You step through line by line and you can hover over the variables and see exactly what the program is doing. That's what it's for. The original error is pretty obvious. It has to be greater than highscore1 to even get to highscore2's check. But by then you've already assigned it to highscore1. That's why his numbers always matched.
-
Oct 30th, 2008, 06:00 PM
#15
Re: If statements drivin me crazy!
 Originally Posted by Half
Yes no doubt it can be made more compact but Jester needs to get the hang of the simple IF first. The debugger is a more advanced topic than this and it can be very frustrating having to learn more to do less. I just thought stepping through is better for out of bounds exceptions at the beginner's level and an overkill here.
The debugger is for debugging, period. Like anything, it's best to learn how to use it in simple situations first, before applying it to more complex ones. People should look at situations like this as opportunities to improve their skills rather than just the need to solve the specific problem at hand. If you learn a problem solving methodology, rather than just a solution to one single problem, then you're less likely to get stumped by similar problems in the future. I didn't think this way when I started out either, but it will be to anyone's own benefit to come to this realisation sooner rather than later, and that's what I'm trying to help with.
-
Oct 30th, 2008, 11:26 PM
#16
Re: If statements drivin me crazy!
 Originally Posted by Half
Yes no doubt it can be made more compact but Jester needs to get the hang of the simple IF first. The debugger is a more advanced topic than this and it can be very frustrating having to learn more to do less. I just thought stepping through is better for out of bounds exceptions at the beginner's level and an overkill here.
that's not all i did. His code always assigned the same number to both. Yours never will. Try this group:
1, 6, 7, 5, 8, 3, 8
If it is working properly, you should have 8 assigned to both and you don't.
-
Oct 31st, 2008, 12:44 AM
#17
Re: If statements drivin me crazy!
You didn't get the question correctly. Jester didn't want highest marks displaying twice. He needed the highest and the second below the highest values. Now, last time I checked 8 was not bigger than 8 (even with the debugger). Besides, what happened to "i won't give you the answer either"?
-
Oct 31st, 2008, 12:52 AM
#18
Re: If statements drivin me crazy!
Now, isn't this more fun than if I'd just posted a solution in the first reply?
-
Oct 31st, 2008, 01:26 AM
#19
Re: If statements drivin me crazy!
I agree with jmc and in my opinion a programmer should know at least three things before using Visual Studio. First how to debug; second put option strict and explicit on; and lastly how to make use of the dynamic help.
Now about the algorithm. Since no matter what kind of algorithm you may write, the code will make N(0) (N means the count of the values) instructions unless you make use of balanced binary tree. Since the instructions are going to be N(0) than why not add the values in to a list and than sort it, and then take the two highest (index 0 and 1) from the list. Even better idea, you can use sorted list. Isn’t this simple enough?
On the other hand, if you will not have more than two values at a time than think what would be the possible highscore1 value. It will be equal to highscore2 or beiger. And the highscore2 value would be equal to highscor1 or less. So let say highscore1 and highscore2 are zeros and you push a new value. The value should be compared with the highscor1 first. If the value > highscore1 than highscor2 is set with the value highscore1 and than highscore1 is set with the new value. If the value is < highscore1 than it is compared with highscore2. If the value > highscore2 than set it with the new value.
Last edited by VBDT; Oct 31st, 2008 at 01:21 PM.
-
Oct 31st, 2008, 06:08 AM
#20
Re: If statements drivin me crazy!
@jester -
what are the "scores"? bowling league, fantasy nascar, golf, SAT, etc.????
approx. how many scores will be looked at at one time?
how are the scores entered? by the user one-at-a-time, a file, etc.???
does the program need to remember scores across runs?
-
Oct 31st, 2008, 09:54 AM
#21
Re: If statements drivin me crazy!
 Originally Posted by Half
You didn't get the question correctly. Jester didn't want highest marks displaying twice. He needed the highest and the second below the highest values. Now, last time I checked 8 was not bigger than 8 (even with the debugger). Besides, what happened to "i won't give you the answer either"?
ok then, if you have a class of students, and you want to show the two highest scores, what happens if two people ace the test? This is a simple example. Anywhere you see someone keeping score on ANYTHING, duplicate scores are allowed simply because they are from different people. And this also applies to video games. If i am playing pacman and i tie the top score, i at least want to be in position 2 on the score list.
He never stated that he wanted no duplicate scores. That is you assuming something. And if he really is recording high scores, duplicates must be allowed.
-
Oct 31st, 2008, 09:59 AM
#22
Re: If statements drivin me crazy!
if we are going off the deep end...
if two people are tied for high, then the next highest score is really third.
-
Oct 31st, 2008, 12:20 PM
#23
Re: If statements drivin me crazy!
Ah, but then you are assuming you have a class of students. 'Anywhere' and 'anything' is incorrect. You may have a patient with a score for some time period. Then you want to see the 2 highest scores he ever achieved before dying/getting healthy in terms of t°, glasgow coma scale, tumor suze, whatever.
You made a different solution because you come from a different background (playing pacman and stuff) but this algorithm is by no means more or less appropriate than mine. It's just...different.
-
Oct 31st, 2008, 12:46 PM
#24
Re: If statements drivin me crazy!
You guys do realize that you're both pushing valid points on stuff that Jester-666 hasn't even chimed in on. Perhaps all of you should just drop this for now until Jester-666 has provided more (if he provides any more) information here.
-
Nov 1st, 2008, 12:28 PM
#25
Thread Starter
New Member
Re: If statements drivin me crazy!
I got this figured out a while ago, thanx for all the help, here is what i came up with.
Code:
Option Strict On
Option Explicit On
Public Class Form1
Dim highscore1, highscore2 As Double
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim score As Double
score = CDbl(txtInput.Text)
If score > highscore2 Then
highscore2 = highscore1
End If
If score > highscore1 Then
highscore1 = score
Else
If score > highscore2 Then
highscore2 = score
End If
End If
txtInput.Text = "0"
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
ListBox1.Items.Add("The two highscores are " & highscore1 & " and " & highscore2 & ".")
End Sub
End Class
-
Nov 1st, 2008, 01:07 PM
#26
Re: If statements drivin me crazy!
if it were me. i'd
tryparse
add to list
at end
sort list / reverse
get list(0) and list(1)
Code:
Dim scoreL As New List(Of Double)
Dim highscore1, highscore2, score As Double
highscore1 = Double.MinValue
highscore2 = Double.MinValue
'test algorithm
Dim somescores() As Double = New Double() {1, 15, 6, 32, 31, 4, 78, 3, 78, 80, 9, 10, 33, 79, 81}
For Each s As Double In somescores
Select Case s
Case Is >= highscore1
highscore2 = highscore1
highscore1 = s
Case Is > highscore2
highscore2 = s
End Select
Debug.WriteLine(highscore1.ToString() & " " & highscore2.ToString())
scoreL.Add(s)
Next
'end test
'If Not Double.TryParse(TextBox3.Text, score) Then
' 'the textbox did not contain a Number
' Stop
'End If
'Select Case score
' Case Is >= highscore1
' highscore2 = highscore1
' highscore1 = score
' Case Is > highscore2
' highscore2 = score
'End Select
''or
''scoreL.Add(score)
''scoreL.Sort() : scoreL.Reverse()
'''highscore1 = scoreL(0) : highscore2 = scoreL(1) <<<<<<<<<<
'If highscore1 <> Double.MinValue Then TextBox1.Text = highscore1.ToString
'If highscore2 <> Double.MinValue Then TextBox2.Text = highscore2.ToString
-
Nov 1st, 2008, 10:20 PM
#27
Re: If statements drivin me crazy!
 Originally Posted by Jester-666
I got this figured out a while ago, thanx for all the help, here is what i came up with.
Code:
Option Strict On
Option Explicit On
Public Class Form1
Dim highscore1, highscore2 As Double
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim score As Double
score = CDbl(txtInput.Text)
If score > highscore2 Then
highscore2 = highscore1
End If
If score > highscore1 Then
highscore1 = score
Else
If score > highscore2 Then
highscore2 = score
End If
end if
txtInput.Text = "0"
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
ListBox1.Items.Add("The two highscores are " & highscore1 & " and " & highscore2 & ".")
End Sub
End Class
i can't image a situation where this is what you intended. Perhaps if you explained it better?
It looks to me that the way you wrote it, if score is greater than score 2 but NOT bigger than score 1, your code will copy score1 into score2 (red code) and then overwrite the value you just placed in score2 with the value in score (green code). That is unnecessary code.
If this is what you intended however, please mark the thread as resolved.
Last edited by Lord Orwell; Nov 1st, 2008 at 10:24 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|