I am doing this assignment, and it makes no sense to me whatsoever. The course did not supply adequate information to describe how to do this(I should know, i read it though 3 times), so even though they help me out it still isn't enough.
Even if someone could get me started, that would be great.
Here is the assignment:
3) Write a program using various procedures listed below to perform the operations listed below. Call these procedures using delegates.
a) Print 1 to N odd numbers
b) Print 1 to N even numbers
c) Print 1 to N prime numbers
d) Print 1 to N even numbers which are exactly divisible by 4
Be sure that you do not just list even and odd numbers. For example this would be incorrect. System.Console.Writeline(2,4,6,8,10).
The program says that you must print 1 to N even numbers. So you need to ask the user for N and print even numbers up to N. So if the user enters 5 you would print 2 and 4 and if the user enters 10 you would print 2,4,6,8,10. A number is even if it is divisible by 2 with no reminder. So If x%2 is 0 then the number is even, otherwise the number is not even. (The reverse is true for odd numbers.) The percent sign is the mod operator, the mod operator provides the remainder after doing division. For example 2mod2 (2%2) is 0 but 2%3 is 1. You will need to use a loop to complete this. Any numbr divisible by four is even. Thus you can use the mod opertr to dertmine if a number is divisible by 4. If N%4 is 0, then the number is divisible by four.
The prime number program is a little more difficult. I have attached code that you can use in your program.
Delegates Assignment 3d
In light of the difficulty of this problem, I have written a method that will print all prime numbers from 1 to N. N is a number supplied by the user. You should create your own procedure that calls the procedure I have written. Here is an example of how to write your procedure.
Sub ProcedureName
1.) Declare an integer variable. All prime numbers from 1 to the value stored in this variable will be printed
2.) Prompt user Ex: I will pint prime numbers from 1 to N, please eneter the value of N.
3.) Get input from user and store the value in variable created in step 1
4.) Call PrimeN and pass it the variable. Example: PrimeN(x)
End Sub
In the main method call the procedure you created using a delegate. In order for this program to work you will need to write your procedure and include the procedure below in your module.
Sub PrimeN(ByVal n As Integer)
Dim a As Integer = 1
Dim low, high, prime As Integer
Dim TPrime As Boolean
low = a
high = n
While low < high
prime = low
If prime = 2 Or prime = 3 Then
TPrime = True
ElseIf prime = 1 Then
TPrime = False
ElseIf prime = high Then
Exit While
Else
For i As Integer = 2 To (prime - 1)
If prime Mod i = 0 Then
TPrime = False
Exit For
Else
TPrime = True
End If
Next
End If
If TPrime = True Then
Console.WriteLine(prime)
End If
low += 1
End While
End Sub
End Module
Having trouble with Delegates...
Started by vbstudent, Jun 04 2010 10:05 AM
8 replies to this topic
#1
Posted 04 June 2010 - 10:05 AM
|
|
|
#2
Posted 04 June 2010 - 01:30 PM
So your task is to add these functions to delegades, right?
#3
Posted 04 June 2010 - 02:03 PM
Vswe said:
So your task is to add these functions to delegades, right?
No, my job is to:
Call these procedures using delegates.
a) Print 1 to N odd numbers
b) Print 1 to N even numbers
c) Print 1 to N prime numbers
d) Print 1 to N even numbers which are exactly divisible by 4
N is inputted by the user.
Now I think that if i didn't have to use delegates, I could have this basic format
Console.WriteLine("Enter a number that I will use for n")
N = Console.ReadLine()
'Print odd numbers from 1 to N
'Print even numbers from 1 to N <<<I would use If-Then-Else with modulo division to do these
'Print prime numbers from 1 to N <<<still not sure how to do this, even with all the code the teacher provides
'Print all even numbers from 1 to N that are divisible by 4 <<<Um I would use modulo division again with an if-then-else, but the else would be another if(if its divisible by 4)
This whole time I would use the same "N" as the user inputted at the beginning.
#4
Posted 04 June 2010 - 02:04 PM
Do you need help with the delegade bit or the actually calculations?
#5
Posted 04 June 2010 - 02:06 PM
I think mostly the delegate part. The calculations I could figure out, given enough time.
#6
Posted 04 June 2010 - 02:07 PM
What do you currently know about delegades? :)
#7
Posted 04 June 2010 - 02:10 PM
OK, I know that Delegates can call functions. Thats about it. For this course, when we had to write some code, I sorta just copied off the example to make this:
Module Module1
Private Delegate Sub ninjacalculatingness()
Sub Main()
Dim snickers As ninjacalculatingness
snickers = New ninjacalculatingness(AddressOf snickersAdd)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersSubtract)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersMultiply)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersDivide)
snickers()
End Sub
Sub snickersAdd()
Console.WriteLine("Enter first number to add")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to add")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 + num2)
End Sub
Sub snickersSubtract()
Console.WriteLine("Enter first number to a subtract")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to subtract")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 - num2)
End Sub
Sub snickersMultiply()
Console.WriteLine("Enter first number to multiply")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to multiply")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 * num2)
End Sub
Sub snickersDivide()
Console.WriteLine("Enter first number to divide")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to divide")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 / num2)
Console.WriteLine("Press any key to exit the program")
Console.ReadLine()
End Sub
End Module
I could tell you what it does, but I can't really tell you how it does that. Like I said, the course really didn't explain them too well. Multi-cast delegates are even worse.
Module Module1
Private Delegate Sub ninjacalculatingness()
Sub Main()
Dim snickers As ninjacalculatingness
snickers = New ninjacalculatingness(AddressOf snickersAdd)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersSubtract)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersMultiply)
snickers()
snickers = New ninjacalculatingness(AddressOf snickersDivide)
snickers()
End Sub
Sub snickersAdd()
Console.WriteLine("Enter first number to add")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to add")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 + num2)
End Sub
Sub snickersSubtract()
Console.WriteLine("Enter first number to a subtract")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to subtract")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 - num2)
End Sub
Sub snickersMultiply()
Console.WriteLine("Enter first number to multiply")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to multiply")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 * num2)
End Sub
Sub snickersDivide()
Console.WriteLine("Enter first number to divide")
Dim num1 As Integer = Console.ReadLine()
Console.WriteLine("Enter second number to divide")
Dim num2 As Integer = Console.ReadLine()
Console.WriteLine(num1 / num2)
Console.WriteLine("Press any key to exit the program")
Console.ReadLine()
End Sub
End Module
I could tell you what it does, but I can't really tell you how it does that. Like I said, the course really didn't explain them too well. Multi-cast delegates are even worse.
Edited by vbstudent, 04 June 2010 - 02:11 PM.
Forgot something
#8
Posted 04 June 2010 - 02:18 PM
Delegates is used to call a function but on another thread. This is usually used when you have time consuming functions, this allow your "normal" code(in the main thread) to continue working.
An example below will show it how it works(it's the same as your code though):
To use it, you use something like this:
An example below will show it how it works(it's the same as your code though):
Private Sub myTestSub(byval myTestParameter as string)
Console.Write(myTestParameter)
End Sub
Private Delegate Sub myTestDelegate(byval myTestDelegateParameter as string)
To use it, you use something like this:
Dim Invoker as new myTestDelegate(AddressOf myTestSub)
Invoker("This message will be written through another thread")
#9
Posted 06 July 2010 - 07:36 AM
Ok thanks I worked around that a little. Got a 55/60 on the project =D


Sign In
Create Account

Back to top









