9 Replies - 2507 Views - Last Post: 02 September 2010 - 02:37 AM Rate Topic: -----

#1 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Using functions to compute an array

Posted 01 September 2010 - 03:56 AM

Dim x As Integer

Private Sub cmdShow_Click()
lblAnswer.Caption = average
End Sub

Private Sub cmdStart_Click()
x = Val(InputBox("Enter the number of courses: ", "Number of courses: "))

For y = 2 To x
Load txtGrade(y)
txtGrade(y).Visible = True
txtGrade(y).Top = txtGrade(y - 1).Top + 50 + txtGrade(y - 1).Height + 50
Next

For y = 2 To x
Load txtHours(y)
txtHours(y).Visible = True
txtHours(y).Top = txtHours(y - 1).Top + 50 + txtHours(y - 1).Height + 50
Next

End Sub

Private Function average(grade() As Integer) As Double
Dim total As Double
grade() = Val(txtGrades())
For y = 1 To x
total = total + grade(y)
average = total / x
Next
End Function




please help me... i don't know why i can't access my array when i'm computing for the average and displaying it.

Here's the problem, please guide me if my codes are on the right track.. PLEASE... :((

*** Write a program to compute a student's grade point average. the program should use Input Box in the Form_load event procedure to request the number of courses to be averaged and then create elements in two text box to hold the grade and semester hours credit for each course. after the student fills in these text boxes and clicks on a command button, the program should use a function to compute the grade- point average. a sub procedure should display the GPA and then display one of the two messages. a student with a GPA of 3 or more should be informed that he has made it to the honor roll. otherwise, the student should be congratulated on having completed the semester, in either case, the student should be wished a merry vacation.


THANK YOU SO MUCH!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Using functions to compute an array

#2 parbipin  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 95
  • Joined: 23-August 10

Re: Using functions to compute an array

Posted 01 September 2010 - 05:14 AM

Quote

the program should use Input Box in the Form_load event procedure to request the number of courses to be averaged and then create elements in two text box to hold the grade and semester hours credit for each course.


This requires you to keep it in the form load event, but your code is in the cmdStart's Click event. This is only to highlight, otherwise this would work fine.

Are you able to create your text boxes; and is it only the average you are having trouble with;

If yes:
The problem is Line#26 that is not the correct way of assigning avlues to an array; and you do not even need an array in this case. all that you need to do is loop through the text boxes and have their values summed and divided by the count. You do need the average function.

The other problem in your code is that you are calculating average everytime the loop runs; this will not lead to any error or incorrect result in this case coz you are not disturbing the value of total; but can get you in trouble in future.

This is what you should have in your Private Sub cmdShow_Click():

Private Sub cmdShow_Click()
'Declaring all variables is a good habit
Dim t As Double
Dim a As Double
Dim i As Integer

For i = 1 To x
    t = Val(txtGrades(i).Text)
Next i

a = t / x
lblAnswer.Caption = a

End Sub


Was This Post Helpful? 1
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: Using functions to compute an array

Posted 01 September 2010 - 06:05 AM

do you mean that your array is array of textboxes ?

do you have any errors ?
Was This Post Helpful? 0
  • +
  • -

#4 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Using functions to compute an array

Posted 01 September 2010 - 06:35 AM

yes, it's an array of text boxes. my errors are on the function and sub procedure. it is stated in the problem that i need to use a sub and function procedure.

to: parbipin

i already modify the codes as what you instructed. but still, it has errors like Sub or procedure not defined. why is that appearing? :(
Was This Post Helpful? 0
  • +
  • -

#5 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Using functions to compute an array

Posted 01 September 2010 - 06:44 AM

to parbipin

i already got the right syntax! :) thank you so much!!! :) my problem now is how to access it as array in functions and sub.
Was This Post Helpful? 0
  • +
  • -

#6 parbipin  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 95
  • Joined: 23-August 10

Re: Using functions to compute an array

Posted 01 September 2010 - 06:52 AM

if your really need to use a function then create an array of values in your sub and then pass this array to the function which further computes your average.

so your sub would be:
Private Sub cmdShow_Click()
'Declaring all variables is a good habit
Dim MyArray() As Double
Dim i As Integer

ReDim MyArray(x)

'Transferring values from the Text Boxes to an Array
For i = 1 To x
    MyArray(i) = Val(txtGrades(i).Text)
Next i

'Passing the array of values we created to the function
lblAnswer.Caption = Myaverage(MyArray)

End Sub

'This is your Function

Function Myaverage(scores() as double) as double

Dim avg as Double
Dim tot as Double
Dim i   as Integer

For i = 1 to x
   tot = tot + scores(i)
Next i

avg = tot/x

Return avg

End Function




Good Luck
Was This Post Helpful? 1
  • +
  • -

#7 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Using functions to compute an array

Posted 01 September 2010 - 06:58 AM

Dim x As Integer

Private Sub cmdShow_Click()
Dim average As Double
Call display(average)
End Sub

Private Sub cmdStart_Click()
x = Val(InputBox("Enter the number of courses: ", "Number of courses: "))

For y = 2 To x
Load txtGrade(y)
txtGrade(y).Visible = True
txtGrade(y).Top = txtGrade(y - 1).Top + 50 + txtGrade(y - 1).Height + 50
Next

For y = 2 To x
Load txtHours(y)
txtHours(y).Visible = True
txtHours(y).Top = txtHours(y - 1).Top + 50 + txtHours(y - 1).Height + 50
Next

End Sub

Private Function average(total As Double) As Double

For i = 1 To x
    total = total + Val(txtGrade(i).Text)
Next i

average = total / x

End Function

Private Sub display(average As Double)
MsgBox average
End Sub



im sorry for disturbing you... :( ahm, this is the new code... now i'm having difficulty in displaying the average... :(
Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: Using functions to compute an array

Posted 01 September 2010 - 07:34 AM

if you call to display the average here.
Private Sub cmdShow_Click()
Dim average As Double
Call display(average)
End Sub



you dont pass any value to it perhaps your looking for this:
Private Sub cmdShow_Click()
Dim average As Double
Dim MyArray() As Double
Dim i As Integer

ReDim MyArray(x)

'Transferring values from the Text Boxes to an Array
For i = 1 To x
    MyArray(i) = Val(txtGrades(i).Text)
Next i
'here calculate the average using the parbipin function'
average=average(MyArray)
'now send the result from your function to the display sub'
Call display(average)
End Sub



i strongly suggest you to name your variables different from your function at least its easy to read. :)

This post has been edited by NoBrain: 01 September 2010 - 07:51 AM

Was This Post Helpful? 1
  • +
  • -

#9 parbipin  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 95
  • Joined: 23-August 10

Re: Using functions to compute an array

Posted 01 September 2010 - 07:42 AM

You are declaring a new variable for average in Sub cmdShow_Click while you should change the name of that variable to something else and pass teh value returned from your function to that variable

This is waht should be there in Sub cmdShow_Click

Private Sub cmdShow_Click()
Dim avg as Double

avg = average
Call display avg

End Sub



Also, in line 25 you have declared an argument (total) in function avrage which is not needed there instead should be declared inside the function with a Dim statement if you wanna do it this way. So that line should read
Private Function average() As Double 


This post has been edited by parbipin: 01 September 2010 - 07:50 AM

Was This Post Helpful? 2
  • +
  • -

#10 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Using functions to compute an array

Posted 02 September 2010 - 02:37 AM

THANK YOU SO MUCH PARBIPIN!!!!!! :') THANK YOU! God bless!!! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1