Function not found in C++ - c++

I am using sublime text 2 and I am trying to program bubble sort, and every time I run the code below it gives me an error on bubbleSort(num[5], terms); the error is
ERROR: no matching function for call to 'bubbleSort'.
Can anyone tell me why this is happening.
The code is:
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int term) {
for(int i = 0; i < term; ++i) {
for(int index = 0; index < term-i-1; ++index) {
if(arr[index] < arr[index + 1]) {
int swap;
swap = arr[index];
arr[index] = arr[index + 1];
arr[index + 1] = swap;
}
}
}
for(int counter = 0; counter < term; counter++) {
cout << arr[counter] << endl;
}
}
int main() {
cout << "Hi in this program I will do bubble sort" << endl;
cout << "The numbers are 2, 9, 5, 10, 6"<< endl;
int num[5] = {2, 9, 5, 10, 6};
int terms = sizeof (num) / sizeof (num[0]);
bubbleSort(num[5], terms);
//answer = [2, 5, 6, 9, 2, 10]
}

Although a good question should be accompanied by a complete example of the compiler error, this answer should be fine.
void bubbleSort(int arr[], int term) {
// ...
}
It's a function which accepts an array of integers as first parameter and an integer as second one.
When you try to invoke it with:
int num[5] = {2, 9, 5, 10, 6};
// ...
bubbleSort(num[5], terms);
You're passing num[5] which is not an array of integer, but it should be an element of the array num, then an integer itself.
In short you're calling the function passing
bubbleSort(INT, INT);
and not, as requested by the function
bubbleSort(ARRAY_INT, INT);
That' why the compiler doesn't find a function which names bubbleSort and accept two integers.
Additional note
It's a little be out of context, but I want to suggest you to improve your C++ base skills, because the expression:
num[5]
It's perfectly wrong in your code, because it tries to access to the 6-th element in the array (which is composed by only 5 elements), that'll produce an out-of-bound behaviour.

Thanks for helping me now I know that I am supposed to use
bubbleSort(num, terms);

Related

Why binary search is not working on my test?

I have just written a binary search on c++, using arrays, but it isn't working for all of my tests.
#include <iostream>
using namespace std;
int bSearch(int arr[], int item);
int main() {
int testArr[] = {1, 3, 5, 7, 9, 10, 12, 13, 15, 16, 18, 20};
int result = bSearch(testArr, 18);
cout << "The result of binary search is " << result << endl;
return 0;
}
int bSearch(int arr[], int item) {
int start = 0;
int middle(0), guess(0);
int finish = sizeof(arr);
while(start <= finish) {
middle = (start + finish) / 2;
guess = arr[middle];
if(guess == item)
return middle;
else if(guess > item)
finish = middle - 1;
else
start = middle + 1;
}
return -1;
}
Can you explain me, why is it so?
In bSearch, the parameter arr is not an array, but a pointer to an int. There is no information on if it points to an array of ints or the number of elements may be part of such an array.so sizeof(arr) will be the size of a pointer (typically 4 or 8).
You'll need to pass number of elements the array holds to bSearch, or use one of the standard containers that track the size (std::vector or std::array).

method on c++ compared to java (syntax from java to c++)

i start to code on c++, i have good background from java, i have some issue with the syntax of C++.
i stuck on one thing, i create a method "SortCloums" , and i missing something on it. in the for loop its give me a massage of :
expression must have pointer-to-object type
this is the short code of my, yes i still learning about pointers. need some direction of you guys.
#include <iostream>
using namespace std;
const int size = 4;
void SortCloums(int arr[size][size], int sizeOfArray);
int main(){
int arr[size][size] = { { 0, 4, 6, 0 },
{5, 6 , 8, 12},
{50, 8, 12, 24},
{900, 10, 30, 50} };
SortCloums(arr, size);
return 0;
}
void SortCloums(int arr ,int sizeOfArray) {
// now we check if the array is column sorted or not.
bool flag = true;
for (int i = 0; i < sizeOfArray && flag != false; i++){
for (int j = 0; j < sizeOfArray && flag != false; j++){
if (arr[j][i] > arr[j+1][i]){
flag = false;
std::cout << "The array of column unsorted" << endl;
std::cin.get();
}
}
}
if (flag == true){
std::cout << "The array is column sorted" << endl;
std::cin.get();
}
}
I guess the error message points to this line:
if (arr[j][i] > arr[j+1][i]){
This code treats arr as a 2-D array. However arr is declared here in an incorrect way:
void SortCloums(int arr ,int sizeOfArray) {
Because the declaration comes before usage, the compiler thinks that the declaration is correct, and usage is incorrect. So it outputs an error message that is not very clear.
To fix, declare arr correctly:
void SortCloums(int arr[size][size], int sizeOfArray) {
Here arr is conceptually a 2-D array, but for various technical reasons it's formally a pointer to an array. Your error message mentions a "pointer-to-object" type; a "pointer to array" is a special case of this, so this fixes the error.

Rotate array to right instead of left

I have some code that rotates a number array to the left but instead, I need it to rotate it to the right. There is other code online that rotates array to the right but that code lets you only rotate numbers in the middle of the array.
I have tried decrementing the loops differently & and changing where its initialized but doesn't seem to rotate the correct way.
Expected output: if array is this {1, 2, 3, 4, 5, 6, 7}. Then it should look like: {7, 1, 2, 3, 4, 5, 6}
Current output: {2, 3, 4, 5, 6, 7, 1}
#include <iostream>
using namespace std;
/*Function to left Rotate arr[] of size n by 1*/
void leftRotatebyOne(int arr[], int n);
/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
int i;
for (i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n-1; i++)
arr[i] = arr[i+1];
arr[i] = temp;
}
/* utility function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
cout << arr[i] << " ";
}
/* Driver program to test above functions */
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
printArray(arr, 7);
leftRotate(arr, 1, 7);
cout << "___" << endl;
printArray(arr, 7);
getchar();
return 0;
}
leftRotateByOne is the key function here. The others can stay the same. Have a look at what it is doing, preferably with a pen and paper to keep track of the operations:
Keeps a copy of the first element.
Moves all elements to the "left" (that is, to the element with index
one less), being careful not to overwrite anything you need later.
Puts the first element in the last place.
So you need to do the opposite:
Keep a copy of the last element.
Moves all elements to the "right" (that is, to the element with index
one more), being careful not to overwrite anything you need later.
Puts the last element in the first place.
For example:
void rightRotatebyOne(int arr[], int n)
{
int i, last;
last = arr[n-1];
for (i = n-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = last;
}

Comparing palindromes using different functions

I am trying to call the functions so that it prints out if the array is a palindrome or not. Please tell me what I am doing wrong; it seems I am not passing the arguments correctly. If I made a mistake with the post, please let me know; I am fairly new to the website.
This is how the output is supposed to look:
This is what my output looks like:
This is my code:
#include <stdio.h>
void createReverseArray();
void printArray();
void compareArray();
int main()
{
int MyArray1[] = {1, 2, 3, 2, 1};
int MyArray2[] = {1, 2, 3, 4, 1};
int MyArray3[] = {1, 2, 3, 3, 2, 1};
int MyArray4[] = {1, 2, 3, 4, 2, 1};
int n = 5, i, j, n, temp;
createReverseArray(MyArray1[5]);
createReverseArray(MyArray2[5]);
createReverseArray(MyArray3[5]);
createReverseArray(MyArray4[5]);
compareArray(MyArray1[5]);
compareArray(MyArray2[5]);
compareArray(MyArray3[5]);
compareArray(MyArray4[5]);
printArray(MyArray1[5]);
printArray(MyArray2[5]);
printArray(MyArray3[5]);
printArray(MyArray4[5]);
}
int createReverseArray(int &a[], int n)
{
i = 0;
j = n - 1;
while(i<j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
return reverse = a[];
}
int compareArray(int &a[], int reverse)
{
if(a[] == reverse) {
printf("The array is a palindrome")
}
else {
return 0;
}
}
void printArray(&a[])
{
printf("Array elements are:%d", a[]);
compareArray(a[]);
}
I am not sure due to the amount of uncertainties I have with the provided code, but if your objective is simply to detect whether the array is a palindrome, it is exceedingly simple and can be done in a single function:
bool isPalindrome(int[] list, int size)
{
for(int k = 0; k < size / 2 - 1; k++)
if(list[k] != list[size - k - 1])
return false;
return true;
}
This will iterate half your array, comparing it to the second half and exiting the instant one value is not equal to its mirror.
As for the mistakes in your code, I might be able to point out some of the more obvious ones, for future reference:
Calling functions with incorrect number of arguments.
Example: createReverseArray(MyArray1[5]).
Calling passing integers in place of arrays as arguments. Example:
MyArray[5] is the integer in the sixth position of MyArray.
Ignoring scope and using variables without declaring them. Example: i and j from main being used in createReverseArray. THIS DOES NOT WORK.
Comparing an integer to an array pointer. Example: if(a[] == reverse). No matter what you were trying to achieve, this will never give you a useful result.
Declaring different functions in your forward declaration and in your implementation. Example: void createReverseArray(); and int createReverseArray(int &a[], int n). You should declare them wit the same arguments and return values or the compiler will not understand they're the same thing.

Sorting an array to another array C++

My program have to sort an array in another array.
When I run the program it prints 1 2 3 -858993460 5 -858993460 7.
I can not understand where the mistake is in the code.
#include <iostream>
using namespace std;
int main()
{
const int N = 7;
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int max = arr[0];
for (int i = 1; i < N; i++)
{
if (max < arr[i])
max = arr[i];
}
int sort_arr[N];
for (int j = 0; j < N; j++)
{
sort_arr[arr[j] - 1] = arr[j];
}
for (int i = 0; i < N; i++)
{
cout << sort_arr[i] << " ";
}
return 0;
}
Okay lets face the problems in your code.
The "weird" numbers you see there, came from the uninitialzied array sort_arr. What do I mean by uninitialized? Well sort_arr is a little chunck somewhere in your memory. Since a program usually does not clear its memory and rather claims the memory it used as free, the chunk of sort_arr may contain bits and bytes set by another program. The numbers occure since these bytes are interpreted as an integer value. So the first thing to do would be to initialize the array before using it.
sort_arr[N] = { 0, 0, 0, 0, 0, 0, 0 };
Now why did these numbers occure? Well you're probably expecting your algorithm to set all values in sort_arr which would result in an sorted array, right? Well but your algorithm isn't working that well. See this line:
sort_arr[arr[j] - 1] = arr[j];
What happens when j is 1? arr[1] is then evaluated to 17 and 17 - 1 equals 16. So sort_arr[arr[1] - 1] is the same as sort_arr[16] which exceeds the bounds of your array.
If you want to program a sorting algorithm by your self than I would recommend to start with an simple bubble sort algorithm. Otherwise, if you only need to sort the array have a look at the algorithm header. It is fairly simple to use:
#include <iostream>
#include <algorithm>
#include <iterator> // << include this to use begin() and end()
using namespace std;
int main()
{
const int N = 7;
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int sort_arr[N] = { 0, 0, 0, 0, 0, 0, 0 };
copy(begin(arr), end(arr), begin(sort_arr));
sort(begin(sort_arr), end(sort_arr));
for (int i = 0; i < N; i++)
{
cout << sort_arr[i] << " ";
}
cout << endl;
}
By the way. You're looking for the biggest value in your array, right? After you have sorted the array sort_arr[N - 1] is the biggest value contained in your array.
If you want to sort a array into another array then one way is you make a copy of the array and then use the sort function in the standard library to sort the second array.
int arr[10];
int b[10];
for(int i=0;i<10;i++)
{
cin>>arr[i];
b[i]=arr[i];
}
sort(b,b+10);
// this sort function will sort the array elements in ascending order and if you want to change the order then just add a comparison function as third arguement to the sort function.
It seems that you think that sort_arr[arr[j] - 1] = arr[j] will sort arr into sort_arr. It won't.
Sorting is already written for you here: http://en.cppreference.com/w/cpp/algorithm/sort You can use that like this:
copy(cbegin(arr), cend(arr), begin(sort_arr));
sort(begin(sort_arr), end(sort_arr));
Live Example
My guess is this is an attempt to implement a type of counting sort. Note that variable length arrays aren't normally allowed in C++ or some versions of C. You could use _alloca() to allocate off the stack to get the equivalent of a variable length array: int * sort_arr = (int *)_alloca(max * sizeof(int)); .
#include <iostream>
using namespace std;
int main()
{
const int N = 7;
// assuming range of values is 1 to ...
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int max = arr[0];
for (int i = 1; i < N; i++)
{
if (max < arr[i])
max = arr[i];
}
int sort_arr[max];
for (int i = 0; i < max; i++)
{
sort_arr[i] = 0;
}
for (int j = 0; j < N; j++)
{
sort_arr[arr[j] - 1]++;
}
for (int i = 0; i < max; i++)
{
while(sort_arr[i])
{
cout << i+1 << " ";
sort_arr[i]--;
}
}
return 0;
}