reset password
Author Message
jyoung18
Posts: 30
Posted 16:04 Jan 16, 2015 |

So I'm a little confused with the assignment. The way I was doing it was by passing in the original array as a const through a function and creating a copy in the function then printing out the changes to the copied array. The assignment says that all operations have to be performed on the original array. Does that mean I can't do this?

For example my third method heading:

void evenZeros(const int myArray[], int length) {

int evenZeroArray[length] = copy(myArray);

//stuff to get the even zeros

print(evenZeroArray, length)

}

Is this ok or not?

redge
Posts: 31
Posted 16:07 Jan 16, 2015 |

Whether it's allowed under the assignment definition seems somewhat less relevant to me than why you're trying to do it that way in the first place. Why increase computation time by duplicating the array, then overwriting values? Why not write a function that reads values from myArray and stores the results in evenZeroArray, which would both satisfy the criteria and execute more efficiently?

jyoung18
Posts: 30
Posted 16:13 Jan 16, 2015 |

The function actually does write them to evenZeroArray but I was trying to post without putting that code there.

redge
Posts: 31
Posted 16:14 Jan 16, 2015 |

Sure, but you start out by cloning myArray into evenZeroArray, then overwriting the values in evenZeroArray. Why not just perform your computations on the values in myArray directly, and write the output to evenZeroArray?

jyoung18
Posts: 30
Posted 16:16 Jan 16, 2015 |

Let me be more clear, I actually don't copy the array into evenZeroArray.

void evenZeros(const int myArray[], int length) {
    int evenZeroArray[length];
    for(int i = 0; i < length; i++) {
            // stuff to put zeros AND myArray values into evenZeroArray
    }

    cout << "Method 03: ";
    print(evenZeroArray, length);
}

redge
Posts: 31
Posted 17:07 Jan 16, 2015 |

Your initial post contained this:

int evenZeroArray[length] = copy(myArray);

Which indicates cloning the array before performing any operations on it. Your new code looks like you're not initializing evenZeroArray, so as long as inside your for loop you're reading values from myArray, then sticking the final results in evenZeroArray, you should be fine.

 

kknaur
Posts: 540
Posted 17:09 Jan 16, 2015 |

Just to clarify, the assignment is mainly about practicing with arrays in c++.  When you get the results for a method it should be for the original configuration of the array.  You can handle this in one of two ways, make a copy of the original for each method, or for some of the methods you can just print out the results.