For loop using pointers to traverse array not working properly - c++

For my homework problem I must use pointers to traverse arrays. When I try to store 3 "name" values into member variables of an array of an Object called RentalAgency, I find that it stores the value, but never increments. Therefore the last value given is stored in the first index and the next two are empty.
RentalAgency *agencies_ptr = agencies;
for(int i = 0; i < 3;i++,++agencies_ptr){
infile.get((agencies->name),MAX_SIZE,space);
}
Where agencies is an array of Objects
If the input is Hertz, Alamo, and Budget, it should output Hertz, Alamo, and Budget.
The actual output is just Budget.

Just write
for(int i = 0; i < 3; i++){
infile.get( agencies_ptr[i].name, MAX_SIZE, space );
}

You are desreferencing agencies, not agencies_ptr (and the parenthesis are not needed):
RentalAgency *agencies_ptr = agencies;
for(int i = 0; i < 3; ++i, ++agencies_ptr)
infile.get(agencies_ptr->name, MAX_SIZE, space);
But a more idiomatic way of traversing a "range" is this (it stands for iterator):
RentalAgency *agencies_it = agencies;
RentalAgency *agencies_end = agencies_it + 3;
for(; agencies_it != agencies_end; ++agencies_it)
infile.get(agencies_it->name, MAX_SIZE, space);
It's cleaner, express intent better and is more familiar to see among experienced programmers.

Related

How to return 2D Array without causing Segmentation Fault (C++)

A homework assignment for an introductory C++ course.
The task is to search a two dimensional array for two numbers adding up to a given number 'search_sum.' Enter all found addends as {startRow,startColumn,endRow,endColumn} in the array 'summations,' then return the array 'summations'.
The line with summations[*sums_found] = new size_t[kIndices_size]; in the if statement was written by the instructor, as well as the comment. I am under the impression that this allocates a new space in memory where data can be assigned to and stored, so, in the next line of code, I attempted to take the column and row variables from the for loop and place them in the newly allocated memory as such.
summations[*sums_found]={startRow, column, endRow, column};
This threw a 'too many assignments' and 'segmentation fault' error.
I assumed you just could not do it this way, and data must be added to a 2D array in another fashion. I removed the faulty assignment code and ran the code as shown below through a debugger, just out of curiosity. The error thrown was once again a Segmentation Fault.
// Parameters:
// - matrix: a two-dimension integer array
// - matrix_size: a two-element size_t array storing sizes of matrix
// - search_sum: the integer value for which the function seeks sums
// - sums_found: an OUTPUT PARAMETER pointing to a size_t
//
const size_t** FindSum(const int** matrix,
const size_t* matrix_size,
int search_sum,
size_t* sums_found) {
*sums_found = 0; // init sums found to 0
size_t summations_size = 2; // start assuming no more than 2 summations
// build structure to hold all found summations
size_t** summations = new size_t*[summations_size];
switch (search_sum)
{
case -92:{ //column search
for(size_t column = 0; column < matrix_size[1]; column++){
for(size_t startRow = 0; startRow < matrix_size[0]; startRow++){
for(size_t endRow = 0; endRow < matrix_size[0]; endRow++){
int j = matrix[startRow][column];
int k = matrix[endRow][column];
int sum = j + k;
if(sum = search_sum){
summations[*sums_found] = new size_t[kIndices_size]; // only done when summation is found
*sums_found++;
}
}
}
}
}
break;
case 60:{ //row search
for(size_t row = 0; row < matrix_size[0]; row++){
for(size_t startColumn = 0; startColumn < matrix_size[1]; startColumn++){
for(size_t endColumn = 0; endColumn < matrix_size[1]; endColumn++){
int j = matrix[row][startColumn];
int k = matrix[row][endColumn];
int sum = j + k;
if(sum = search_sum){
summations[*sums_found] = new size_t[kIndices_size]; // only done when summation is found
*sums_found++;
}
}
}
}
}
break;
case 1203:{ //desc/ascending diagonal
}
break;
case 412:{ //single entry
}
break;
default:{ //large array
}
break;
}
return const_cast<const size_t**>(summations);
}
I did not know what this was, so I researched the error and found that you are not allowed to perform a read/write action on read-only code, which makes sense. What I do not understand is what exactly makes this code read-only, when it seems like its function is to assign a new space for data to be assigned to, which (to me), sounds like a 'write-like' action? I more than likely am misunderstanding the full scope of the codes function, and I am further confused with as to how I should go about assigning the data to the summations array.
Also, this is a university course taught by a grad student who is (seemingly) less than well versed in c++. It is a very 'teach yourself' type class. I understand that this assignment is an exercise on pointers and references, but it feels like I am very poorly equipped to solve a problem like this, and I am unsure what exactly to research and study independently to improve my knowledge of this particular topic. Please, if you can tell what it is I am struggling with just by looking at the code I've written, let me know what I should prioritize my studying on.

Radix Sort using C++

Suppose I have bunch of numbers. I have to first put the least significant digit into the corresponding bucket. Ex: 530 , I have to first put into the bucket 0. For number 61, I have to put into bucket 1.
I planned to use a multidimensional array to do this. So I create a 2-dimenional array, which nrows is 10 ( for 0~ 9) and ncolumns is 999999 ( because I don't know how large will the list be):
int nrows = 10;
int ncolumns = 999999;
int **array_for_bucket = (int **)malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
array_for_bucket[i] = (int *)malloc(ncolumns * sizeof(int));
left = (a->value)%10;
array_for_bucket[left][?? ] = a->value;
Then I created one node call a. In this node a, there is a value 50. To find out which bucket I want to put it in, I calculate "left" and I got 0. So I want to put this a-> value into bucket 0. But now I am stuck. How do I put this value into the bucket? I have to use a pointer array to do this.
I thought for a long time but still couldn't find a good way to do it. So please share some ideas with me. thank you!
There is a much easier way of doing this, and instead of radix*nkeys space you only need an nkeys-sized buffer.
Allocate a second buffer that can fit nkeys keys. Now do a first pass through your data and simply count how many keys end up in each bucket. You now can create a radix-sized array of pointers where each pointer is to the start of that bucket in the output buffer. Finally, the second pass though the data moves the keys. Every time you move a key, increment that bucket pointer.
Here's some C code to make into C++:
void radix_sort(int *keys, int nkeys)
{
int *shadow = malloc(nkeys * sizeof(*keys));
int bucket_count[10];
int *bucket_ptrs[10];
int i;
for (i = 0; i < 10; i++)
bucket_count[i] = 0;
for (i = 0; i < nkeys; i++)
bucket_count[keys[i] % 10]++;
bucket_ptrs[0] = shadow;
for (i = 1; i < 10; i++)
bucket_ptrs[i] = bucket_ptrs[i-1] + bucket_count[i-1];
for (i = 0; i < nkeys; i++)
*(bucket_ptrs[keys[i] % 10]++) = keys[i];
//shadow now has the sorted keys
free(shadow);
}
But I may have misunderstood the question. If you are doing something a little different than radix sort, pleas add some details.
Look the Boost Pointer containers library if you want to store pointers.
C++ isn't my forte but this code from wikipedia-Raidx Sort is very comprehensive and probably is more C++-ish than what you've implemented so far. Hope it helps
This is C++, we don't use malloc anymore. We use containers. A two-dimensional array is a vector of vectors.
vector<vector<int> > bucket(10);
left = (a->value)%10;
bucket[left].push_back(a->value);

Remove specific items from a listbox in MFC

CString dance[] = {L"Atb", L"Tiesto", L"Madonna", L"Paul van Dyk", L"Armin van Burren", L"Jennifer Lopez"};
for(int i = 0; i < m_ItemsListBox.GetCount(); ++i)
{
CString item;
int length = m_ItemsListBox.GetTextLen(i);
m_ItemsListBox.GetText(i, item.GetBuffer(length));
for(int j = 0; j < sizeof(dance)/sizeof(*dance); ++j)
{
if(item != dance[j])
{
m_ItemsListBox.DeleteString(i);
}
}
}
I'm trying to remove from a listbox(m_ItemsListbox) all elements that are not part from the CString array. But how I coded it doesnt work, because if the first element its different it will delete it without searching the entire array.
Doesn't seemed like a hard task but I really have no idee how to do it. I think one way should be to use a CList instead of the array because it has a find() method, but I don't like it because I have to manually add all the elements, do you guys have another idee? Or the CList solution is the only one ?
Sorry, I'm a MFC begginer. Thanks for your time.
Hmmm I wouldn't be comfortable deleting things from the list box while iterating through the items in the listbox seems to be asking for problems down the line.
Honestly you could do something like this, I've just whipped together - construct a list of all the item indexes you want to remove and remove them at the end.
CList<int, int> ListIndexItemsToRemove;
for(int i = 0; i < m_ItemsListBox.GetCount(); ++i)
{
CString item;
int length = m_ItemsListBox.GetTextLen(i);
m_ItemsListBox.GetText(i, item.GetBuffer(length));
bool isMatchFound = false;
for(int j = 0; j < sizeof(dance)/sizeof(*dance); ++j)
{
if(item == dance[j])
{
isMatchFound = true;
}
}
if (!isMatchFound)
ListIndexItemsToRemove.AddHead(i);
}
for(int i = 0; i < ListIndexItemsToRemove.GetCount(); ++i)
m_ItemsListBox.DeleteString(ListIndexItemsToRemove.GetAt(ListIndexItemsToRemove.FindIndex(i));
but - it may be better to clear the whole list and refill it everytime something changes, as Martin says (if it doesn't affect anything.
For dynamic lists I tend to store data in its own variable and clear/re-populate the list when that data changes. Especially as the list gets bigger re-adding the strings tends to be much faster than doing searches through the list and/or original source.

Invalidating loop bounds

I've recently inherited a project primarily done in C++, so this is my first real exposure to it. I'm wondering if I may have a problem erasing a vector's elements from within a loop bounded by the vector's begin() and end().
Here's (essentially) what I've been trying to do:
for (vector<double>::iterator i = distance.begin(); i < distance.end(); i++) {
for (vector<double>::iterator j = i + 1; j < distance.end(); j++) {
/* code to assemble *i, *j, and some other values, say *x & *y
(also iterators) assumed to be in the distance vector */
vector< vector<double >::iterator remove;
remove.push_back(i); remove.push_back(j);
remove.push_back(x); remove.push_back(y);
sort(remove.begin(), remove.end());
for (int r = remove.size() - 1; r >= 0; r--) {
distance.erase(remove.at(r));
}
}
}
For what I'm testing it on, this appears to work. However, I'm concerned that's just because of a fluke and this solution shouldn't be used. Does distance.end() get reset at the beginning of each loop, or does C++ just check with the initial value?
for-loop will evaluate i < distance.end() on each loop. The problem is in distance.erase, it will invalidate i, so the result of i++ is undefined.
distance.end() is always accurate to the current state of the vector.
for() loops always re-evaluate the condition on every loop.

Template Sort In C++

Hey all, I'm trying to write a sort function but am having trouble figuring out how to initialize a value, and making this function work as a generic template. The sort works by:
Find a pair =(ii,jj)= with a minimum value = ii+jj = such at A[ii]>A[jj]
If such a pair exists, then
swap A[ii] and A[jj] else
break;
The function I have written is as follows:
template <typename T>
void sort(T *A, int size)
{
T min =453;
T temp=0;
bool swapper = false;
int index1 = 0, index2 = 0;
for (int ii = 0; ii < size-1; ii++){
for (int jj = ii + 1; jj < size; jj++){
if((min >= (A[ii]+A[jj])) && (A[ii] > A[jj])){
min = (A[ii]+A[jj]);
index1 = ii;
index2 = jj;
swapper = true;
}
}
}
if (!swapper)
return;
else
{
temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
sort(A,size);
}
}
This function will successfully sort an array of integers, but not an array of chars. I do not know how to properly initialize the min value for the start of the comparison. I tried initializing the value by simply adding the first two elements of the array together (min = A[0] + A[1]), but it looks to me like for this algorithm it will fail. I know this is sort of a strange type of sort, but it is practice for a test, so thanks for any input.
most likely reason it fails, is because char = 453 does not produce 453 but rather different number, depending what char is (signed versus unsigned). your immediate solution would be to use numerical_limits, http://www.cplusplus.com/reference/std/limits/numeric_limits/
you may also need to think about design, because char has small range, you are likely to overflow often when adding two chars.
The maximum value of any type is std::numeric_limits<T>::max(). It's defined in <limits>.
Also, consider a redesign. This is not a good algorithm. And I would make sure I knew what I was doing before calling my sort function recursively.
I haven't put too much time reading your algorithm, but as an alternative to std::numeric_limits, you can use the initial element in your array as the initial minimum value. Then you don't have to worry about what happens if you call the function with a class that doesn't specialize std::numeric_limits, and thus can't report a maximum value.