Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 22nd, 2004, 9:48 AM   #1
Johnny_English
Newbie
 
Join Date: Sep 2004
Posts: 7
Rep Power: 0 Johnny_English is on a distinguished road
Hello,
I'm a beginner in VB and I'm having trouble with my code. It's a prime number calculator. Here is the function:

Public Function prime(x As Integer)
  Dim primes(0) As Integer
  primes(0) = 2
  For y = 2 To x
    bound= UBound(primes)
    For Index = 0 To bound
      If (y Mod primes(Index)) = 0 Then
        Exit For
      End If
    Next Index
    If Index = bound Then
        ReDim Preserve primes(boun + 1)
        primes(bound + 1) = y
    End If
  Next y
End Function
It receives the argument x passed by main.

When I run the code I get "Array already dimentioned."
Johnny_English is offline   Reply With Quote
Old Sep 22nd, 2004, 10:09 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Some of your variables are misspelled, others are undefined... I don't have VB on hand right now, but try this:

Public Function prime(ByVal x As Integer)
    Dim primes(0) As Integer
    Dim bound, y, Index As Integer

    primes(0) = 2
    For y = 2 To x
      bound = UBound(primes)
      For Index = 0 To bound
        If (y Mod primes(Index)) = 0 Then
          Exit For
        End If
      Next Index
      If Index = bound Then
        ReDim Preserve primes(bound + 1)
        primes(bound + 1) = y
      End If
    Next y
  End Function
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 22nd, 2004, 10:09 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
edit: ignore what i wrote if you read it.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Sep 22nd, 2004, 10:38 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
it was pretty good advice too Berto.

The code above compiles and runs fine in VB .NET
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 22nd, 2004, 10:41 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
nah i said something that was wrong, by a mile so thought i should get rid of it.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Sep 22nd, 2004, 11:15 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
In VB (not sure about VB .NET, but as you're not using that, it's irrelevant), if you want to redimension an array, you have to define it without any dimensions:
Dim primes() as Integer

Then redimension it at the start.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC