Insert int into int array

Hi guys, I'm having trouble trying to put an int into an int array. I'm supposed to use a function called FillArray for the user to input an array of their choice then another function called insert that inserts the int into the array into a specific position and shifts the contents of the array right so the last int is lost and then a function called PrintArray that will print out the changed array.

PrintArray and FillArray cannot be changed and I cannot use vectors.

when I run the program, it sort of skips over insert and goes back to PrintArray with nothing changed to it.

Please help?


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
  

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

/*Given Function prototypes*/
void PrintArray(const int arr[], const int size);
void FillArray(int arr[], const int size);

void Insert(int arr, const int SIZE, int value, int InsertHere);

int main()
{
	const int SIZE = 15;
	int arr[SIZE];

	int value;
	int InsertHere;

	FillArray(arr, SIZE);

	PrintArray(arr, SIZE);

	cout << "Value: ";
	cin >> value;
	cout << "To be inserted in position: ";
	cin >> InsertHere;

	Insert();

	PrintArray(arr, SIZE);

	return 0;
}


void Insert(int arr[], const int SIZE, int value, int InsertHere)
{
	 
	for (int i = (value - 1); i < SIZE; i++)
		arr[value - 1] = 0;



}

void PrintArray(const int arr[], const int size)
// this function prints the contents of the array
{
	cout << "\nThe array:\n { ";
	for (int i = 0; i < size - 1; i++)	// print all but last item
		cout << arr[i] << ", ";

	cout << arr[size - 1] << " }\n";	// print last item
}

void FillArray(int arr[], const int size)
// this function loads the contents of the array with user-entered values
{
	cout << "Please enter " << size
		<< " integers to load into the array\n> ";

	for (int i = 0; i < size; i++)
		cin >> arr[i];			// enter data into array slot
}


Your code does not even compile; you call Insert without parameters.

What does your Insert do? Please explain in detail.
Use pointer for the array as your argument in FillArray function
Or change void FillArray to int FillArray, and make a return arr at the end of function
so I fixed it up and the output is what I want but I get a runtime error #2

how do I fix that?

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

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

/*Given Function prototypes*/
void PrintArray(const int arr[], const int size);
void FillArray(int arr[], const int size);


void Insert(int arr[], const int SIZE, int insert_this, int index);

int main()
{
	const int SIZE = 15;
	int arr[SIZE];
	int insert_this;
	int index;


	FillArray(arr, SIZE);

	PrintArray(arr, SIZE);

	cout << "\nEnter value to insert: ";
	cin >> insert_this;
	cout << "Enter index at which to insert: ";
	cin >> index;

	Insert(arr, SIZE, insert_this, index);

	PrintArray(arr, SIZE);

	return 0;
}


void Insert(int arr[], const int SIZE, int insert_this, int index)
{
	
	//moves all elements up one position
	for (int i = SIZE; i > index; i--)
	{
		arr[i] = arr[i - 1];
		
	}
	//puts new value at index desired
	arr[index] = insert_this;
	
	
}


void PrintArray(const int arr[], const int size)
// this function prints the contents of the array
{
	cout << "\nThe array:\n { ";
	for (int i = 0; i < size - 1; i++)	// print all but last item
		cout << arr[i] << ", ";

	cout << arr[size - 1] << " }\n";	// print last item
}

void FillArray(int arr[], const int size)
// this function loads the contents of the array with user-entered values
{
	cout << "Please enter " << size
		<< " integers to load into the array\n> ";

	for (int i = 0; i < size; i++)
		cin >> arr[i];			// enter data into array slot
}
in your insert function you try to access an array element at index 15 the size of the array is 15 and therefore valid indexes are 0 - 14.


also you want to check that the index the user enters is valid
Last edited on
thank you! I was able to fix the problem by adding a -1 here:
 
for (int i = SIZE - 1; i > index; i--)


I also made it so that I can check if the index is valid with an if else
Topic archived. No new replies allowed.