Hi ;
I know how to solve the same problem if i have limit numbers ,but here i have n intergers .How can the compiler understanst me when i need n integers ,like this question:

Write a program that takes an n number of integers and calculates their average.
Using for or while loop!

Dani AI

Generated

As pointed out, the usual pattern is: read numbers, accumulate a running total and a count, then compute average = total / count. noted that the input stream can provide the count up front. Two practical variants follow — one where the user supplies the number of values first, and one where values are provided until end-of-file or a sentinel. Each example below includes simple validation and avoids the common integer-division pitfall by using a floating-point accumulator.

Known-count (first line = how many values). This reads the count from a line, then keeps parsing numbers (allowing multiple numbers per line) until the requested amount is collected. It reports if fewer values were supplied and avoids division by zero.

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string line;
    if (!std::getline(std::cin, line)) return 0;
    std::istringstream first(line);
    int n = 0;
    if (!(first >> n) || n < 0) {
        std::cerr << "invalid count\n";
        return 1;
    }
    double sum = 0.0;
    int read = 0;
    while (read < n && std::getline(std::cin, line)) {
        std::istringstream in(line);
        double x;
        while (read < n && (in >> x)) {
            sum += x;
            ++read;
        }
    }
    if (read != n) std::cerr << "warning: fewer numbers than expected\n";
    double average = (read == 0) ? 0.0 : (sum / read);
    std::cout << average << '\n';
}

Unknown-count (read until EOF or sentinel). Useful for file/redirected input or when the user types numbers until done. EOF is signaled with Ctrl+D (Unix/mac) or Ctrl+Z then Enter (Windows).

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string line;
    double sum = 0.0;
    std::size_t count = 0;
    while (std::getline(std::cin, line)) {
        std::istringstream in(line);
        double x;
        while (in >> x) {
            sum += x;
            ++count;
        }
    }
    if (count == 0) {
        std::cout << "no numbers\n";
        return 0;
    }
    std::cout << (sum / count) << '\n';
}

Notes and pitfalls: always use a floating accumulator (double or long double) or cast before dividing to avoid truncation. Validate the supplied count (nonnegative) and handle zero values gracefully. For large streams, watch for overflow and prefer double or long double. When storing values (for median, etc.), read into a container and then use algorithms such as std::accumulate or parsing helpers like std::getline with std::istringstream for robust input.

Recommended Answers

All 3 Replies

you should create a loop which takes in a number with every pass, adds it to a total number and stores how many numbers where entered (how many passes the loop made)

Then you would need something to break out of the loop.

When you're out of the loop, calculate the average value and display it.

This is not a very difficult assignment if you understand how loops work.

Good luck!

cin >> n;

That will allow the user to input a number, if that's what you were looking for. If you need to know how to use loops, you should do a bit more studying. It isn't very hard, I promise!

I know that it is not hard ,but it needs practice .
Thanks for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.