I wrote this code for an assignment that asked to create a program that calculates and displays the average of any amount of real numbers. The program is supposed to ask the user how many numbers they want to enter and then use a loop to ask the user to enter a number, keeping track of the sum as they are entered, and then calculating the average. I have it somewhat ok, until the end where it's supposed to give the average. I tried a couple things but none of them gave me the right answer. I don't think I'm keeping track of the sum either.

#include <iostream>

using namespace std;

double avg(double[],double);

int main () 
{
	int total;
	cout << "How many numbers would you like to enter? ";	
	cin >> total;
	
	while (total<=0)
	{
		cout << "Enter a number: ";
		cin >> total;
	}
	
	double nums[total];
	
	for (int i=0; i<total; i++) 
	{
		cout << "Enter number" << i+1 << ":";
		cin >> nums[i];
	}
	
	cout << "The average of the numbers you entered is " << avg(nums,total) << endl;

    return 0;
}

double avg(double vals[], double T)
{
	
}

Dani AI

Generated

your outline is fine. The two problems that break the average are:

  • double nums[total]; is a variable-length array. That is not standard C++ and may only work as a compiler extension. Prefer std::vector<double>.
  • You used int for the running sum and for the division. That truncates decimals on every addition and again when dividing. Keep the sum as double (or cast when dividing).

You also do not need new here; vectors manage memory for you (agreeing with ). Here is a minimal, standard C++ version that stores the inputs and computes the mean cleanly.

#include <iostream>
#include <vector>
#include <numeric>   // accumulate

double avg(const std::vector<double>& v) {
    if (v.empty()) return 0.0;               // or handle as an error
    double s = std::accumulate(v.begin(), v.end(), 0.0);
    return s / static_cast<double>(v.size());
}

int main() {
    std::size_t n = 0;
    std::cout << "Count of values: ";
    if (!(std::cin >> n) || n == 0) return 0;

    std::vector<double> vals(n);
    for (std::size_t i = 0; i < n; ++i) {
        std::cout << "Value " << (i + 1) << " of " << n << ": ";
        std::cin >> vals[i];
    }

    std::cout << "Average: " << avg(vals) << "\n";
}

Notes:

  • If you do not need to keep every value, you can skip the vector and add to a double sum as you read each input, then print sum / n.
  • For a nicely formatted decimal output, include <iomanip> and print with std::fixed << std::setprecision(3).

Recommended Answers

All 8 Replies

allocate memory using the new operator and save the address of that memory in the pointer.
you can try it like this;

int* nums = NULL;   // Pointer to int, initialize to nothing.
int total;           
cin >> total;       
nums = new int[total];
	for (int i=0; i<total; i++) 
	{
		cout << "Enter number" << i+1 << ":";
		cin >> nums[i];
	}

Forget the new operator. You don't need it.

I have it somewhat ok, until the end where it's supposed to give the average. I tried a couple things but none of them gave me the right answer.

What's your definition of "somewhat ok"? If it doesn't give you a proper answer, what's OK?

I don't think I'm keeping track of the sum either.

How can you tell?
What do you need to keep track of a sum?
If I give you 5 numbers, can you keep track of the sum on paper?
If so, how did you do it?
Do the same thing in your code.

Think through the problem, then code. Don't code, then beg for help.

What's your definition of "somewhat ok"? If it doesn't give you a proper answer, what's OK?
\

I said somewhat ok because the program I had was running, so there weren't errors in the code. It just wasn't calculating the average properly.

My code needs to run a sum function before an avg function. I can think through a problem I just don't know how to translate that into a code.

My code needs to run a sum

How do you do that? What do you need to do to run a sum?
Forget C++ for the moment and explain the process to me...

add all the input values

So, then add all the input values.

If that doesn't help, explain again in greater detail.

So, then add all the input values.

If that doesn't help, explain again in greater detail.

This is my new code, it sums the values and calculates the average, but how to I get it to give me an average that's a decimal?

#include <iostream>

using namespace std;

double sum(double[],double);

int main () 
{
	int total;
	cout << "How many numbers would you like to enter? ";	
	cin >> total;
	
	while (total<=0)
	{
		cout << "Enter a number: ";
		cin >> total;
	}
	
	double nums[total];
	
	for (int i=0; i<total; i++) 
	{
		cout << "Enter number" << i+1 << ":";
		cin >> nums[i];
	}
	
	int sum=0;
	int i=0;
	
	while (i<total)
	{
		sum=sum+nums[i];
		i++;
	}
	
	cout << "The average of the numbers you entered is " << sum/total << endl;
	
    return 0;
}

never mind my question about the decimals. I figured that one out.

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.