2

I am failing to reach expected output when testing my 'grow'and 'subArray' functions. I've tried dereferencing back and forth in the function and also in main(). I'm wondering if there's something wrong with my memory allocation that is causing the lapse. I am extremely stuck and was hoping someone could potentially see something that I am missing, thanks.

#include <iostream>
#include <iomanip>
using namespace std;

bool isSorted(int *arr, int size){
    for(int index = 0; index < size - 1; index ++){
        if(*(arr + index) > *(arr + index + 1)){
            return false;
        }

    }
    return true;
}


double chain (int totalInches, int *feet, int *inches){
    *feet = totalInches/12;
    *inches = totalInches%12;
    return *(feet)*3.49 + *(inches)*.30;
}

int *grow (int *arr, int size){
    int *newArray;
    newArray = new int[size*2]; //alocate new array
    for(int i = 0; i < size*2; i+=2){
        *(newArray + i) = *(arr+i);
        *(newArray + i + 1) = *(arr+i);    
    }
    return newArray;

}

int *duplicateArray (int *array, int size) {

    int *newArray;
    if (size <= 0)
    return NULL;
    newArray = new int [size]; //allocate new array
    for (int index = 0; index < size; index++){
        newArray[index] = array[index]; //copy to new array
    }
    return newArray;
}

int *subArray( int *array, int start, int length){
    int *result = duplicateArray(array,5);
    return result;
}

void showArray( int *arr, int size){
    for(int  i = 0; i < size; i ++)
    {
        cout << *(arr + i) << " ";
    }
}
int main(){

    int size = 8;
    int testArray[] = {1,2,3,4,5,6,7,8};
    cout << "testing isSorted: " << endl;
    cout << "test data array 1: ";
    showArray(testArray, size);
    cout << endl;
    cout << "Expected result: true" << endl;
    cout << "Actual result: " << boolalpha << isSorted(testArray, size);
    cout << endl;
    int testArray2[]= {8,7,6,5,4,3,2,1};
    cout << "test data array 2: ";
    showArray(testArray2, size);
    cout << endl;
    cout << "Expected result: false" << endl;
    cout << "Actual result: " << boolalpha << isSorted(testArray2, size);
    cout << endl << endl << endl;
    int chainTest = 53;
    cout << "Checking chain for 53 inches: " << endl;
    cout << "Expected result: 15.46 " << "  " << "feet: 4 " <<
    "  " << "inches: 5"<< endl;
    int in;
    int ft;
    cout << "Actual results : " << chain(chainTest,&ft,&in);
    cout << "   " << "feet: " << ft << "   " << "inches: " << in << endl;
    cout << endl << endl;
    cout << "testing grow: " << endl;
    cout << "test data 1: ";
    showArray(testArray, size);
    cout << endl;
    cout << "Expected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 " << endl;
    cout << "Actual results: " << *(grow(testArray, size));
    cout << endl << endl;
    cout << "testing subArray:" << endl;
    cout << "test data: ";
    showArray(testArray, size);
    cout << endl;
    int start = 5;
    int length = 3;
    cout << "start: " << start << " " << "length: " << length << endl;
    cout << "Expected result: " << "6 7 8" << endl;
    cout << "Actual result:   " << *(subArray(testArray, start, length));
    cout << endl;

    return 0;
}

Output:

Here is the output I am getting

As you notice, the loop is terminating after one traversal. The grow function is intended to duplicate and expand. In other words, it's supposed to make a copy of itself and append as it traverses. Any ideas as to why I am getting hung on the first element of the array?

4
  • cout << "Actual results: " << *(grow(testArray, size)); You're dereferencing the first element of the array when what you really want is to show the entire array. Also your subarray function calls the duplicate array function, which always starts at index 0. You may want to write new code for the sub-array copy. You could use memcpy(). Commented Feb 28, 2017 at 4:03
  • Thank you! Good point. I actually didn't notice that I wasn't actually outputting the entire array. No wonder! As far as the subArray, that was supplied. So, no changing that. I called both of the functions to make them do their work followed by the display function. I got a better looking output but not quite there yet. It doesn't seem to be duplicated as intended in the grow() function. Commented Feb 28, 2017 at 4:54
  • Off topic: the code leaks memory. That buffer allocated in grow and returned is never assigned to anything so it's next to impossible for you to find it again and delete[] it. Commented Feb 28, 2017 at 5:45
  • Thank you guys very much! I will be sure to handle the leaks as well. Commented Feb 28, 2017 at 5:53

2 Answers 2

1

You are actually doubling the array but only the first element is being printed because you are dereferencing an int* . To print all the elements, write a loop and print all the elements.

Also there is so much memory leak here. Please free memory after you are done using it. You are read about the delete[] operator.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, sir! I will be sure to address the memory leaks.
1

Your loop going two at a time is good but it prevents you from selecting every element in the original array causing you to skip the even numbers. check your for loop and consider using two counters or if you want to modify your for loop to

for(int i = 0; i < size*2; i+=2){
    *(newArray + i) = *(arr+i/2);
    *(newArray + i + 1) = *(arr+i/2);    
}

to ensure every element is reached

also as stated in the comments, use the showArray method you implemented

showArray(grow(testArray, size),size*2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.