473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to average numbers from an array?

Private Sub btnDisplay_Click(sender As Object, e As System.EventArgs) Handles btnDisplay.Click
' calculates average score and displays the result

Dim intScores() As Integer = {88, 72, 99, 20, 66, 95, 99, 100, 72, 88, 78, 45, 57, 89, 85, 78, 75, 88, 72, 88}

This is the beginning of the programming, but I don't know how to continue to achieve the end result. I need to find the total and display the average score of the array in a message box.
Jan 19 '12 #1
11 11712
C CSR
144 100+
1) Get the length of the array.
2) Total the array values.
3) Divide the Total Value by the Array length.

Present results in msgbox; Do you need help with that part of the code?
Jan 19 '12 #2
@C CSR
My problem is the coding, I am confuse on how to word the codes to obtain the result
Jan 19 '12 #3
C CSR
144 100+
Using Access Basic:

Expand|Select|Wrap|Line Numbers
  1. Sub avgarray1()
  2. Dim varData As Variant
  3. varData = Array(1, 2, 3, 4, 5) 'zero based
  4. x = 0
  5.  For i = 0 To UBound(varData)
  6.    x = x + varData(i)
  7. Next
  8.  
  9. y = UBound(varData) + 1 'gives length of array
  10. varDataAvg = x / y
  11.  
  12. Debug.Print "Totaled values in array varData = " & x
  13. Debug.Print "Length of array varData = & y"
  14. Debug.Print "Average value in array = " & varDataAvg
  15.  
  16. End Sub
  17.  
See if this works.
Jan 19 '12 #4
C CSR
144 100+
Simple message box:
Expand|Select|Wrap|Line Numbers
  1. myMsg = MsgBox("Results = " & varDataAvg, vbOKOnly, "Example")
  2.  
Jan 19 '12 #5
C CSR
144 100+
Your VB variation:

Expand|Select|Wrap|Line Numbers
  1. Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
  2.  
Everything else worked in VB 2008 (I wasn't set up for that before)
Jan 19 '12 #6
C CSR
144 100+
You also need the code below, and put "Interaction.MsgBox" instead of just "msgbox" in the previous code.

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function MsgBox ( _
  2.     Prompt As Object, _
  3.     Buttons As MsgBoxStyle, _
  4.     Title As Object _
  5. ) As MsgBoxResult
  6.  
I'm a little behind on the real deal. Thanks for the exercise.
Jan 19 '12 #7
@C CSR


Thanks you, for some reason on my VIsual Basic those codes do not work. Maybe I am doing something wrong. I will keep trying to see what I am doing wrong and fix it.
Thanks again
Jan 19 '12 #8
@C CSR
I have VB 2010 and it not took that codes. Still I can't get the display box to get the sum of all numbers and get the average.
Jan 19 '12 #9
C CSR
144 100+
I hope I didn't mix you up because the very first code I sent was not what you needed. I used almost all of it, except:

1} I replaced 1 line the declaration of the array with #6.

2) I changed the msgbox line from from #5 and added the "Interaction" word.

3) I also added at the top of my module the code in #7.

I only have the 2008 Express version on this machine and to save time all I did was run it as a Console aplication. And I apologize because I'm rusty and because I haven't been building things in there real recently.

Somebody Else Needs to HELP US ON THIS, PLEASE.
Jan 19 '12 #10
danp129
323 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1.         Dim intScores() As Integer = {88, 72, 99, 20, 66, 95, 99, 100, 72, 88, 78, 45, 57, 89, 85, 78, 75, 88, 72, 88}
  2.         MsgBox(intScores.Average.ToString)
  3.         MsgBox(intScores.Sum.ToString)
  4.  
Jan 20 '12 #11
C CSR
144 100+
Thank you danp129.!
I knew there was a shortcut out there somewhere. My bad.
Jan 20 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul | last post by:
The following code: <?php header("Content-Type: text/plain"); $numbers = array(3.714, 0.7142857142857143, 9.667, 6.333, 7, 6, 8, 2, 3, -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5,...
10
by: Ole | last post by:
Goodday everybody, i want to create an array that represents data, that has to be transferred to a pdf doc ( iTextSharp ). The problem is, that i seem too loose my faith. Namely, ( i have...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
12
by: Steve | last post by:
here's a quirk i can't seem to handle, just hack. since call-time by-reference is depreciated and i don't want to enable it in the php.ini, i'm kind of stuck when i want to pass userdata as an...
1
by: Nightfarer | last post by:
Hello. I have a big trouble using System.Array class (it's the first time I use it) for a software I'm developing. I have a form with a textbox(numbers) and one button (done). Once the number...
2
by: tuananh87vn | last post by:
Hi ! can anyone help me with the following topic: Find All Duplicates in a List of Numbers - Array implementation - -InitializeTree() -AddNode() -Add into...
1
by: koger | last post by:
Hello I'm going to need two algorithms for an calculating average on array. I'm programming in c# / .Net I have an array with up to 72000 entries of random data. 1) How do I make an...
8
by: jabernet | last post by:
Is it possible in the new DB2 9.5 to use the content of an array in an IN expression? Say we have the ddl: CREATE TYPE Numbers AS Integer ARRAY; CREATE tab1 AS ( id integer, numbers...
2
by: raylopez99 | last post by:
Here is a short program demonstrating using IEnumberable, Linq, predicates, extension methods and some tricks and how to convert an IEnumberable sequence into an array. For future reference, not...
1
by: niner | last post by:
I have been working on the following code for about 6 hours now. I am attempting to write a program in Java that finds the highest, lowest, sum, and mean of an array. The program should also end...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.