This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
when ever am trying to compile it, i get an error message
unresolved external symbol "void __cdecl showarray(int * const,int)"
am not sure what i am doing wrong? what should I do, my code looks like this
#include<iostream>
#include<string>
using namespace std;
void sortArray(int [], int );
void showarray(int [],int );
int main(){
int endtime[10] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };
cout << "unsorted" << endl;
showarray(endtime, 10);
sortArray(endtime, 10);
cout << "the sorted value are :" << endl;
showarray(endtime, 10);
return 0;
}
void sortArray(int array[], int size){
bool swap;
int temp;
do{
swap = false;
for (int count = 0; count < (size - 1); count++){
if (array[count] > array[count+1]){
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void showarray(const float array[], int size){
for (int i = 0; i < size; i++)
cout << array[i] <<" "<< endl;
}
At first you declared function
void showarray(int [],int );
but defined function
void showarray(const float array[], int size)
{
//...
}
Change the both declaration and the definition like
void showArray( const int [], int );
^^^^^^^^^ ^^^^^^^^^^^^
For example (the function definition)
void showArray( const int array[], int size )
{
for ( int i = 0; i < size; i++ )
{
cout << array[i] << ' ';
}
cout << endl;
}
And do not forget to change function calls using name showArray.
Take into account that the variable temp should be declared inside the if statement. Also you could use standard function std::swap as for example
std::swap( array[count], array[count+1] );
The program can look like
#include <iostream>
void sortArray( int a[], size_t n )
{
while ( !( n < 2 ) )
{
size_t last = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
int temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
last = i;
}
}
n = last;
}
}
void showArray( const int a[], size_t n )
{
for ( size_t i = 0; i < n; i++ ) std::cout << a[i] << ' ';
std::cout << std::endl;
}
int main()
{
const size_t N = 10;
int endtime[N] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };
std::cout << "unsorted: ";
showArray( endtime, N );
sortArray( endtime, N );
std::cout << " sorted: ";
showArray( endtime, N );
}
Its output is
unsorted: 70 38 8 101 11 127 313 14 16 127
sorted: 8 11 14 16 38 70 101 127 127 313
With using standard function std::swap the function sortArray can be more compact and clear
void sortArray( int a[], size_t n )
{
while ( not ( n < 2 ) )
{
size_t last = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
std::swap( a[i], a[i-1] );
last = i;
}
}
n = last;
}
}
You are declaring a
void showarray(int [],int );
but defining a
void showarray(const float array[], int size)
Both are not the same. At the point the compiler tries to compile the function call, he only knows about the first one and uses this. But the linker can only find the second one, which is why he produces an error.
Your function declaration needs to match your function definition:
void showarray(int [],int );
And
void showarray(const float array[], int size){
for (int i = 0; i < size; i++)
cout << array[i] <<" "<< endl;
}
Need to have matching function signatures.
Related
I have been trying to sort an array using recursion, but I am getting no output, can somebody help me with this, like why there is no output?
void sortarray(int arr[], int index, int key, int len){
if (index == len-2){
}
else{
if (key==len-1){
key=0;
sortarray(arr, index++, key, len );
}
else {
if (arr[key] > arr[key+1]){
swap(arr[key], arr[key+1]);
}
sortarray(arr, index, key++, len );
}
}
}
int main(){
int arr[5] = {5,6,4,3,2};
sortarray(arr, 0,0,5);
for(int i=0; i<5; i++){
cout << arr[i] << " ";
}
}
I fixed the segfault and simplified the code to make it more clear what it does. Namely, walks the array once and swap the key with the next if it's larger. This is not a valid sort algorithm:
#include <iostream>
using namespace std;
void sortarray(int arr[], int key, int len) {
// base case
if (key + 1 == len) return;
// recursive case
if (arr[key] > arr[key + 1]){
swap(arr[key], arr[key + 1]);
}
sortarray(arr, key + 1, len );
}
int main() {
int arr[] = {5,6,4,3,2};
int len = sizeof(arr) / sizeof(*arr);
sortarray(arr, 0, len);
for(int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
return 0;
}
and the result is:
5 4 3 2 6
I made some edits to your code and it works now (don't mind the printArr function, it's there just because cycles in main are ugly):
#include <iostream>
using namespace std;
void printArr(int arr[], int size)
{
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void sortarray(int arr[], int index, int key, int len)
{
if (index < len)
{
if (key == len - 1) {
key = 0;
sortarray(arr, ++index, key, len);
}
else {
if (arr[key] > arr[key + 1]) {
swap(arr[key], arr[key + 1]);
}
sortarray(arr, index, ++key, len);
}
}
}
int main()
{
int arr[5] = { 5,6,4,3,2 };
cout << "Before sort:" << endl;
printArr(arr, sizeof(arr)/sizeof(*arr));
sortarray(arr, 0, 0, 5);
cout << "After sort:" << endl;
printArr(arr, sizeof(arr) / sizeof(*arr));
return 0;
}
So the first problem was missing iostream and std namespace.
Key changes in the algorithm itself were:
change argument++ to ++argument
change the first condition to (index < len)
As to why your code didn't work properly: the index missed the stopping condition and went over the value of len (thus the screen o' fives).
So this answers your question, but this algorithm you wrote breaks once there are two same values next to each other. There is a good reason we use cycles for sorting and unless recursion is the point, I would recommend abandoning it for this task.
This code works perfectly-:
void sortArr(int arr[],int index, int key, int len){
// static int index = 0;
// static int key = 0;
if (index == len-1){
}
else{
if (key==len-1){
key=0;
sortArr(arr,index+1,key,len );
}
else {
if (arr[key] > arr[key+1]){
swap(arr[key], arr[key+1]);
}
sortArr(arr, index, key+1 ,len );
}
}
}
int main(){
int arr[5] = {5,6,4,3,2};
sortarray(arr, 0, 5);
for(int i=0; i<5; i++) {
cout << arr[i] << " ";
}
return 0;
}
The output of the code is:
2 3 4 5 6
#include <iostream>
#include <string>
using namespace std;
template <class T> void sortArray(T items, int count) {
T t;
for (int a = 1; a < 5; a++) {
for (int b = count - 1; b >= a; b--) {
if (items[b - 1] > items[b]) {
t = items[b - 1];
items[b - 1] = items[b];
items[b] = t;
}
}
}
}
template <class T> void printArray(T items, int count) {
T array[5];
for (int i = 0; i < count; i++) {
cout << array[i] << " ";
}
}
int main() {
int b[5];
double d[5];
char c[5];
int i = 0;
while (i < 5) {
cin >> b[i];
i++;
}
i = 0;
while (i < 5) {
cin >> d[i];
i++;
}
i = 0;
while (i < 5) {
cin >> c[i];
i++;
}
sortArray(b, 5);
sortArray(d, 5);
sortArray(c, 5);
printArray(b, 5);
printArray(d, 5);
printArray(c, 5);
return 0;
}
For starters even this function
template <class T> void printArray(T items, int count) {
T array[5];
for (int i = 0; i < count; i++) {
cout << array[i] << " ";
}
}
does not make sense. There is declared an uninitialized local array array that is outputted in a loop.
The function can be defined the following way
template <class T>
std::ostream & printArray( const T *items, size_t n, std::ostream &os = std::cout )
{
for ( size_t i = 0; i < n; i++ )
{
os << items[i] << ' ';
}
return os;
}
In calls of the template function sortArray like this
sortArray(b, 5);
the template type parameter is deduced to the type int *. So this declaration
T t;
is equivalent to
int *t;
that does not make sense.
Also within the function you are using a magic number 5.
Here is a demonstrative program that shows how the functions can be defined.
#include <iostream>
#include <utility>
template <class T>
void sortArray( T *items, size_t n )
{
for ( size_t last; !( n < 2 ) ; n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( items[i] < items[i-1] )
{
std::swap( items[i-1], items[i] );
last = i;
}
}
}
}
template <class T>
std::ostream & printArray( const T *items, size_t n, std::ostream &os = std::cout )
{
for ( size_t i = 0; i < n; i++ )
{
os << items[i] << ' ';
}
return os;
}
int main()
{
int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
const size_t N = sizeof( a ) / sizeof( *a );
printArray( a, N ) << '\n';
sortArray( a, N );
printArray( a, N ) << '\n';
return 0;
}
The program output is
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
I'm taking a C++ class and we've gotten to pointers. The assignment we've been given is to basically bubble sort an array that was read from a text file by passing pointers as parameters for various functions. I think I have a decent setup that outputs what I'm looking for, but for specific actions, I'm getting a zero as an element when there isn't one written to the array.
#include <iostream>
#include <fstream>
using namespace std;
int capacity;
int count;
int readData(int *&arr);
void swap(int *xp, int *yp);
void bsort(int *arr, int last);
void writeToConsole(int *arr, int last);
void bubble_sort(int *arr, int last, int(*ptr)(int, int));
int ascending(int a, int b);
int descending(int a, int b);
int main() {
int *whatever = NULL;
count = readData(whatever);
cout << "Raw array data:" << endl;
writeToConsole(whatever, capacity);
cout << "After simple bubble sort:" << endl;
bubble_sort(whatever, capacity, ascending);
writeToConsole(whatever, capacity);
cout << "Now descending:" << endl;
bubble_sort(whatever, capacity, descending);
writeToConsole(whatever, capacity);
return 0;
}
int readData(int *&arr) {
ifstream inputFile;
inputFile.open("data.txt");
if (!inputFile) {
cout << "Error!";
}
inputFile >> capacity;
arr = new int[capacity];
for(int i = 0; i < capacity; i++){
inputFile >> arr[i];
}
inputFile.close();
return capacity;
}
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bsort(int *arr, int last) {
int i, j;
bool swapped;
for (i = 0; i < last + 1; i++)
{
swapped = false;
for (j = 0; j < last-i; j++)
{
if (arr[j] > arr[j+1])
{
swap(arr[j], arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
void writeToConsole(int *arr, int last) {
cout << "[ ";
for(int i = 0; i < last; i++){
cout << arr[i] << " ";
}
cout << "]" << endl;
}
void bubble_sort(int *arr, int last, int(*ptr)(int, int)){
int i, j;
bool swapped;
for (i = 0; i < last; i++)
{
swapped = false;
for (j = 0; j < last-i; j++)
{
//Use the function pointer to determine which logic to use
if (ptr(arr[j] , arr[j+1]))
{
swap(arr[j], arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
int ascending(int a, int b){
return a > b;
}
int descending(int a, int b){
return a < b;
}
My output looks like this:
Raw array data:
[ 8 4 7 2 9 5 6 1 3 ]
After simple bubble sort:
[ 0 1 2 3 4 5 6 7 8 ]
Now descending:
[ 9 8 7 6 5 4 3 2 1 ]
Any ideas as to why my second sort is throwing in a zero? Thank you!
you have to do this change is in bubble_sort function
for (j = 1; j < last - i; j++)
{
//Use the function pointer to determine which logic to use
if (ptr(arr[j-1], arr[j]))
{
swap(arr[j-1], arr[j]);
swapped = true;
}
}
here I have a problem. I want to manipulate the following Matrix
Matrix A =
1---------2----------3
4---------5----------6
7---------8----------9
into
7---------8----------9
4---------5----------6
1---------2----------3
#include<iostream>
using namespace std;
void f(int ArrayName, int Size);
int main()
{
int x[3][3]={{1,2,3}, {4, 5,6},{7,8,9}};
f(x, 3);
system("pause");
}
void f(int ArrayName, int Size)
{
int holder;
for(int i=0; i<Size; i++){
for(int j=0; j<Size; j++)
{
holder=ArrayName[i][j];
ArrayName[i][j]=ArrayName[i+2][j+2];
ArrayName[i+2][j+2]=holder;
}
}
for(int k=0; k<Size; k++)
for(int l=0; l<Size; l++)
{
cout<<ArrayName[k][l];
if(l=3) cout<<"\n";
}
}
Errors:
E:\Semester 2\CPrograms\Array2.cpp In function `int main()':
10 E:\Semester 2\CPrograms\Array2.cpp invalid conversion from `int (*)[3][3]' to `int'
10 E:\Semester 2\CPrograms\Array2.cpp initializing argument 1 of `void f(int, int)'
E:\Semester 2\CPrograms\Array2.cpp In function `void f(int, int)':
22 E:\Semester 2\CPrograms\Array2.cpp invalid types `int[int]' for array subscript
(Last error repeated for five times)
All is done before us.:)
You can do quickly the assignment using standard algorithm std::reverse declared in header <algorithm>
For example
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
std::reverse( std::begin( a ), std::end( a ) );
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
The program output is
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3
As for your code then already this function declaration
void f(int ArrayName, int Size);
is wrong.
The first parameter should be either a reference to the array or a pointer to its first element.
For example
void f( int ( &ArrayName )[3][3] );
or
void f( int ( *ArrayName )[3], int Size );
If you want to write the function yourself then it can look the following way
#include <iostream>
void reverse( int ( *ArrayName )[3], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
for ( size_t j = 0; j < 3; j++ )
{
int tmp = ArrayName[i][j];
ArrayName[i][j] = ArrayName[Size - i - 1][j];
ArrayName[Size - i - 1][j] = tmp;
}
}
}
int main()
{
int a[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
::reverse( a, 3 );
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
The program output will be the same as above.
The prototype and declaration of your function should be :
void f(int ArrayName[][3], int Size)
Modify the code as:
#include<iostream>
using namespace std;
void f(int ArrayName[][3], int Size); //change here
int main()
{
int x[3][3]={{1,2,3}, {4, 5,6},{7,8,9}};
f(x, 3);
system("pause");
}
void f(int ArrayName[][3], int Size) //change here
{
int holder;
for(int i=0; i<Size/2; i++){ //change here
for(int j=0; j<Size; j++)
{
holder=ArrayName[i][j];
ArrayName[i][j]=ArrayName[size-i-1][size-i-1]; //change here
ArrayName[size-i-1][size-i-1]=holder; //change here
}
}
Try the solution in the code below. It uses the matrix as a vector, but has no dimension dependencies in the f() function prototype.
You have some errors in your code:
The array x[][] has to be passed by reference/pointer and not by value, then the function f() has to receive a pointer (or a reference) to int and not an int!
The for-loop scanning the matrix rows doesn't have to be for(int i=0; i<Size; i++), but for(int i=0; i<Size/2; i++). Using i<Size you swaps two times all the rows and then it seems nothing happens!
The following code:
ArrayName[i][j]=ArrayName[i+2][j+2];
ArrayName[i+2][j+2]=holder;
should be:
ArrayName[i][j]=ArrayName[Size-i-1][j];
ArrayName[Size-i-1][j]=holder;
The following code:
if(l=3) cout<<"\n";
l=3 assigns a value, don't do a comparison.
It should be:
if(l==Size-1) cout<<"\n";
#include<iostream>
using namespace std;
void f(int * ArrayName, int Size);
int main()
{
static const int Size=4;
int x[Size][Size];
//Loads the numbes into the matrix
for(int i=0;i<Size;i++)
for(int j=0;j<Size;j++)
x[i][j]=i*Size+j+1;
f(&x[0][0], Size);
}
void f(int *ArrayName, int Size)
{
int holder;
cout << "Input\n";
for(int k=0; k<Size; k++) {
for(int l=0; l<Size; l++)
cout<<ArrayName[k*Size+l]<<" ";
cout << "\n";
}
// Elaborate the matrix
// ----------------------------------------
for(int i=0; i<Size / 2; i++){
for(int j=0; j<Size; j++) {
holder=ArrayName[i*Size+j];
ArrayName[i*Size+j]=ArrayName[(Size-i-1)*Size+j];
ArrayName[(Size-i-1)*Size+j]=holder;
}
}
// ----------------------------------------
cout << "----------\n" << "Output\n";
for(int k=0; k<Size; k++) {
for(int l=0; l<Size; l++)
cout<<ArrayName[k*Size+l]<<" ";
cout << "\n";
}
}
i have a project in c++ -i receive an array, and i need to build a program that will print only the numbers that appears more than 3 times + their index's.
for example, for the array 6,4,4,5,2,4,4,3,5,5 - it will print:
4: 1,2,5,6
5: 3,8,9
and the most important - it should be no more than O(n*log n).
sooo....for the problem...
this is the error i keep getting:
1>HW - 2.obj : error LNK2019: unresolved external symbol "void __cdecl mergeSortP(int * const,int)" (?mergeSortP##YAXQAHH#Z) referenced in function "void __cdecl checkIfNumberIsMoreThenThreeTimes(int * const,int)" (?checkIfNumberIsMoreThenThreeTimes##YAXQAHH#Z)
1>C:\documents\visual studio 2012\Projects\HW - 2\Debug\HW - 2.exe : fatal error LNK1120: 1 unresolved externals
and this is the code:
void checkIfNumberIsMoreThenThreeTimes(int arr[], int n)
{
int **p;
p = copyToPointersArr(arr, n);
mergeSortP(*p, n);
output( arr, p, n);
}
//
int** copyToPointersArr(int *arr, int n)
{
int **pointers;
for (int i=0; i<n; i++)
*pointers[i]=arr[i];
return pointers;
}
//
void merge(int **a1, int **a2, int size1, int size2, int **res)
{
int ind1, ind2, ind;
ind1=ind2=ind=0;
while (ind1<size1 && ind2<size2)
{
if (*a1[ind1]<=*a2[ind2])
{
res[ind]=a1[ind1];
ind1++;
}
else
{
res[ind]=a2[ind2];
ind2++;
}
ind++;
}
while (ind1<size1)
{
res[ind]=a1[ind1];
ind1++;
ind++;
}
while (ind2<size2)
{
res[ind]=a2[ind2];
ind2++;
ind++;
}
}
//
void mergeSortP(int **a, int size)
{
int i;
int **temp=NULL;
if (size==1)
return;
else
{
mergeSortP(a, size/2);
mergeSortP(a+(size/2), size-(size/2));
temp = new int* [size];
merge(a, a+(size/2), size/2 , size-(size/2), temp);
for (i = 0; i < size; i++)
a[i] = temp[i];
delete []temp;
temp=NULL;
}
}
//
void output(int arr[], int **ptr,int size)
{
int i, j, count=0;
for (i = 0; i < size-1; i++)
{
if(*ptr[i]==*ptr[i+1])
count++;
else if (count>=2)
{
cout << *ptr[i] << ": ";
for (j = count; j >= 0; j--)
cout << (ptr[i-j]-arr) << ", ";
count=0;
}
else
count=0;
}
}
please help!!!
thanks in advance....
In void checkIfNumberIsMoreThenThreeTimes(int arr[], int n), you have
int **p;
and
mergeSortP(*p, n);
So the first argument to mergeSortP is of type int*.
However, you only have a function
void mergeSortP(int **a, int size)
so the linker complains because it can't find it. Maybe you meant to call
mergeSortP(p, n);
instead?
Following may help: https://ideone.com/oiAzTh
void displayFrequentNumber(const std::vector<int>& input)
{
std::vector<std::pair<int, std::size_t>> v;
for (std::size_t i = 0; i != input.size(); ++i) {
v.push_back({input[i], i});
}
std::sort(begin(v), end(v));
for (auto it = begin(v); it != end(v); ) {
auto next = find_if(it, end(v),
[it](const std::pair<int, std::size_t>& rhs)
{return it->first != rhs.first;});
if (next - it >= 3) {
std::cout << it->first << ":";
for (; it != next; ++it) {
std::cout << " " << it->second;
}
std::cout << std::endl;
}
it = next;
}
}