I am wondering if there would be any chance that someone could help me out with this, I am trying to teach myself c++ which i find it really interesting and I came accross this and would love to see how it works so i could do it and learn by it.

  1. You need to implement code to output the following line to the screen:
    –- Task 1 –-
  2. You will need to use a for loop to multiply a sequence of integers entered by the user.
    2.1. Assume that the first integer read specifies the number of values remaining to be entered.

2.2. You program should read only one value per input (cin) statement.

2.3. Your program will then output both the product and the average of the numbers entered.

A typical input sequence might be:
−−− Task 1 −−−
Enter a l i s t o f numbers .
The f i r s t number w i l l r e p r e s e n t the t o t a l number o f r e p e t i t i o n s : 5 2 2 2 2 2
The p r oduc t o f the numbers e n t e r e d i s : 32
The a v e r a g e o f the numbers e n t e r e d i s : 2. 0
Another example may l o o k l i k e :
−−− Task 1 −−−
Enter a l i s t o f numbers .
The f i r s t number w i l l r e p r e s e n t the t o t a l number o f r e p e t i t i o n s : 3 1 2 3
The p r oduc t o f the numbers e n t e r e d i s : 6
The a v e r a g e o f the numbers e n t e r e d i s : 2. 0

Dani AI

Generated

For : the task is a straightforward input-loop problem. The program should print the task heading, read a first integer that specifies how many values follow, then read exactly that many integers (one value per cin), compute their product and the arithmetic mean, and print both results. As noted, it resembles homework; as suggested, confirming the compiler with a minimal program first is a sensible step.

A clear, safe approach:

  • Read the count n. If n <= 0 handle it explicitly (print a message or treat as an error).
  • Use a 64‑bit integer for accumulated product and a 64‑bit sum for the average accumulator.
  • Loop exactly n times, reading one value per cin, multiply into product and add into sum.
  • Compute average = static_cast<double>(sum) / n and format with std::fixed and std::setprecision(1) if a single decimal is required.

Example implementation (one possible, simple solution):

#include <iostream>
#include <iomanip>
#include <cstdint>

int main() {
    std::cout << "-- Task 1 --\n";
    long long n;
    std::cout << "Enter count: ";
    if (!(std::cin >> n)) return 0;

    if (n <= 0) {
        std::cout << "No numbers to process.\n";
        return 0;
    }

    std::int64_t product = 1;
    long long sum = 0;

    for (long long i = 0; i < n; ++i) {
        long long x;
        std::cin >> x;
        product *= x;
        sum += x;
    }

    double avg = static_cast<double>(sum) / n;
    std::cout << "Product: " << product << '\n'
              << "Average: " << std::fixed << std::setprecision(1) << avg << '\n';
    return 0;
}

Notes and troubleshooting: a single zero input makes the product zero. Large products may overflow 64‑bit integers; use boost::multiprecision::cpp_int or another big‑integer library for arbitrary precision. Reading with cin accepts values separated by spaces or newlines, satisfying the “one value per input” requirement. Typical compile command: g++ -std=c++11 file.cpp -o prog.

Recommended Answers

All 2 Replies

Sounds like homework. It also sounds like you haven't read the DaniWeb Rules or Please read this before posting.

Nice try he he. :)
We would love to see how you would show some effort by at least writing some code and telling us what problems you have.
Don't tell me you can't write an "Hello world" program in C++. It would at least solve your "--- Task1 ---" problem.

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.