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)
{
	
}

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.