The average function of infinite number of numbers
Write a function that receives infinite parameter and returns the mean numbers
If the parameter was the non -extent
Write a function that receives infinite parameter and returns the mean numbers
If the parameter was the non -extent
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
#include <stdio.h>
float calculateAverage() {
int m = 0, n = 0, number;
while (1) {
printf("number: ");
if (scanf("%d", &number) != 1) {
printf("input no number\n");
while(getchar() != '\n'); // clear invalid input
continue;
}
if (number == -1) { // receive numbers until -1 is entered
break;
}
m += number;
n++;
}
return (n > 0) ? (float)m / n : 0; // print average of numbers
}
int main() {
printf("Average = %.2f\n", calculateAverage());
return 0;
}
#include <iostream>
float calculateAverage() {
int m = 0, n = 0, number;
while (true) {
std::cout << "number: ";
if (!(std::cin >> number)) {
std::cout << "input no number\n";
std::cin.clear(); // clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard invalid input
continue;
}
if (number == -1) { // receive numbers until -1 is entered
break;
}
m += number;
n++;
}
return (n > 0) ? (float)m / n : 0; // print average of numbers
}
int main() {
std::cout << "Average = " << calculateAverage() << std::endl;
return 0;
}
def average(): # Definition of function
sum, len = 0, 0
while 1:
try:
number = int(input("number: "))
if number == 0: # When zero enters the trick and shows the average
break
sum += number
len += 1
except ValueError: # This code ignores the numeric abuse
print("input no number")
return " average = {}".format(sum / len) # Finally this code prints the average numbers
print(average())
def a():
s = 0
x = 0
while True:
try:
b = int(input("num:"))
if b == -1:
break
s += b
x += 1
except ValueError:
print("input no number")
return s / x
print(a())
def calcaverage():
while True:
num = input('Enter number:')
if num == '0':
break
if num.replace(".", "").isnumeric():
list.append(float(num))
return sum(list)/len(list) if (list) else 'Enter at least one non-zero number'
list = []
print(calcaverage())
def calculate_average(*args):
total = 0
count = 0
for value in args:
if isinstance(value, (int, float)): # Check whether the amount of numeric is
total += value
count += 1
# Calculate the mean
if count == 0:
return 0 # If there were no numbers, the average of zero would be
return total / count
# Example of the use of function
average = calculate_average(10, 20, 30, 'a', None, 15.5, 5)
print(f"میانگین اعداد: {average}")
return=num=int(input("Enter"[0: ])
if num is "str":
print("invaliable")
go to return:
avrage="num"/len(num)
def calculate_average(*args):
# Filtering numeric parameters
numeric_values = [arg for arg in args if isinstance(arg, (int, float))]
# Calculate the mean if there is a numeric value
if numeric_values:
return sum(numeric_values) / len(numeric_values)
else:
return 0 # If there is no numeric value, the average of zero will be returned
Submitting answers is currently unavailable.
You must be logged in to access this section.
Login/Sign up If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.