CodeSolved

Solved Programming Questions & Exercises

The average function of infinite number of numbers

Practice Easy 84/ Download 2707 Views

Write a function that receives infinite parameter and returns the mean numbers

If the parameter was the non -extent

21 Answers

This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
#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;
}
Ai Download C & C++
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())
Amirsaleh Download Python
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())

F.sarli Download Python
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())
User 251 Download Python
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}")
Mma123 Download Python
return=num=int(input("Enter"[0: ])
if num is "str":
print("invaliable")
go to return:
avrage="num"/len(num)
User 1496 Download Python
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
Milad.bio Download Python
<< Previous page 1 2 3 Next page >>

Submit answer

Submitting answers is currently unavailable.

×
×
Close