How to resolve the algorithm Averages/Arithmetic mean step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Averages/Arithmetic mean step by step in the BASIC programming language
Table of Contents
Problem Statement
Write a program to find the mean (arithmetic average) of a numeric vector. In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Arithmetic mean step by step in the BASIC programming language
Source code in the basic programming language
mean = 0
sum = 0;
FOR i = LBOUND(nums) TO UBOUND(nums)
sum = sum + nums(i);
NEXT i
size = UBOUND(nums) - LBOUND(nums) + 1
PRINT "The mean is: ";
IF size <> 0 THEN
PRINT (sum / size)
ELSE
PRINT 0
END IF
REM specific functions for the array/vector types
REM Byte Array
DEF FN_Mean_Arithmetic&(n&())
= SUM(n&()) / (DIM(n&(),1)+1)
REM Integer Array
DEF FN_Mean_Arithmetic%(n%())
= SUM(n%()) / (DIM(n%(),1)+1)
REM Float 40 array
DEF FN_Mean_Arithmetic(n())
= SUM(n()) / (DIM(n(),1)+1)
REM A String array
DEF FN_Mean_Arithmetic$(n$())
LOCAL I%, S%, sum, Q%
S% = DIM(n$(),1)
FOR I% = 0 TO S%
Q% = TRUE
ON ERROR LOCAL Q% = FALSE
IF Q% sum += EVAL(n$(I%))
NEXT
= sum / (S%+1)
REM Float 64 array
DEF FN_Mean_Arithmetic#(n#())
= SUM(n#()) / (DIM(n#(),1)+1)
100 NUMERIC ARR(3 TO 8)
110 LET ARR(3)=3:LET ARR(4)=1:LET ARR(5)=4:LET ARR(6)=1:LET ARR(7)=5:LET ARR(8)=9
120 PRINT AM(ARR)
130 DEF AM(REF A)
140 LET T=0
150 FOR I=LBOUND(A) TO UBOUND(A)
160 LET T=T+A(I)
170 NEXT
180 LET AM=T/SIZE(A)
190 END DEF
You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Wren programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Circular primes step by step in the Go programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Action! programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the EchoLisp programming language