Question:
Can someone help me with this Delegate assignment?
?
2012-11-25 20:09:01 UTC
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

I mostly need help with the syntax necessary to perform the calculations, mainly for C (I think I can probably work out the calculations for the other ones); but if you would be willing to give me the syntax for the others, that would be great!
Five answers:
Jonathan
2012-11-25 21:06:37 UTC
It must be a FAQ. I've answered this one before. And the link shown below gives another time, too. Should make a Wiki on it.



Module vbconsole

    Delegate Function isvalid(Of T)(element As T) As Boolean

    Delegate Function createlist(Of T)(count As Integer) As List(Of T)

    Function factor(n As Integer) As Integer

        Dim q As Integer, sqrtn As Integer

        If n < 2 Then Return n - 1

        If n = 3 Then Return n

        If n Mod 2 = 0 Then Return 2

        sqrtn = CInt(System.Math.Sqrt(n))

        For q = 3 To sqrtn Step 2

            If n Mod q = 0 Then Return q

        Next

        Return n

    End Function

    Sub PrintList(Of T)(elements As List(Of T), printtest As isvalid(Of T))

        For Each p As T In elements

            If printtest(p) Then

                Console.Write(" {0}", p)

            End If

        Next

        Console.WriteLine("")

    End Sub

    Sub Main()

        Dim intlist As createlist(Of Integer) = Function(count As Integer) As List(Of Integer)

                                                    Dim i As Integer

                                                    Dim a As New List(Of Integer)

                                                    For i = 0 To count - 1

                                                        a.Add(i + 1)

                                                    Next

                                                    Return a

                                                End Function

        Console.Write("Enter N: ")

        Dim response As String = Console.ReadLine()

        Dim N As Integer = Val(response)

        Console.WriteLine("Odd numbers:")

        PrintList(intlist(N), Function(a) a Mod 2 = 1)

        Console.WriteLine("Even numbers:")

        PrintList(intlist(N), Function(a) a Mod 2 = 0)

        Console.WriteLine("Prime numbers:")

        PrintList(intlist(N), Function(a) factor(a) = a)

        Console.WriteLine("Numbers divisible by 4:")

        PrintList(intlist(N), Function(a) a Mod 4 = 0)

    End Sub

End Module



EDIT: The calculations of even, odd, and divisible by 4 are obvious to grade school students, so you couldn't possibly have been asking about that. Leaving prime numbers. And googling makes that trivial, too -- a waste asking here. So that leaves delegates as the only reasonable question you could be asking. But C doesn't have delegates. So no C, sorry.
no1home2day
2012-11-25 20:19:33 UTC
You need to start, and when you come to something you need help with, you can ask for assistance. But don't ask others to do your homework for you! That's called "cheating"; and out here in the "real" world, we don't need any cheaters, losers, or drop-outs, and when you use some one Else's work and claim it as your own, that's exactly what you become. :-(



But here are a few clues:

a) Use a simple for-next loop, from 1, with a step value of 2, and a stop value of N

b) You can not print even numbers if you start with 1. Start with 2, and use what I told in for a).

c) What IS a prime number? (A number that can only be divided evenly by 1 and by itself). Use a simple for-next loop from 1 to N, and for each loop, determine if the number is a prime number.

d) you cannot print even numbers if you start with 1. (Refer to (b).) Just use a step value of 4 rather than 2.



That should help enough to get you through most of it.



See how far you can get with it, and THEN ask for help.
Matt Johnson
2012-11-25 20:18:43 UTC
i'm not familiar with c or vb.net but i can tell you how it's done. use modulus division by 2 for a and b. for c, you need to create a for..next loop that divides by every num from 1 up to the num you entered minus 1. for c, the loop will contain a mod function. if the mod funtion returns 0 at least once, then that num is not prime. if it returns something other than 0 for all divisions in the loop, it's prime. for d, modulus divide by 4, if it returns 0, then print
bruse
2016-10-16 03:35:14 UTC
i presumed that the branch of delegates based on the share of victory in each contest would desire to be greater suited. I dont understand how human beings p.c. caucuses or primaries yet to make it honest, i could say do a lottery to determine who does which. Then there is not any 'computerized' income or shy away. As for those date leaping states...i could say, if the democratic management had a hand in the substitute (like the FL persons who voted unanimously to pass up their date) then they go through like they did this year. i could additionally determine that there became uniformity because it on the subject remember of the applicants. If the state doesnt count quantity then no names on the pollperiod. then you rather dont have somebody saying "I won vs uncommitted. "
Angel
2016-08-10 20:42:13 UTC
write a class of delegate that print prime number


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...