Solve Computing an Average Problem | Help

  • Context: MHB 
  • Thread starter Thread starter mjcruzz
  • Start date Start date
  • Tags Tags
    Average Computing
Click For Summary
SUMMARY

The discussion focuses on calculating the average of a set of numbers using a simple algorithm. The formula for the average of three numbers, a, b, and c, is defined as (a + b + c) / 3. The provided example illustrates this with the numbers 3, 4, and 8, resulting in an average of 5. A generalized approach is also described for calculating the average of n numbers stored in an array, using a loop to sum the elements and then dividing by n.

PREREQUISITES
  • Understanding of basic arithmetic operations
  • Familiarity with programming concepts, specifically loops
  • Knowledge of arrays in programming
  • Basic syntax of a programming language (e.g., Python, Java, C++)
NEXT STEPS
  • Learn how to implement array manipulation in Python
  • Explore the use of built-in functions for average calculation in JavaScript
  • Study the concept of time complexity in algorithms
  • Investigate error handling for empty arrays in average calculations
USEFUL FOR

Students learning programming, software developers, and anyone interested in algorithm design and data manipulation.

mjcruzz
Messages
1
Reaction score
0
I can't seem to understand how to solve this problem. Please help
 

Attachments

  • Screen Shot 2021-01-30 at 8.20.30 PM.png
    Screen Shot 2021-01-30 at 8.20.30 PM.png
    205.7 KB · Views: 138
Technology news on Phys.org
The average of 3 numbers, a, b, and c, is [math]\frac{a+ b+ c}{3}[/math]. With the given example (3+ 4+ 8)/3= 15/3= 5.

You need "avg_sales= (numsales1+ numsales2+ numsales3)/3"
 
More generally, if you have n numbers, given as an array, num, and want to calculate their average it is
N= 0
for (i= 0; i< n; i++)
N= N+ num
Average= N/n;

In the case that n= 3, N would start at 0, after the i= 0 loop, N= 0+ num[0]= num[0], after the i= 1 loop, N= num[0]+ num[1], and after the i= 2 loop, N= (num[0]+ num[1])= num[2]= num[0]+ num[1]+ num[2]. The loop stops there because i= 3 does not satify "i< 3", and then Average= (num[0]+ num[1]+ num[2])/3.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 102 ·
4
Replies
102
Views
2K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
973
Replies
29
Views
5K
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
3
Views
2K