Help with arrays and pointers

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 got step 1 & 2 and I created the addToSize function and I call it but it doesn't do anything...


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

int* addToSize (int*, int);

using namespace std;


int main()
{ 	
	
	int userSize;
	int userInts;
	int userArray[userSize] = {};
	int  *intptr;
	int *arrayNew;
	
	cout << "Please enter the array size!" << endl;
	cin >> userSize; 
	
	for (int count = 0; count < userSize; count ++)
	{
		cout << "Please enter the value for " << count+1 << endl;
		cin >> userInts;
	}
	
	addToSize(arrayNew, userSize);
	
	
	return 0;
	
	}
	
	
	
	
	
	
int* addToSize(int* arrayNew, int userSize) 
{
	int* expandSize= new int [userSize +1];
	
	for (int index = 0; index < userSize; index++)
	{
		expandSize[index]= arrayNew[index];
	}
    for (int index = userSize; index < (userSize+1); index ++) 
	{
		expandSize[index]=0;
	}	
	return expandSize;
}
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

In your main function, you call addToSize but neglect to store the return value. You should store the return value in a variable. Also, you should remember to eventually delete what you new and delete[] what you new[]. (Ideally, you should use smart pointers and actual containers instead)
This is what I have so far, when I run it, there is no output or errors...
Is this correct?



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

int* addToSize (int*, int);

using namespace std;


int main()
{ 	
	
	int userSize;
	int userInts;
	int userArray[userSize] = {};
	int *num = userArray;
	//int  *intptr;
	//int *arrayNew;
	//int newA;
	
	cout << "Please enter the array size!" << endl;
	cin >> 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 >> num[index];
    }
	
	num = addToSize(userArray, userSize);
   
	for(int index=0;index< (userSize + 1);index++)
		cout<<num[index]<<endl;

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

	
	int* addToSize(int* arrayNew, int newSize) 
{
	int* expandSize= new int [newSize +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;
}
You are corrupting memory. Line 16 is not valid C++ - even if it were, you are still using an uninitialized variable as the size of your array.
Okay I've already initialized userSize and the program runs. How would I set the first element of the array to 0 and copy element 0 and 1 of the argument array to 1 and 2 of the new array?
Please paste your current code so I can give you the best advice.
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

int* addToSize (int*, int);

using namespace std;


int main()
{ 	
	
	int userSize=0;
	int userInts;
	int userArray[userSize] = {};
	int *num = userArray;
	//int  *intptr;
	//int *arrayNew;
	//int newA;
	
	cout << "Please enter the array size!" << endl;
	cin >> 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 >> num[index];
    }
	
	num = addToSize(userArray, userSize);
   
	for(int index=0;index< (userSize + 1);index++)
		cout<<num[index]<<endl;

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

	
	int* addToSize(int* arrayNew, int newSize) 
{
	int* expandSize= new int [newSize +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;
}
Line 16 is still invalid. Just because your compiler accepts it does not mean it is correct. Also, it makes the array have zero elements. You are still corrupting memory.
Last edited on
I'm not sure how to fix that...
This is what I have ...


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

int* addToSize (int*, int);

using namespace std;


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

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

	
	int* addToSize(int* arrayNew, int newSize) 
{
	int* expandSize= new int [newSize +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;
}

Line 22 is still invalid. If you want a dynamically sized array, learn to use std::vector - it will make your life a lot easier:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector

Ideally, any decent C++ course should teach std::vector before plain arrays.
He had mentioned vectors, but very briefly. I am very lost now...

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>
#include<vector>


int* addToSize (int*, int);

using namespace std;


int main()
{ 	
	
	
	vector <int> userSize(0);
	vector <int> userInts;
	vector <int> userArray(userSize);
	int *num = userArray;
	//int  *intptr;
	//int *arrayNew;
	//int newA;

	cout << "Please enter the array size!" << endl;
	cin >> 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 >> num[index];
    }
	
	num = addToSize(userArray, userSize);
   
	for(int index=0;index< (userSize + 1);index++)
		cout<<num[index]<<endl;

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

	
	int* addToSize(int* arrayNew, int newSize) 
{
	int* expandSize= new int [newSize +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;
}
Why did you make userSize a vector? Don't just change random things and hope it works.

If you are going to use vectors, you will not need to use pointers in your program anymore. Most of the places you are using pointers can be replaced with vectors. Also, your addToSize function is no longer necessary - vector has .push_back(blah) built-in.
So should int userArray[userSize]; be the only vector? This program should be mainly focused on pointers..
Yes.

Does your assignment specifically forbid using std::vector or require using pointers? If that is the case, you should probably do what it says. (I don't agree with that way of learning, but I'm not going to make you fail an assignment because of my beliefs.)
It does not say anything about not using vectors, the lab is titled Pointers. He also told us to make sure that we utilize pointers when we are dynamically allocating an array using user's input as size. Otherwise the program won't work.
Alright, so you are specifically required to do things the hard way, thus vectors are out of the question.

Your userArray should be declared after the user inputs into userSize, and you need to use dynamic memory allocation with new[] (which means that before the program ends you will also need to call delete[]). You can look at your addToSize function for reference, as you have already done some of the logic there.
Am I using new[] correctly ?

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);

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];
	int userArray[userSize] = {};
	int *num = userArray;
	
	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 >> num[index];
    }
	
	num = addToSize(userArray, userSize);
   
	for(int index=0;index< (userSize + 1);index++)
		cout<<num[index]<<endl;

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

	
	int* addToSize(int* arrayNew, int newSize) 
{
	int* expandSize= new int [newSize +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
You don't need lines 27 or 28, just use your memory array.

Also, you should not declare a variable until you have a value to assign to it.
I still don't quite get it but thank you for the help.
Topic archived. No new replies allowed.