I'm trying to get a head start on some homework, and the functions we're meant to write are a bit more complicated than the ones I've done before. I'm hoping for a bit of advice on how I can understand and approach these. Thank you very much.
1. arrNumBetween - Given an int array and its length, a low value, and a high value, return the number of values in the array greater (or equal to) low, and less than high. An empty array should return 0.
(My thoughts: Have the code use a for loop to compare array[i] to the values set for low and high, and then an if statement so that if the value matches the conditions, count++. finally display count?)
2. arrReverse - Given an int array and its length, reverse the array in-place. Because arrays act like pass-by-reference, if you reverse the order of the given array, the array that was originally sent will also get reversed. This returns nothing (void).
(My thoughts: No idea for this one!)
3. arrFind - Given an int array, its length, a value, and a starting position, return the position greater than or equal to the starting position in the array that matches the value. If no starting position is given, use 0. If the given value is not in the array, return -1.
(My thoughts: Also no idea for this one, I'm actually not entirely sure what it's even asking me to do)
I'm hoping for a bit of advice on how I can understand and approach these.
Write some pseudo-code. Write in English as comments in your file the steps you need to take to do the task. It can start out very general, go back and refine what needs to be done as many times as necessary. When you are ready convert the comments to code. You may want to leave some of the comments in, as a form of documentation.
If you are confused about what needs to be done, think about how you would do it with pen and paper. Actually write down some data values on the paper. For the example of finding a value in an array (Q3), you can see straight away what the answer is, but you need to break it down into the logical steps required to arrive at that answer.
With Q2, a temporary array could come in handy, along with a for loop that goes backwards.
I've completed the first function and I'm now trying to write the second, but I'm running into a couple of problems I don't understand
1 2 3 4 5 6 7 8 9 10 11
int arrReverse(int array[], int length){
int temp;
for(int i = 0; i < (length/2); i++){
temp = array[i];
array[i] = array[(length - 1) - i];
temp = array[(length - 1) - i];
}
for(i = 0; i < length; i++)
return array[i];
}
}
errors:
stats.cpp:143:7: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
for(i = 0; i < length; i++)
^
stats.cpp:143:7: note: (if you use ‘-fpermissive’ G++ will accept your code)
stats.cpp: At global scope:
stats.cpp:146:1: error: expected declaration before ‘}’ token
}
^
stats.cpp: In function ‘int arrReverse(int*, int)’:
stats.cpp:145:3: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make: *** [stats-sandbox.o] Error 1
The i on line 3 is a different one compared to the one on line 8. This is because new scope is introduced with braces. So make line 8: for(int i = 0; i < length; i++) { or use a a different variable name.
If your editor does not do braces for you, then consider typing both the opening and closing brace, then go back and put in what goes inside them. That way you will never have mismatching braces, or anything else that comes in pairs for that matter.
The return is not a good thing in the for statement. It only happens once. The function could be void, you are altering the original array.
TheIdeasMan wrote:
With Q2, a temporary array could come in handy, along with a for loop that goes backwards.
So my pseudo code for this would have been:
1 2 3
// Starting at end of the array, work backwards.
// Write the values to temp array, the array is now reversed.
// Use values in temp array to overwrite the values in original array