Code errors

Compilation failed due to following error(s). main.cpp: In function 'int main()':
main.cpp:49:20: error: invalid conversion from 'int*' to 'int' [-fpermissive]
showArray(array, x);
^
main.cpp:16:6: note: initializing argument 1 of 'void showArray(int, int)'
void showArray(const int, int);
^
main.cpp:51:21: error: invalid conversion from 'int*' to 'int' [-fpermissive]
selectSort(array, n);
^
main.cpp:14:6: note: initializing argument 1 of 'void selectSort(int, int)'
void selectSort(int, int);
^
main.cpp:54:20: error: invalid conversion from 'int*' to 'int' [-fpermissive]
showArray(array, x);
^
main.cpp:16:6: note: initializing argument 1 of 'void showArray(int, int)'
void showArray(const int, int);
^
main.cpp:56:40: error: invalid conversion from 'int*' to 'int' [-fpermissive]
int results = binarySearch(array, x, n);
^
main.cpp:13:5: note: initializing argument 1 of 'int binarySearch(int, int, int)'
int binarySearch(const int, int, int);

I get these errors, I'm unsure what I did wrong? Any help would be greatly appreciated.

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
129
130
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int binarySearch(const int, int, int);
void selectSort(int, int);
int median(int, int);
void showArray(const int, int);

const int x = 50;

int main()
{
	int n;
	int array[x];
	
	
	string myfile;
	ifstream testfile;
	cout << "Enter file name: ";
    cin >> myfile;                  
    testfile.open(myfile.c_str());  
    if (!testfile)                  
    {
      cout << "Error opening file\n";
      cin.get();
      return -1;
    }
    else
	{
		testfile >> n;
		int array[n];
		for (int i = 0; i < 50; i ++)
			testfile >> array[i];
	}
	testfile.close();
	
	cin.get();
	return 0;
	
	cout << "The unsorted values are: \n";
	showArray(array, x);
	
	selectSort(array, n);
	
	cout << "The sorted values are: \n";
	showArray(array, x);
	
	int results = binarySearch(array, x, n);
	if (results == -1)
		cout << "That number does not exist in the array." << endl;
	else
	{
		cout << "That number is found at element " << results;
		cout << " in the array." << endl;
	}
	
	int median1;

	median1 = median ( array[0], x); 
	cout << endl << median1;
	
	return 0;
}

void selectSort(int array[], int n)
{

	int pos_min,temp;

	for (int i=0; i < n-1; i++)
	{
	    pos_min = i;
		
		for (int j=i+1; j < n; j++)
		{

		if (array[j] < array[pos_min])
                   pos_min=j;
	
		}
		
	
            if (pos_min != i)
            {
                 temp = array[i];
                 array[i] = array[pos_min];
                 array[pos_min] = temp;
            }
	}
}

int binarySearch(const int array[], int x, int value)
{
	int first = 0;
	int last = x-1;
	int middle;
	int position = -1;
	bool found = false;
	
	while(!found && (first <= last))
	{
		middle = (first+last)/2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle -1;
		else 
			first = middle+1;
	}
	return position;
}

 int  median(int array[], int x) 
{
    if (x %  2 == 0)
        return (array[x / 2] + array[x / 2 - 1]) / 2; 
    else
        return array[x / 2]; 
}

void showArray(const int array[], int x)
{
	for (int count = 0; count < x; count++)
		cout << array [count] << " " ;
	cout << endl;
}
On line 8 you have declared showArray as taking an int as the first argument but when you call the function you are trying to pass an array. An array can't be converted to an int, hence the error.
Last edited on
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int binarySearch(const int [], int, int);
void selectSort(int [], int);
int median(int, int);
void showArray(const int [], int);

const int x = 50;

int main(void)
{
	int n;
	int array[x];
	
	
	string myfile;
	ifstream testfile;
	cout << "Enter file name: ";
    cin >> myfile;                  
    testfile.open(myfile.c_str());  
    if (!testfile)                  
    {
      cout << "Error opening file\n";
      cin.get();
      return -1;
    }
    else
	{
		testfile >> n;
		int array[n];
		for (int i = 0; i < 50; i ++)
			testfile >> array[i];
	}
	testfile.close();
	cin.get();
	return 0;
	
	cout << "The unsorted values are: \n";
	showArray(array, x);
	
	selectSort(array, n);
	
	cout << "The sorted values are: \n";
	showArray(array, x);
	
	int results = binarySearch(array, x, n);
	if (results == -1)
		cout << "That number does not exist in the array." << endl;
	else
	{
		cout << "That number is found at element\n" << results;
		cout << " in the array." << endl;
	}
	
	int median1;

	median1 = median ( array[0], x); 
	cout << endl << median1;
	
	return 0;
}

//*******************************************************************
// selectSort function
// This function sorts out the array
// 
// Return value
// ----------------
// Void
// 
// Parameter
// ----------------
// int array[]
// int n
//********************************************************************
void selectSort(int array[], int n)
{

	int pos_min,temp;

	for (int i=0; i < n-1; i++)
	{
	    pos_min = i;
		
		for (int j=i+1; j < n; j++)
		{

		if (array[j] < array[pos_min])
                   pos_min=j;
	
		}
		
	
            if (pos_min != i)
            {
                 temp = array[i];
                 array[i] = array[pos_min];
                 array[pos_min] = temp;
            }
	}
}

//****************************************************************************
// binarySearch function 
// This function does a binary search of the array after it has been sorted
//
// Return value
// ---------------
// int
//
// Parameter
// ---------------
// int array[]
// int x
// int value
//****************************************************************************
int binarySearch(const int array[], int x, int value)
{
	int first = 0;
	int last = x-1;
	int middle;
	int position = -1;
	bool found = false;
	
	while(!found && (first <= last))
	{
		middle = (first+last)/2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle -1;
		else 
			first = middle+1;
	}
	return position;
}

//****************************************************************************
// median function
// This function calculates the median of array
//
// Return Value
// --------------
// int
//
// Parameter
// --------------
// int array[]
// int x
//*****************************************************************************
 int  median(int array[], int x) 
{
    if (x %  2 == 0)
        return (array[x / 2] + array[x / 2 - 1]) / 2; 
    else
        return array[x / 2]; 
}

//*****************************************************************************
// showArray function
// This function displays the array
// 
// Return value
// ---------------
// void
//
// Parameter
// ---------------
// int array[]
// int x
//*****************************************************************************
void showArray(const int array[], int x)
{
	for (int count = 0; count < x; count++)
		cout << array [count] << " " ;
	cout << endl;
}
}


I got the program to compile doing this, but it isn't outputting anything, what could possibly be causing this? These are the values that are stored in the .dat file.

67
34
566
233
1
28334
65
3343
233
45
34
233
78
12
3
5
890
23
4
455
3445
433
567
547
899
23
879
90
677
455
356
23
78
99
799
779
57547
455
334
847
Last edited on
The return statement on line 38 ends the program so the output code is never executed.
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int binarySearch(const int [], int, int);
void selectSort(int [], int);
int median(int [], int);
void showArray(const int [], int);

const int x = 50;

int main()
{
	int n;
	int array[x];
	
	
	string myfile;
	ifstream testfile;
	cout << "Enter file name: ";
    cin >> myfile;                  
    testfile.open(myfile.c_str());  
    if (!testfile)                  
    {
      cout << "Error opening file\n";
      cin.get();
      return -1;
    }
    else
	{
		testfile >> n;
		int array[n];
		for (int i = 0; i < 50; i ++)
			testfile >> array[i];
	}
	testfile.close();
	
	cout << "The unsorted values are: \n";
	showArray(array, x);
	
	selectSort(array, n);
	
	cout << "The sorted values are: \n";
	showArray(array, x);
	
	int results = binarySearch(array, x, n);
	if (results == -1)
		cout << "That number does not exist in the array." << endl;
	else
	{
		cout << "That number is found at element\n" << results;
		cout << " in the array." << endl;
	}
	
	float median1;

	median1 = median(array, x); 
	cout << median1 << endl;
	
	cin.get();
	return 0;
}

//*******************************************************************
// selectSort function
// This function sorts out the array
// 
// Return value
// ----------------
// Void
// 
// Parameter
// ----------------
// int array[]
// int n
//********************************************************************
void selectSort(int array[], int n)
{

	int pos_min,temp;

	for (int i=0; i < n-1; i++)
	{
	    pos_min = i;
		
		for (int j=i+1; j < n; j++)
		{

		if (array[j] < array[pos_min])
                   pos_min=j;
	
		}
		
	
            if (pos_min != i)
            {
                 temp = array[i];
                 array[i] = array[pos_min];
                 array[pos_min] = temp;
            }
	}
}

//****************************************************************************
// binarySearch function 
// This function does a binary search of the array after it has been sorted
//
// Return value
// ---------------
// int
//
// Parameter
// ---------------
// int array[]
// int x
// int value
//****************************************************************************
int binarySearch(const int array[], int x, int value)
{
	int first = 0;
	int last = x-1;
	int middle;
	int position = -1;
	bool found = false;
	
	while(!found && (first <= last))
	{
		middle = (first+last)/2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle -1;
		else 
			first = middle+1;
	}
	return position;
}

//****************************************************************************
// median function
// This function calculates the median of array
//
// Return Value
// --------------
// int
//
// Parameter
// --------------
// int array[]
// int x
//*****************************************************************************
 int  median(int array[], int x) 
{
    if (x %  2 == 0)
        return (array[x / 2] + array[x / 2 - 1]) / 2; 
    else
        return array[x / 2]; 
}

//*****************************************************************************
// showArray function
// This function displays the array
// 
// Return value
// ---------------
// void
//
// Parameter
// ---------------
// int array[]
// int x
//*****************************************************************************
void showArray(const int array[], int x)
{
	for (int count = 0; count < x; count++)
		cout << array [count] << " " ;
	cout << endl;
}


I now have an output, but the output is completely wrong. I'm sure the problem is integer overflow, but I'm not too sure what is causing this to happen.

Ex. of output: https://gyazo.com/d6ed823a685c5a128369f2a7c92e0a21
Last edited on
At line 32, you declare array. This is in scope only until line 35, at which point it is destroyed.

Note that this shadows the other array which you declared at line 15. That one, you never assign any values to.

Also, the declaration at line 32 is illegal. You cannot declare an array with variable length; the length must be known at compile-time.
@Mikey If it shadows the array at line 15, this means that this array declared at line 15 is pointless, as it is not outputting any values?

Also I think I understand why the declaration at line 32 is illegal, I have to assign n a value before declaring, so I would do something like n = 50 if I am dealing with an array holding 50 values?

What I don't understand is why the array declared at line 32 is only in scope until line 35 then it is destroyed, could you please explain why exactly this is?
@Mikey If it shadows the array at line 15, this means that this array declared at line 15 is pointless, as it is not outputting any values?

As you've written it, yes. But it seems to me that the one at line 15 is the one you should be using - because you want the array to be in scope throughout your main function. It's the one you declare at line 32 that's pointless.

Also I think I understand why the declaration at line 32 is illegal, I have to assign n a value before declaring, so I would do something like n = 50 if I am dealing with an array holding 50 values?

That's correct, yes. When declaring an array statically, the size needs to be a constant.

If you want to dynamically set the size of your array, you need to allocate the memory for it dynamically - or, better yet, let the standard library do the work for you, by using a std::array or a std::vector.

What I don't understand is why the array declared at line 32 is only in scope until line 35 then it is destroyed, could you please explain why exactly this is?

http://www.cplusplus.com/doc/tutorial/namespaces/
Topic archived. No new replies allowed.