I am trying to send a vector into a bubbleSort function to organize numbers from max to min as they are produced one by one, but I am getting the "C2100: Illegal indirection" warning. Can someone please help me out?
private: void bubbleSort(vector<int> &matrixPtr)
{
int temp;
int numLength = *matrixPtr.size( );//length of vector
for (int i = 1; (i <= numLength);i++)
{
for (int j=0; j < (numLength -1); j++)
{
if (*matrixPtr[j+1] > *matrixPtr[j])
{
temp = *matrixPtr[j];//Swap elements
*matrixPtr[j] = *matrixPtr[j+1];
*matrixPtr[j+1] = temp;
}
}
}
}
The bubbleSort is drawn from another function ahead of it:
bubbleSort(&output);//pass to bubble sort
for (int rows=0;rows<creation->getZeroRows();rows++)
{
for (int cols=0;cols<creation->getCols();cols++)
{
txt_DisplayRowSum->Text= String::Concat(txt_DisplayRowSum->Text, (*creation->zeroArrayPtr)[rows][cols]," ");
}
txt_DisplayRowSum->Text+=" \n";
}
Thank you for your help in advance
You are incorrectly using references.
Instead of *matrixPtr.size( ) you need matrixPtr.size(), and everywhere else in the function you do not need the * when referring to matrixPtr. Also, when passing the vector to the function, you should pass just output and not &output.
You should not and can not use references like pointers. While similar, they're different in several important ways. I also recommend this question for a good summary of those differences.
Related
I'm trying to implement a sorting program of numbers, but am getting the following error:
I was able to compile and work the code on an online c++ compiler, but when I must run it via the terminal so after attempting to do so, it won't compile anymore. There's actually nothing wrong with the code itself, since they're just warnings. But I would like to know how to fix them all, please!
The errors come from these parts of my code:
void sort()
{
int i, j;
for (i = 0; i < storage.size()-1; i++)
{
for (j = 0; j < storage.size()-i-1; j++)
{
if (storage[j] > storage[j+1]) // swap the values
{
int temp = storage[j];
storage[j] = storage[j+1];
storage[j+1] = temp;
}
}
}
}
void print()
{
for(int i = 0; i < storage.size(); i++)
cout<<storage[i]<<" ";
cout<<endl;
}
vector<int> getList()
{
return storage;
}
void setList(vector<int> list)
{
storage = list;
}
};
void quickSort(int start, int end)
{
if (start < end)
{
int part = partition(start, end);
quickSort(start, part - 1);
quickSort(start + 1, end);
}
}
void sort()
{
quickSort(0, storage.size() - 1);
}
void print()
{
for(int i = 0; i < storage.size(); i++)
cout<<storage[i]<<" ";
cout<<endl;
}
Any help will be much appreciated. Thank you!
would like to know how to fix them all, please!
The diagnostic says that you are comparing numbers of different signedness. You can fix that by not comparing numbers of different signedness. You can go with using signed type on both sides or unsigned type on both sides.
Aside from the warning, storage.size()-1 is a dangerous operation unless you first enforce that the container is non-empty. If you subtract from a unsigned zero, you get a very large unsigned value. That is not bad by itself, but when you eventually use that large value as subscript, you get undefined behaviour. Simple solution to this is to add +1 to both sides of the comparison: i+1 < storage.size().
I'm trying to fix a SIGSEGV error in my program. I am not able to locate the site of error. The program compiles successfully in Xcode but does not provide me the results.
The goal of the program is to check whether the same element occurs in three separate arrays and return the element if it is more than 2 arrays.
#include <iostream>
using namespace std;
int main()
{
int i = 0 ,j = 0,k = 0;
int a[5]={23,30,42,57,90};
int b[6]={21,23,35,57,90,92};
int c[5]={21,23,30,57,90};
while(i< 5 or j< 6 or k< 5)
{
int current_a = 0;
int current_b = 0;
int current_c = 0;
{ if (i<5) {
current_a = a[i];
} else
{
;;
}
if (j<6)
{
current_b = b[j];
} else
{
;;
}
if (k<5)
{
current_c= c[k];
} else
{
;;
}
}
int minvalue = min((current_a,current_b),current_c);
int countoo = 0;
if (minvalue==current_a)
{
countoo += 1;
i++;
}
if (minvalue==current_b)
{
countoo +=1;
j++;
}
if (minvalue==current_c)
{
countoo += 1;
k++;
}
if (countoo >=2)
{
cout<< minvalue;
}
}
}
I am not getting any output for the code.
This is surely not doing what you want
int minvalue = min((current_a,current_b),current_c);
If min() is defined meaningfully (you really should provide an MCVE for a question like this), you want
int minvalue = min(min(current_a,current_b),current_c);
This will result in the minimum of the minimum of (a and b) and c, i.e. the minimum of all three, instead of the minimum of b and c. The comma operator , is important to understand this.
This seems to be a flag/counter to make a note across loop executions or count something
int countoo = 0;
It can however not work if you define the variable inside the loop.
You need to move that line BEFORE the while.
With this line you do not prevent the indexes to leave the size of the arrays,
that is very likely the source for the segfault.
while(i< 5 or j< 6 or k< 5)
In order to prevent segfaults, make sure that ALL indexes stay small enough,
instead of only at least one.
while(i< 5 && j< 6 && k< 5)
(By the way I initially seriously doubted that or can compile. I thought
with a macro for or it could, but I do not see that. It could be a new operator in a recent C++ standard update which I missed...
And it turns out that it is the case. I learned something here.)
This should fix the segfault.
To achieve the goal of the code I think you need to spend some additional effort on the algorithm. I do not see the code being related to the goal.
I have a struct to assign values to it. But my programm crashs it. Hopefully you can help me.
struct HashEntry{
std::string key; //the key of the entry
bool used; //the value of the entry
int value; //marks if the entry was used before
};
HashEntry *initHashList(int N){
HashEntry* hashList = new HashEntry[N];
for (int i = 0; i <= N; i++){
hashList[i].key = " ";
hashList[i].value = -1;
hashList[i].used = false;
}
for(int i = 0; i <N; i++){
cout<<hashList[i].value<<endl;
}
return hashList;
}
You iterate through one element too many on creation:
for (int i = 0; i <= N; i++){
Shoule be
for (int i = 0; i < N; i++){
It's because with arrays being 0-based, you can't access the element N of an array of the size N, only N-1, but in return also element 0.
Also, to make the code clearer and less error prone, you could use std::array instead of a pure C style array, or even an std::vector to be able to loop through them range based. You might also rething your use of new which should be avoided in most cases. If you don't really need that, I'd change the function to
std::vector<HashEntry> initHashList(int N) {
std::vector<HashEntry> hashList(N, { "", false, -1, }); //Creating vector of N elements
for (const HashEntry& entry : hashList) { //Iterating through the elements
std::cout << entry.value << std::endl;
}
return hashList;
}
I hope this makes it clearer how you can approach such a problem.
This way of creating the vector and looping through it avoids the potential access errors and is easier to read, imo. For more information, search for std::vector, its constructors, and range-based loops.
I am trying to implement merge sort for my algorithm analysis class and every time I run it there is a segmentation fault. I think the problem is when i split the vector in the merge_sort function but I cannot find the problem. Help would be really appreciated guys.
template <typename T>
std::vector<int> merge(std::vector<T>& A,std::vector<T>& B)
{
int a_size = A.size();
int b_size = B.size();
std::vector<int> C(a_size+b_size,0);
//int *c = new int[b_size+a_size];
int i =0,j =0,k=0;
while(i < a_size && j < b_size)
{
if(A[i]<B[j])
{
C[k] = A[i];
k++;
i++;
}
else
{
C[k] = B[j];
k++;
j++;
if(i!=a_size)
{
for(;i<a_size;i++,k++)
{
//copy rest of a to c
C[k] = A[i];
}
}
if(j != b_size)
{
for(;j<b_size;k++,j++)
{
//copy the rest of b to c
C[k] = B[j];
}
}
}
}
return C;
}
// Merge sort implementation
template <typename T>
void merge_sort(std::vector<T>& vector)
{
// TODO implement merge sort
int vector_size = vector.size();
int big_vector_index = 0;
int half_size = (int)vector_size/2;
int remainder = vector_size%2;
std::vector<int> left(half_size,0);
std::vector<int> right(half_size+remainder,0);
for(int l = 0;big_vector_index<half_size;l++,big_vector_index++)
{
left[l] = vector[big_vector_index];
}
for(int m = 0;big_vector_index<vector_size;m++,big_vector_index++)
{
right[m] = vector[big_vector_index];
}
big_vector_index = 0;
merge_sort(left);
merge_sort(right);
vector = merge(left,right);
}
I took a look at your code, and the majority of it is correct from my testing. I don't want to do your coursework for you but maybe a couple of hints in the right direction will help.
For your original question about the segfault that you got, PaulMcKenzie, Jim Lewis, and Tahlil are right, merge_sort() needs a base condition (base case) to check whether or not the recursion should continue, so it doesn't run forever and/or until your computer runs out of memory (which is what is happening in your case). In general, any recursive function should have a base case.
Also, you should take a look at your merge function as well. It has all the parts of a correct merge function, but some parts of it run a bit earlier/more often than you want them too. I don't want to give too much away since it's for class, but if you fix the segfault problem and are still getting strange answers, take a look at it.
So here's what I have so far:
void sortArray(int amountOfScores, int* testScores)
{
for(int i = 0; i < amountOfScores; i++)
{
for(int j = 0; j < amountOfScores-1; j++)
{
if(*(testScores+i) > *(testScores+j+1))
{
int temp = *(testScores+j);
*(testScores+j) = *(testScores+j+1);
*(testScores+j+1) = temp;
}
}
}
for(int i = 0; i < amountOfScores; i++)
{
cout << *(testScores+i) << endl;
}
}
Basically I'm trying to read in however many numbers the user wants to input, then sort them in ascending order. Catch is I have to use pointers and I've never really understood them. This code above works for 3 numbers, however, adding any more causes it to not sort them...I've tried trouble shooting as best I could but without any knowledge in pointers I don't know what I'm looking for.
Thanks for the help!
You problem might be here:
if(*(testScores+i) > *(testScores+j+1))
Did you mean:
if(*(testScores+j) > *(testScores+j+1))
(Note i replaced by j).
btw, in Bubble sort, if there are no swaps, you should break. This will cause a speed up in some cases.
Bubble sort works the same no matter if you are talking an array or a linked list (pointers).
The only catch is that rather than swapping the location of two adjacent items in an array, you are swapping pointer values between two adjacent list elements.
The algorithm is the same.