Array sorting and inserting help

I need help with sorting an array and then inserting a number. It outputs what im assuming is a memory address and the first number inserted isnt in the array I know theres ways to do it using set word like sort() or templates but I really want to know how to do it using only loops. Anything would help thank you

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#include<iostream>
#include<string>

using namespace std;

bool insert(int value, int intArray[], int & numberOfValidEnties, int size);

bool erase(int value, int intArray[], int & numberOfValidEnties);
int main()
{
	const int size = 10;
	int value = 0;
	int intArray[size];
	int numberOfValidEnties = 0;
	char answer;
	char selection;


	do {


		cout << "\n\n=================== What would you like to do ===================\n\n";
		cout << "A: Insert a value\n";
		cout << "B: Delete a value\n";
		cout << "C: Print the array\n";
		cout << "Your choice: ";

		cin >> selection;

		if (toupper(selection) == 'A')
		{

			insert(value, intArray, numberOfValidEnties, size);

		}

		else if (toupper(selection) == 'B')
		{

			erase(value, intArray, numberOfValidEnties);

		}

		cout << "Would you like to quit?(Y/N): ";
		cin >> answer;

	} while (toupper(answer) == 'N');

	return 0;






}

bool insert(int value, int intArray[], int & numberOfValidEnties, int size)
{
	int temp;

	

	cout << "Enter no more than 10 values to be sorted: ";
	cin >> value;
	while (value >= 0 && numberOfValidEnties < 10)
	{
		intArray[numberOfValidEnties] = value;
		numberOfValidEnties++;

		cout << "(Enter -1 to stop): ";
		cin >> value;


	}

	for (int i = 0; i < numberOfValidEnties; i++)    // sorting the array
	{
		for (int j = 0; j < numberOfValidEnties; j++)
		{
			if (intArray[j] > intArray[j+1])
			{
				temp = intArray[j];

				intArray[j] = intArray[j+1];

				intArray[j+1] = temp;

			}
		}
	}

	for (int i = 0; i < numberOfValidEnties; i++)
	{

		cout << intArray[i] << endl;

	}

	cout << "Enter a value to be added to the array: ";
	cin >> value;

	

	return true;

}






=================== What would you like to do ===================

A: Insert a value
B: Delete a value
C: Print the array
Your choice: a
Enter no more than 10 values to be sorted: 9
(Enter -1 to stop): 8
(Enter -1 to stop): 7 
(Enter -1 to stop): 6
(Enter -1 to stop): 5
(Enter -1 to stop): 4
(Enter -1 to stop): -1
-858993460
4
5
6
7
8
Enter a value to be added to the array:  


Last edited on
The problem is on line 80 for (int j = 0; j < numberOfValidEnties; j++)
You access an element outside the valid values.
If numberOfValidEnties is 3 and j is 2 for example - your code copies the value from intArray[3] to intArray[2]. Problem is that intArray[3] has no meaningful value
Topic archived. No new replies allowed.