Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2006, 05:55 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default fill two-dimensional array

I have an Array with primenumbers: Prime() = 1,2,3,5,7,11,13,17,19
With this array I want to make a function FillArray(n As Integer).
This function tries to find every possible sum of primenumbers to be 7 (is Prime(4)).
BUT, every primenumber is to be followed by his next or previous primenumber in array Prime, like in following sequense. I want function FillArray to make this sequense.

1+2+3+5=11
1+2+3+2=8
1+2+1+2+3=9
1+2+1+2+1=7 is true
2+3+5=10
2+3+2=7 is true
2+1+2+3=8
2+1+2+1+2=8
3+5=8
3+2+3=8
3+2+1+2=8
5+7=12
5+3=8
7=7 is true

This seqence learns there are three sums of primenumbers to be true:
1+2+1+2+1=7
2+3+2=7
7=7
These results have to be put in a two-dimensional array like this:
1,2,1,2,1
2,3,2
7
I tried to make some code, but my VBA knowledge is too limited to make it function. So help is needed. Maybe the code helps understanding what I want, although you will find many errors in it.

    visual basic code:

    Function FillArray(n As Integer) 'for this example n=4
    Dim primeSum, counter, row, number As Integer
    Dim primeArray(row,number), Prime() As ?

    primeSum=0
    counter=0
    row=0
    number=0
     DoWhile primeSum<Prime(n)
                primeSum = primeSum + Prime(counter)
                primeArray(row,number) = Prime(counter)
                counter=counter+1
                number=number+1
                If primeSum=Prime(n)
                   primeArray(row,number)=Prime(counter)
                   row=row+1
         'Then copy in primeArray the values of the last row in the new one
         'except the last value
                   number=number-1 'go place back in the new row in primeArray
                   counter=counter-2 'now return to the DoWhile loop and go on
                Else counter=counter-2 'primeSum>Prime(n) When counter<2,
                                                  'number has to be number-1?

    primeSum=0 'start all over again
    a=1 'start with the second prime
    row=row+1 'go on with next row in primeArray
    number=0 ' and then DoWhile loop starts again






Similar Threads
Thread Thread Starter Forum Replies Last Post
using 2 dimensional array Abraham EJB 0 July 17th, 2007 09:27 AM
two-dimensional array Lizane Java Basics 4 May 23rd, 2007 09:35 AM
2 dimensional array trangd Beginning PHP 0 August 18th, 2005 12:37 AM
2 dimensional array venterjo General .NET 2 January 23rd, 2005 09:04 AM
Searching a two dimensional array Dinesh22 VB.NET 2002/2003 Basics 1 January 13th, 2004 03:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.