English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This is what i have to do:

Write a method to accept 10 date objects and store them in the array.

Expand the above method to create a look up table for ‘MONTHS’. ( This will have 12 elements one for each month and should contain a value showing how many times each month appears within the date object array.). Print the contents of the look up table to the screen.

Because the bucket sort looks at integers, i have tried to create a method for the months in my dates so that i could assign each month an iteger value.
e.g. final int January=0; etc

my dates are actually set in my main class and at the moment they look like this:
Date[] a = new Date[ARRAY_SIZE];
a[0]=new Date (2, "January", 2007);
a[1]=new Date (21, "Feburary", 2007); etc.

How do i get my assigned values linked with these dates? I know this example a[0]=new Date.LookUp() (2, "January", 2007); is incorrect but you should see what i am trying by calling up my lookup method which holds the int

2007-12-28 10:00:50 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

You need a function that returns a month number based on the month in each Date object. The function can be a member of the Date class or it could be separate. Simply call that function and use the result as an index into an array that counts how many times that month is used. Something like this:

int counts[12];
int i;

for (i = 0; i < 12; i++)
counts[i] = 0;

for (i = 0; i < 10; i++)
counts[Month(a[i])]++;

or...

for (i = 0; i < 10; i++)
counts[a[i].Month()]++;

The latter example assumes Month() is a member of Date.

2007-12-28 10:35:33 · answer #1 · answered by Craig R 6 · 0 0

I have an example below which uses the Date Datatype. I used VB2005 express and placed a button and a listbox on a form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim idx As Integer
Dim a(9) As Date 'Load with dates
Dim myDate As Date
Dim m(12), monthNum As Integer 'Hold month counts
Dim str As String 'Used to create a list box item


'Load array with dummy data starting today and adds a multiple
'of 21 days to each array element based upon the index value
'This will generate a number different months some of
'which will be duplicate months
For idx = 0 To 9
'Get the current date and convert to a double
'add a computed value to this double to increment
'the current date to a future date
'using FromAODate convert the double back into a system date
a(idx) = Date.FromOADate(System.DateTime.Today.ToOADate + (idx * 21))
Next

For idx = 0 To 9 ' read the dates
myDate = a(idx) 'Read stored date from array
monthNum = myDate.Month ' get the month number (1-12)
'increment the array element for the corresponding month
m(monthNum) += 1 ' count the month
Next

ListBox1.Items.Clear()
For idx = 1 To 12
str = "Month " & idx & " = " & m(idx)
ListBox1.Items.Add(str)

Next

End Sub


I don't have to worry about linking the months to the respective counter m() as I am using the system.Date object to extract numbers 1 to 12 which represent the month.

The m() array doesn't use the zero element in the array so I am wasting the m(0) resources as month 0 should never exist.

Read the date from the array a() into a Date variable and then assign its month property to an integer. This integer will be used as the index to point to the appropriate element in the totalizer array m()

2007-12-28 10:46:59 · answer #2 · answered by MarkG 7 · 0 0

fedest.com, questions and answers