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;
}
}
Related
The error is on line 76 int res[mSize]; the problem is on mSize. It seems like a simple fix but I can't figure it out. If someone can figure it out or point me in the right direction that would be greatly appreciated.
Also, the deconstructor ~MyContainer(), I am not sure if I am using it right or if there is a correct place to put it.
Here is my code:
#include <iostream>
using namespace std;
class MyContainer
{
private:
int* mHead; // head of the member array
int mSize; // size of the member array
public:
MyContainer();
MyContainer(int*, int);
//~MyContainer();
void Add(int);
void Delete(int);
int GetSize();
void DisplayAll();
int FindMissing();
~MyContainer() {}
};
MyContainer::MyContainer()
{
mHead = NULL;
mSize = 0;
}
MyContainer::MyContainer(int* a, int b)
{
mHead = a;
mSize = b;
}
void MyContainer::Add(int a)
{
*(mHead + mSize) = a;
mSize++;
}
void MyContainer::Delete(int a)
{
int index;
for (int i = 0; i < mSize; i++)
{
if (*(mHead + i) == a)
{
index = i;
break;
}
}
for (int i = index; i < mSize; i++)
{
*(mHead + i) = *(mHead + i + 1);
}
mSize--;
}
int MyContainer::GetSize()
{
return mSize;
}
void MyContainer::DisplayAll()
{
cout << "\n";
for (int i = 0; i < mSize; i++)
{
cout << *(mHead + i) << " ";
}
}
int MyContainer::FindMissing()
{
int res[mSize];
int temp;
int flag = 0;
for (int i = 1; i <= mSize; i++)
{
flag = 0;
for (int j = 0; j < mSize; j++)
{
if (*(mHead + j) == i)
{
flag = 1;
break;
}
}
if (flag == 0)
{
temp = i;
break;
}
}
return temp;
}
int main()
{
const int cSize = 5;
int lArray[cSize] = { 2, 3, 7, 6, 8 };
MyContainer lContainer(lArray, cSize);
lContainer.DisplayAll();
lContainer.Delete(7);
lContainer.DisplayAll();
cout << "Size now is: " << lContainer.GetSize() << endl; lContainer.Add(-1);
lContainer.Add(-10);
lContainer.Add(15);
lContainer.DisplayAll();
cout << "Size now is: " << lContainer.GetSize() << endl;
cout << "First missing positive is: " << lContainer.FindMissing() << endl;
system("PAUSE"); return 0;
}
int res[mSize];
The size of the array mSize must be known at compile time. You cannot use a variable here. An option may be to define a macro with an largish value that will not exceeded.
static const int kLargeSize =100;
int res[kLargeSize];
Edited in response to the comments - const and constexpr are a better option than a macro.
Or even better, you can use std::vector - https://en.cppreference.com/w/cpp/container/vector
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
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.
There is a mismatch error in strcpy() function. I'm new to the C++ language.
#include<iostream>
#include<cstring>
using namespace std;
#define max 5
class ss {
private:
char str[max][10];
public:
void get_str() {
for (int i = 0; i < max; i++)
cin >> str[i];
}
void disp() {
cout << "Entered strings are\n";
for (int i = 0; i < max; i++) {
if (strcmp(str[i], str[i + 1]) != 0)
cout << str[i] << endl;
}
}
/*
void sort()
{
char temp[max];
for (int i = 0; i < max - 1; i++)
for (int j = 0; j < (max - i - 1); j++)
{
if (strcmp(str[j], str[j + 1])>0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
disp();
}
*/
void qsort()
{
qs(str, 0, max - 1);
disp();
}
void qs(char *&str, int st, int e)
{
int pi = part(str, st, e);
qs(s, st, pi - 1);
qs(s, pi + 1, e);
}
int part(char *&s, int st, int e)
{
char pi[max], swap[max];
strcpy(pi, s[e]);
int pii = st;
int i = st;
for (i; i < e; i++) {
if ((strcmp(s[i], s[pii])) <= 0)
{
strcpy(swap, s[pii]);
strcpy(s[pii], s[i]);
strcpy(s[i], swap);
pii++;
}
}
strcpy(swap, str[e]);
strcpy(str[e], str[pii]);
strcpy(str[pii], swap);
}
};
main()
{
ss s;
cout << "Enter the strings\n";
s.get_str();
s.disp();
s.sort();
cout << "after the sort" << endl;
s.disp();
}
I found several problems with your code:
Arguments are passed to qs() and part() as char *&, not char ** or char [max][10].
s is undefined in qs() (did you mean str?).
ISO C++ forbids a definition of main() without a return value.
part() has a non-void return type and doesn't return a value.
Repaired code is shown below:
#include <iostream>
#include <cstring>
using namespace std;
#define max 5
class ss {
private:
char str[max][10];
public:
void get_str() {
for (int i = 0; i < max; i++)
cin >> str[i];
}
void disp() {
cout << "Entered strings are\n";
for (int i = 0; i < max; i++) {
if (strcmp(str[i], str[i + 1]) != 0)
cout << str[i] << endl;
}
}
void sort()
{
char temp[max];
for (int i = 0; i < max - 1; i++)
for (int j = 0; j < (max - i - 1); j++)
{
if (strcmp(str[j], str[j + 1])>0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
disp();
}
void qsort()
{
qs(reinterpret_cast<char **>(str), 0, max - 1);
disp();
}
void qs(char **str, int st, int e)
{
int pi = part(reinterpret_cast<char **>(str), st, e);
qs(str, st, pi - 1);
qs(str, pi + 1, e);
}
int part(char **s, int st, int e)
{
char pi[max], swap[max];
strcpy(pi, s[e]);
int pii = st;
int i = st;
for (; i < e; i++) {
if ((strcmp(s[i], s[pii])) <= 0)
{
strcpy(swap, s[pii]);
strcpy(s[pii], s[i]);
strcpy(s[i], swap);
pii++;
}
}
strcpy(swap, str[e]);
strcpy(str[e], str[pii]);
strcpy(str[pii], swap);
// NO RETURN VALUE?
return 0;
}
};
int main()
{
ss s;
cout << "Enter the strings\n";
s.get_str();
s.disp();
s.sort();
cout << "after the sort" << endl;
s.disp();
return 0;
}
Or, if you don't want to use reinterpret_cast<>(), you can use this:
...
void qsort()
{
qs(str, 0, max - 1);
disp();
}
void qs(char str[max][10], int st, int e)
{
int pi = part(str, st, e);
qs(str, st, pi - 1);
qs(str, pi + 1, e);
}
int part(char s[max][10], int st, int e)
...
#include<iostream>
using namespace std;
class darray
{
private:
int n; // size of the array
int *a; // pointer to the 1st element
public:
darray(int size)
{
n = size;
a = new int[n];
}
~darray(){ delete[] a; }
void get_input();
int get_element(int index);
void set_element(int index, int value);
int count(){ return n; }
void print();
};
void darray::get_input()
{
for (int i = 0; i < n; i++)
{
cin >> *(a + i);
}
}
int darray::get_element(int index)
{
if (index == -1)
index = n - 1;
return a[index];
}
void darray::set_element(int index,int value)
{
a[index] = value;
}
void darray::print()
{
for (int i = 0; i < n; i++)
{
cout << a[i];
if (i < (n - 1))
cout << " ";
}
cout << endl;
}
// perform insertion sort on the array a
void insertion_sort(darray d)
{
int v = d.get_element(-1); // v is the right-most element
int e = d.count() - 1; // pos of the empty cell
// shift values greater than v to the empty cell
for (int i = (d.count() - 2); i >= 0; i--)
{
if (d.get_element(i) > v)
{
d.set_element(e,d.get_element(i));
d.print();
e = i;
}
else
{
d.set_element(e, v);
d.print();
break;
}
}
}
int main()
{
int s;
cin >> s;
darray d(s);
d.get_input();
insertion_sort(d);
system("pause");
return 0;
}
I use the darray class to make a array of size n at runtime. This class gives basic functions to handle this array.
This programs says debugging assertion failed at the end.
It gives this error after ruining the program.Other than that the program works fine. What is the reason for this error ?
You need to declare and define a copy constructor:
darray::darray(const darray& src)
{
n = src.n;
a = new int[n];
for (int i = 0; i < n; i++)
{
*(a + i) = *(src.a + i);
}
}