Using pointers for user defined array - calling array in function

First go through at using pointers to allow user defined array index.

I can create the array -

ptr1[inArraySize]

and show the contents of the array.

I am unable to "bubblesort" the array when calling it into the bubblesort function.


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>
using namespace std;

int *ptr1;

int inArraySize;
int userinput;
void bubblesort(int* ptr1, int inarraysize);
void binarysearch();

int main()
{
	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
	cin >> inArraySize;
	
	while (inArraySize <= 0 || inArraySize > 50)
	{
		cout << "Integer elements must be between 0 and 50: " << endl;
		cin >> inArraySize;
	}
	


	ptr1 = new int(inArraySize);
	if (ptr1 == NULL)
	{
		cout << "Error Allocating Memory!" << endl;
		return -1;
	}
	cout << "Enter the integer elements that will be searchable below: " << endl;
	for (int count = 0; count < inArraySize; count++)
	{
		cout << "Integer Element " << (count + 1) << ": " << endl;
		cin >> ptr1[count];
	}

	for (int count = 0; count < inArraySize; count++)
	{
		cout << ptr1[count] << endl;
	}
bubblesort(ptr1, inArraySize);


	return 0;
}

void bubblesort(int ptr1, int SIZE)
{
	cout << "in bubblesort" << endl;
	bool swap;
	int temp;
	int bottom = SIZE - 1;

	do
	{
		swap = false;
		for (int count = 0; count < bottom; count++)
		{
			if (ptr1[count] > ptr1[count + 1])
			{
				temp = ptr1[count];
				ptr1[count] = ptr1[count + 1];
				ptr1[count + 1] = temp;
				swap = true;

			}
		}
		bottom--;

	} while (swap != false);
}
Last edited on
new int(inArraySize) creates a single int with a value of inArraySize.
new int[inArraySize] creates an array of inArraySize ints.

void bubblesort(int ptr1, int SIZE) accepts two ints.
void bubblesort(int *ptr1, int SIZE) accepts a pointer to an int or array of ints, and an int.
Last edited on
I am trying to use the coding you game me.

It wasn't creating the array that was the problem, as displaying it gave all integers that were inputed out. It is sorting the array / using that function in the process.

I am not sure the reason why in lines 59 - 60, the array positions are not swapping correctly in the original array.

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>
using namespace std;

int *ptr1;

int inArraySize;
int userinput;
void bubblesort(int* ptr1, int inarraysize);
void binarysearch();

int main()
{
	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
	cin >> inArraySize;
	
	while (inArraySize <= 0 || inArraySize > 50)
	{
		cout << "Integer elements must be between 0 and 50: " << endl;
		cin >> inArraySize;
	}
	


	ptr1 = new int[inArraySize];
	if (ptr1 == NULL)
	{
		cout << "Error Allocating Memory!" << endl;
		return -1;
	}
	cout << "Enter the integer elements that will be searchable below: " << endl;
	for (int count = 0; count < inArraySize; count++)
	{
		cout << "Integer Element " << (count + 1) << ": " << endl;
		cin >> ptr1[count];
	}

	for (int count = 0; count < inArraySize; count++)
	{
		cout << ptr1[count] << endl;
	}
bubblesort(ptr1, inArraySize);


	return 0;
}

void bubblesort(int &array, int SIZE)
{
	cout << "in bubblesort" << endl;
	bool swap;
	int temp;
	int bottom = SIZE - 1;

	do
	{
		swap = false;
		for (int index = 0; count < SIZE; index++)
		{
			if (array[index] > array[index + 1])
			{
				temp = *array[index];
				array[index] = array[index + 1];
				array[index + 1] = temp;
				swap = true;

			}
		}
		bottom--;

	} while (swap != false);
}
Look at the signature for blubblesort() you wrote on line 47 and the signature I wrote on my previous post.
That is not allow my program to debug.

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
#include <iostream>
using namespace std;

int *ptr1;

int inArraySize;
int userinput;
int bubblesort(int* ptr1, int inarraysize);
int binarysearch(int ptr1, int numElems, int value);
int searchvariable(int value);

int main()
{
	int found, value;

	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
	cin >> inArraySize;

	while (inArraySize <= 0 || inArraySize > 50)
	{
		cout << "Integer elements must be between 0 and 50: " << endl;
		cin >> inArraySize;
	}



	ptr1 = new int[inArraySize];
	if (ptr1 == NULL)
	{
		cout << "Error Allocating Memory!" << endl;
		return -1;
	}
	cout << "Enter the integer elements that will be searchable below: " << endl;
	for (int count = 0; count < inArraySize; count++)
	{
		cout << "Integer Element " << (count + 1) << ": " << endl;
		cin >> ptr1[count];
	}

	for (int count = 0; count < inArraySize; count++)
	{
		cout << ptr1[count] << endl;
	}
	found = searchvariable(value);
	bubblesort(ptr1, inArraySize);
    binarysearch(ptr1, inArraySize, found);
	




	return 0;
}

void searchvaribale(int value)
{
	cout << "Integer to search array for: " << endl;
	cin >> value;
}


int bubblesort(int *ptr1, int SIZE)
{
	cout << "in bubblesort" << endl;
	bool swap;
	int temp;
	int bottom = SIZE - 1;

	do
	{
		swap = false;
		for (int index = 0; index < SIZE; index++)
		{
			if (ptr1[index] > ptr1[index + 1])
			{
				temp = ptr1[index];
				ptr1[index] = ptr1[index + 1];
				ptr1[index + 1] = temp;
				swap = true;

			}
		}
		bottom--;

	} while (swap != false);
}

int binarysearch(int ptr1, int numElems, int value)
{
	int first = 0;
	int last = numElems - 1;
	int middle;

	while (first <= last)
	{
		middle = first + (last - first) / 2;
		if (ptr1[middle] == value)
			return middle;
		else if (ptr1[middle] < value)
			last = middle - 1;
		else
			first = middle + 1;

	}
	return -1;
}
Last edited on
That is not allow my program to debug.

Please explain what you mean by that statement.


Loop on lines 72--82 commits out-of-range error.

Recheck the argument types of binarysearch().

What does the searchvaribale() do?

How do you deallocate the dynamically allocated memory?
some globals probably causing issues too. Change top part of program to remove all the globals and look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int bubblesort(int* ptr1, int inarraysize);
int binarysearch(int* ptr1, int numElems, int value);
bool searchvariable(int value);

int main()
{
	int found, value;
	int inArraySize;

	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
// ...
}


(and later change signature of the method definitions to match prototypes). This at least gets you closer to compilation.
I have been fiddling with the code.

line 53:55
Getting an invalid conversion from int* to int

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
110
111
112
113
114
115
116
#include <iostream>
using namespace std;

int *ptr1;

int inArraySize;
int bubblesort(int* ptr1, int inarraysize);
int binarysearch(int, int, int *);
int searchvariable(int value);

int main()
{
	int found, value;

	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
	cin >> inArraySize;

	while (inArraySize <= 0 || inArraySize > 50)
	{
		cout << "Integer elements must be between 0 and 50: " << endl;
		cin >> inArraySize;
	}



	ptr1 = new int[inArraySize];
	if (ptr1 == NULL)
	{
		cout << "Error Allocating Memory!" << endl;
		return -1;
	}
	cout << "Enter the integer elements that will be searchable below: " << endl;
	for (int count = 0; count < inArraySize; count++)
	{
		cout << "Integer Element " << (count + 1) << ": " << endl;
		cin >> ptr1[count];
	}

	for (int count = 0; count < inArraySize; count++)
	{
		cout << ptr1[count] << endl;
	}
	bubblesort(ptr1, inArraySize);

	for (int count = 0; count < inArraySize; count++)
	{
		cout << ptr1[count] << endl;
	}
	found = searchvariable(value);
	int *uservariable = nullptr;
	uservariable = &found;
	
	value = binarysearch(ptr1, inArraySize, *uservariable);
	
	cout << "The location in the arry " << value + 1 << "holds the value " << found << "." << endl;



cin.get();
	return 0;
}

int searchvariable(int value)
{
	cout << "Integer to search array for: " << endl;
	cin >> value;
}


int bubblesort(int *ptr1, int SIZE)
{
	cout << "in bubblesort" << endl;
	bool swap;
	int temp;
	int bottom = SIZE - 1;

	do
	{
		swap = false;
		for (int index = 0; index < SIZE; index++)
		{
			if (ptr1[index] > ptr1[index + 1])
			{
				temp = ptr1[index];
				ptr1[index] = ptr1[index + 1];
				ptr1[index + 1] = temp;
				swap = true;

			}
		}
		bottom--;

	} while (swap != false);
}

int binarysearch(int userlist, int numElems, int value)
{
	int first = 0;
	int last = numElems - 1;
	int *subptr;
	int middle = 0;

	while (first <= last)
	{
		middle = first + (last - first) / 2;
		subptr = &middle;
		if (userlist[subptr] == value)
			return middle;
		else if (userlist[subptr] < value)
			last = middle - 1;
		else
			first = middle + 1;

	}
	return -1;
}
The first parameter to the function binarysearch is expected to be an int.

ptr1 is not an int.
I am trying to create a user defined array index and elements.

I am then trying to sort that array.

I am then trying to allow the user to insert an integer, and search for it in that user defined/created array.



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
using namespace std;

int *ptr1;

int inArraySize;

int bubblesort(int list[], int inarraysize);
int binarysearch(int list[], int, int);
int searchvariable(int value);
int userUnsortedList[];
int main()
{
	int value, location;

	cout << "Please provide the number of integer elements between 0 and 50: " << endl;
	cin >> inArraySize;

	while (inArraySize <= 0 || inArraySize > 50)
	{
		cout << "Integer elements must be between 0 and 50: " << endl;
		cin >> inArraySize;
	}



	ptr1 = new int[inArraySize];
	if (ptr1 == NULL)
	{
		cout << "Error Allocating Memory!" << endl;
		return -1;
	}
	cout << "Enter the integer elements that will be searchable below: " << endl;
	for (int count = 0; count < inArraySize; count++)
	{
		cout << "Integer Element " << (count + 1) << ": " << endl;
		cin >> ptr1[count];
	}

	for (int count = 0; count < inArraySize; count++)  // creating the array correctly?
	{
		cout << ptr1[count] << endl;
		userUnsortedList[count] = ptr1[count];
		cout << userUnsortedList[count] << endl;
	}
	bubblesort(userUnsortedList, inArraySize);

	for (int count = 0; count < inArraySize; count++)  // visual check that list was sorted
	{
		cout << ptr1[count] << endl;
		cout << userUnsortedList[count] << endl;
	}
	
	value = searchvariable(value);  // value which the user entered - returned


	location = binarysearch(userUnsortedList, inArraySize, value); // location = index of 
                                                                                                   //  found value

	cout << "The location in the arry " << location + 1 << "holds the value " << value << "." << endl;

	

	cin.get();
	return 0;
}

int searchvariable(int value)  //takes in the value to search the array for
{
	cout << "Integer to search array for: " << endl;
	cin >> value;
	return value;
}


int bubblesort(int userlist, int SIZE)  // sorts the array - prep for searching
{
	cout << "in bubblesort" << endl;
	bool swap;
	int temp;
	int bottom = SIZE - 1;

	do
	{
		swap = false;
		for (int index = 0; index < SIZE; index++)
		{
			if (userlist[index] > userlist[index + 1])
			{
				temp = userlist[index];
				userlist[index] = userlist[index + 1];
				userlist[index + 1] = temp;
				swap = true;

			}
		}
		bottom--;

	} while (swap != false);
 
}


int binarysearch(int userlist, int numElems, int value)  //searching the array for input int
{
	int first = 0;
	int last = numElems - 1;
	int middle = 0;
	int position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (userlist[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (userlist[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return position;
}




I am returning errors to the pointers of the loops in the functions.
Last edited on
int binarysearch(int userlist, int numElems, int value)

Shouldn't userList be an int* rather than an int?
I thought that was the point of creating

userUnsortedList[]

from the ptr1[]

allowing me to use userUnsortedList[] as a created array of constants.
You do have:
1
2
int userUnsortedList[];
int* ptr1 = new int[ inArraySize ];

The ptr1 points to the first element of an array that has inArraySize integer elements.

How many integer elements are in the array userUnsortedList?


On function arguments:
1
2
3
4
5
int bubblesort(int [], int);
int binarysearch(int [], int, int);
// mean exactly same as
int bubblesort(int*, int);
int binarysearch(int*, int, int);

When i am calling in the bubblesort function

it MUST use a point to reference the memory location in this given example because the user is defining the amount of integer elements?

C-style arrays don't inherently know about their size. Once you learn about STL containers, you'll see that std::vector, for example, has access to a size() function.

So if you're trying to make functions with an interface to a C-style array, you need to provide a pointer to the front of the array, as well as its size. These variables are typically named "arr" and "arr_size" since most people associate the variable "arr" with an array.

C++ allows "int arr[]" syntax as well, which turns into "int* arr" behind the scenes. Similarly, you're allowed to use the bracket operator to access elements:
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
#include <iostream>

using namespace std;

void Show(int* arr, int arr_size)
{
    for(int i=0; i<arr_size; ++i)
        cout << arr[i] << ' ';
    cout << '\n';
}
void BubbleSort(int* arr, int arr_size)
{
    // etc.
}

int main()
{
    int arr_size = 5;
    int* arr = new int[arr_size];
    arr[0] = 10;
    arr[1] = 12;
    arr[2] = 7;
    arr[3] = 19;
    arr[4] = 16;
    Show(&arr[0], arr_size);   // This makes it clear, the ADDRESS of first element
    BubbleSort(arr, arr_size); // Also allowed to use this simpler-to-read syntax
    Show(arr, arr_size);
    delete [] arr;

    return 0;
}


Work with this example first and avoid all user input until your sort or search actually works.

Also, one of your main issues is that you're continuously not matching signature of function prototype, aka function declaration, to the function definition . If you put main() as the last method in the file, it can "see" the other functions and you don't need to prototype it. Prototyping is a useful skill, though -- eventually you'll see that people tend to break up the declarations and definitions into separate files.
Last edited on
Get rid of userUnsortedList and return to your code from your post on Jul 25, 2018 at 2:33pm. Here are some comments referring to that code:

You haven't declared binarySearch right. Just like bubbleSort, it needs a pointer to the array and the size of the array. It also needs the value that you're searching for. So change line 8 to
int binarysearch(int *ptr1, int size, int value);
and change line 96 to
int binarysearch(int *userlist, int numElems, int value)

Change searchVariable() to return the value (you fixed this in your most recent code). Also, it doesn't need to take any parameters. So change line 9 to:
int searchvariable();
and change lines 63-67 to:
1
2
3
4
5
6
7
int searchvariable()
{
    int value;
    cout << "Integer to search array for: " << endl;
    cin >> value;
    return value;
}

Note that value inside searchvariable is a local variable, so it is a different variable from the value declared in main.

Right now bubbleSort() is declared as returning an int. It doesn't need to return anything so change the return type to void at lines 7 and 70.

In bubbleSort() at line 80, index goes through all the possible values in the array. But lines 82 and 85 access the value after index. So line 80 should really be:
for (int index = 0; index < SIZE-1; index++) {

It looks like you got lost in the code that gets the searchvariable and does the binary search. You need the value that you're going to search for, and you need to remember the index of the value returned by binarysearch():
int value, index;
Getting the value to search for is pretty easy:
value = searchvariable();
Then you call binarysearch() to find the value. Binarysearch() returns an index:
index = binarysearch(ptr1, inArraySize, value);

But after calling binarysearch(), you can't just print out ptr1[index] because binarysearch might not have found it. binarysearch() returns -1 if it doesn't find the value so you need to check for that case. Maybe something like:
1
2
3
4
5
    if (index == -1) {
        cout << value << " isn't in the array\n";
    } else {
        cout << value << " is at location " << index+1 << '\n';
    }

The code in binarysearch isn't quite right:
- remove subptr (delete line 100 & 106)
- change subptr to middle at lines 107 and 109.
- change < to > at line 109

After these changes, the program should work.
Went back to basic, as this is my problem over and over.

I cannot get the displayarray function to run through the array.

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

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

int *ptr;
int *size;
int inArraySize;
void displayarray(int *list, int totalsize);



int main()

{

cout << "Please enter the total integer count between 1 - 50: " << endl;
cin >> inArraySize;
if (inArraySize <1 || inArraySize >50)
{
    cout << "Please Try Again" << endl;
    cout <<"Please enter the total integer count between 1 - 50: " << endl;
    cin >> inArraySize;
    size = &inArraySize;
}

ptr = new int[inArraySize];
if (ptr==NULL)
{
    cout << "Error allocating memory!\n";
    return 1;
}


int arrayMAX;
for (int count = 0, arrayMAX=1; count < inArraySize; count++, arrayMAX++)
{
    cout << "Please enter integer #" << (count+1) << ": " << endl;
    cin >> *(ptr+count);
    
}

cout << inArraySize<<endl;
cout << arrayMAX << endl;
cout << size << endl;
cout << *size << endl;

displayarray(*ptr, inArraySize);


    return 0;
}


void displayarray(int *list,int totalsize)
{
    for (int count = 0; count < totalsize; count++)
    {
        cout<<*(list+count)<<endl;
    }
}

I cannot get the displayarray function to run

"to run"?

You cannot even compile the code, let alone "run":
 In function 'int main()':
56:31: error: invalid conversion from 'int' to 'int*' [-fpermissive]
1
2
3
4
5
6
7
8
9
10
void displayarray( int *, int );
int main() {
  int foo {};
  int* bar {};
  displayarray( *bar, foo ); // error: invalid conversion from 'int' to 'int*'
  // the *bar IS-A int
  // the function requires int*
  // bar IS-A int*
  displayarray( bar, foo ); // ok
}



That said,
1. Your indentation could be more informative.
2. Global variables are not needed.
3. Some of the local variables have no apparent need.
4. *(foo+bar) is equivalent to foo[bar]
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
#include <iostream>

void displayarray(int *list, size_t totalsize);

int main()
{
  using std::cout;
  using std::cin;
  using std::endl;

  size_t inArraySize {};
  cin >> inArraySize;
  int *ptr = new int[inArraySize] {};

  int value {};
  size_t count {0};
  // the input can fail.
  while ( count < inArraySize && cin >> value )
  {
    ptr[count] = value;
    ++count;
  }

  cout << inArraySize<<endl;
  cout << count << endl;
  displayarray( ptr, count );
  return 0;
}


void displayarray( int *list, size_t totalsize)
{
    for ( size_t count = 0; count < totalsize; ++count )
    {
        std::cout << list[count] << '\n';
    }
}
Last edited on
Topic archived. No new replies allowed.