Need help with array average

#include <iostream>
#include <cmath>
using namespace std;


int main()
{
// Prompt the user to enter array size
cout << "Enter array size: ";
int arraySize;
cin >> arraySize;

int *numbers = new int [arraySize];
int size = 0;

double avg(double [arraySize], int size)
{
for (int i=0; i<size; i++)
{

sum = sum + a[i];
}
return sum / size;
}


for (int i = 0; i < arraySize; i++)
{
// Read and store numbers in an array if it is new
cout << "Enter an integer: ";
int value;
cin >> value;

bool isInArray = false;

for (int j = 0; j < size; j++)
if (*(numbers + j) == value)
{
isInArray = true;
break;
}

if (!isInArray)
{
*(numbers + size) = value;
size++;
}
}

cout << avg << endl;
return 0;
}


This is my program and i cant figure out how to make the average function work. Any help would be greatly appreciated
First: Use code tags [code]Your code[/code]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
using namespace std;

// You can't have a function inside another function
double avg(double a[], int size) // Note the parameter
{
for (int i=0; i<size; i++)
{

sum = sum + a[i];
}
return sum / size;
}


int main()
{
// Prompt the user to enter array size
cout << "Enter array size: ";
int arraySize;
cin >> arraySize;

int *numbers = new int [arraySize];
int size = 0;


for (int i = 0; i < arraySize; i++)
{
// Read and store numbers in an array if it is new
cout << "Enter an integer: ";
int value;
cin >> value;

bool isInArray = false;

for (int j = 0; j < size; j++)
//if (*(numbers + j) == value)
if (numbers[j] == value)// This is neater
{
isInArray = true;
break;
}

if (!isInArray)
{
//*(numbers + size) = value;
numbers[size] = value; // This is neater
size++;
}
}

cout << avg(numbers, size) << endl; // Note that you have to call the function avg with () and paramters
return 0;
}
Topic archived. No new replies allowed.