Can you guys help me understand this, please?

I am new to this website, this is my second post and I was wondering if I could have some help with this code. I am suppose to write a function that accepts as arguments the following:
A)An array of integers
B)An integer that indicates the number of elements in the array.
Overall, this code is suppose to be a random number generator and I have to find the avg of all the numbers. I am having trouble doing so with the averaging part.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int* randomArray(int);  /*This function creates an array of random numbers
							 of a given size within a given range*/
int* findMedValue(int, double);  //This function determines the median of the array
void displayArray(int*,int*, int);  //This function displays the array of random numbers

int main()
{
	srand(time(0));
	double avg;
	int size = 50;
	int* aptr = findMedValue(size, avg);
	int* iptr = randomArray(size);

	cout << " Random Array: ";
	displayArray(iptr, aptr, size);
	findMedValue(size, avg);
	cout << endl;

	delete[] iptr;
	cout << " Type any key to continue--> ";
	_getch();

	return 0;

}

//Define randomArray() function
/*This function creates an array of random numbers
of a given size in a given range pointed to by an integer pointer*/
int* randomArray(int size)
{
	int* iptr = new int[size];  //Allocate memory for the array
	for (int i = 0; i < size; i++)
	{
		*(iptr + i) = rand() % 101;  //Generates a random number 0--range
	}

	return iptr;
}

//Define displayArray()
void displayArray(int* iptr, int* aptr, int size)
{
	for (int i = 0; i < size; i++)
		cout << *(iptr + i) << " ";
	cout << endl;
	for (int i = 0; i < size; i++)
		cout << *(aptr + i) << " ";
	cout << endl;
}

//Define findMedValue() function
/*This function determines the average
of an array of values of a given size in a given range*/
int* findMedValue(int size, double avg)
{
	double sum = 0;
	int* aptr = new double[avg];

	for (int i = 0; i < size; i++)
	{
		sum += [i]
		*aptr = sum/size;
	{
	cout << " The Median is: " << avg << endl;

	return aptr;

}
Last edited on
A few problems here:

Line 17: avg is not initialized.
Line 19: Uninitialized value is passed to findMedValue().
Line 66. new is called with uninitialized value.
Line 73: You're outputting the uninitialized value.

Line 66: You're trying to allocate an array of doubles, but you're assigning the result to an int pointer.

Median and average are not the same thing. As I read your problem description, you want to find the average of the random numbers in your iptr array. If that's the case, shouldn't you be passing that array to your average function? I don't see that you have any need for the aptr array.

Line 70: You're effectively summing the values from 1 to 50.
Line 71: You're storing the calculation in the first element of the array every time through the loop.


Last edited on
I'm sorry, I am rusty on programming and I am bad at it lol I have to just find the average of 50 numbers ranging from 0-100. I am trying to use pointers because it states in the problem "demonstrate your pointer prowess by using pointer notation instead of array notation in this function."
I have fixed the problem thank you!
Topic archived. No new replies allowed.