Pointers & Arrays

Hi, so I am having some trouble with this program we have to make. This is the description of what we have to do, any help is greatly appreciated. Thank you.

1. Write a function that accepts an int array and the array’s size as arguments.

2. The program should ask the size of the array and lets the users enter some integer values.

3. The function should create a new array that is one element larger than the argument array.

4. The first element of the array should be set to 0.

5. Element 0 of the argument array should be copied to element 1 of the new array.

6. Element 1 of the argument array should be copied to element 2 of the new array, etc.

7. The function should return a pointer to the new array.

8. There should be three other functions: getMode, getMedian and getAverage. 8.1.These functions should get Mode, Median and Average of the values within an array.

9. You should display the argument array and the new array as well as the mode, median and the average.

This is what I have so far, I'm not sure if its right...

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

int* addToSize (int*, int, int);

using namespace std;


int main()
{ 	
	
	int userSize=0;
	int userInts;
	int *memory; //dynamically allocate an array
	

	//int  *intptr;
	//int *arrayNew;
	//int newA;
	
	cout << "Please enter the array size!" << endl;
	cin >> userSize; 
	
	memory = new int [userSize];
	
	
	for (int count = 0; count < userSize; count ++)
	{
		cout << "Please enter the value for " << count+1 << endl;
		cin >> userInts;
	}
	
	for (int index = 0; index < userSize; index ++)
    {
        cin >> memory[index];
    }
	
	memory = addToSize(memory, userSize);
   
	for(int index=0;index< (userSize + 1);index++)
		cout<<memory[index]<<endl;

	delete[] memory;	//Used to delete memory
	memory = 0;
	
    return 0;
    
}

	
	int* addToSize(int* arrayNew, int newSize, int userSize) 
{
	int* expandSize= new int [userSize +1];
	
	for (int index = 0; index < newSize; index++)
	{
		expandSize[index]= arrayNew[index];
	}
    for (int index = newSize; index < (newSize+1); index ++) 
	{
		expandSize[index]=0;
	}	
	return expandSize;
}
Last edited on
Please do not post more than once:
http://www.cplusplus.com/forum/general/175726/

If you want me in specific to stop helping you, just say so, no need to start a whole new thread for the same problem.
Topic archived. No new replies allowed.