So the assignment is to :
Design a program that generates 100 random numbers, and keep a count of how many of those random numbers are even and how many are odd. The function must return a Boolean value.
I wrote a code but I do not know if it is right (most likely it is not...) So i was hoping someone can help me and correct me.. OH and as I am taking a beginners class, the code is actually in pseudocode.

Declare Integer number
Declare integer evens = 0
Declare integer odds = 0
Set number = random(1, 100)
Display number

If isEven(number) Then
    Display “The number “, number, “is even.”
Else
    Display “The number “, number, “is odd.”
End If
Function Boolean isEven (Integer number)
    Declare Boolean status

    If number MOD 2 == 0 Then
        Set status = True
        Set evens = number + 1
Else
        Set status = False
        Set odds = number + 1
End If
Return status
End Function
Member Avatar for stbuchok

Pleas read the rules. Particularily:

Do provide evidence of having done some work yourself if posting questions from school or work assignments

Please try the assignment, and when you get stuck, ask for help with what you are actually stuck with.

For $1000 I would be willing to do the assignment for you.

Member Avatar for stbuchok

Just kidding on the last line, I'm too busy ;)

What language you code in?

The code above is all done by me. I do not know how to provide any more evidence then that. It is for my homework assignment yes but all the assignment said was that of above. I cannot really 'try' the code because it is pseudocode and all I could do was attempt to write it and now I am looking for help to see if what I wrote was right.

OK but you must at least have a language, C,C++,C#,Pascal,Python,Java etc. where you are going to write your code in. Tell us which?

And what have you tried in C++? Could you show us some code. Do not worry that your code is full of mistakes. That is what we're here for, to help you with your mistakes in your effort of producing code.

the teacher had said that this was just to be in pseudocode and not in the C++ code format.. so i do not have any actual code to show. I ws just given the prompt from the top and was told to write the pseudocode (which I did at the top). I wanted to know if what I wrote was correct and if not where am I wrong.. I hope I did not post wrong....

OK your IsEven function is correct, but leave out line 17 and 20. A functiion must do what it says, not setting some variables behind the scenes. Set those variables in your if-statement where the IsEven function is used.
Awaiting your new pseudocode listing.

so is it like this?

Declare Integer number
Declare integer evens = 0
Declare integer odds = 0
Set number = random(1, 100)
Display number

If isEven(number) Then
    Display “The number “, number, “is even.”
    Set evens = number + 1
Else
    Display “The number “, number, “is odd.”
    Set odds = number + 1
End If
Function Boolean isEven (Integer number)
    Declare Boolean status

    If number MOD 2 == 0 Then
        Set status = True
Else
        Set status = False
End If
Return status
End Function

Yes.
Now remove lines 8 and 11 from the if statement. If you can put the if statement in some kind of loop, from 1 to 100 I guess that would be fine.
And use in line 9 and 12 something like
Set evens = evens + 1
Set odds = odds + 1
Now at the end of the loop you can display the total odds and evens you can find in 100 random numbers. Success! Have to leave now, maybe tomorrow.

So in the end it would be like this:

Declare Integer number
Declare integer evens = 0
Declare integer odds = 0
Set number = random(1, 100)
Display number

If isEven(number) Then
    Set evens = evens + 1
    Set evens = number + 1
Else
    Set odds = odds +1
    Set odds = number + 1
End If

Display “The total even numbers generated was “, evens
Display “The total odd numbers generated was “, odds

Function Boolean isEven (Integer number)
    Declare Boolean status

    If number MOD 2 == 0 Then
        Set status = True
Else
        Set status = False
End If
Return status
End Function

Thank you for helping me I hope when you see this tomorrow it is correct!

I do not know how to delete that previous post but I had copied the wrong code.. its messed up so here is the real one:

Declare Integer number
Declare integer evens = 0
Declare integer odds = 0
Set number = random(1, 100)
Display number

If isEven(number) Then
    Set evens = evens + 1
Else
    Set odds = odds +1
End If
Display “The total even numbers generated was “, evens
Display “The total odd numbers generated was “, odds
Function Boolean isEven (Integer number)
    Declare Boolean status

    If number MOD 2 == 0 Then
        Set status = True
Else
        Set status = False
End If
Return status
End Function

You almost got it now. Just put lines 4 to 11 in a loop, say from 1 to 500 or whatever.

like so?

Declare Integer number
Declare integer evens = 0
Declare integer odds = 0
For counter = 1 To 100
    Set number = random(1, 100)
    Display number

    If isEven(number) Then
        Set evens = evens + 1
    Else
        Set odds = odds +1
    End If
End For
Display “The total even numbers generated was “, evens
Display “The total odd numbers generated was “, odds
Function Boolean isEven (Integer number)
    Declare Boolean status

    If number MOD 2 == 0 Then
        Set status = True
Else
        Set status = False
End If
Return status
End Function

Seems OK to me. Success!

yay! thank you so much for your help!! your the best!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.