This code is supposed to create an array called fun, initialize it to the expression fun[i] = 7 * i ^ 2 - 4 * i - 500, process it backwards, output it in range specified by user. My teacher wouldn't accept it and I'm not sure why. Can anyone tell me what seems to be wrong with it? And any suggestions on how to fix it? thanks in advance
update: i think it has to do with the expression but im not sure why its not working
Perhaps your teacher meant something different for "output in range". What you are doing is simply filling up the array with random numbers between 'low' and 'high'. Are you sure this is what your teacher wants?
I was rather thinking that 'low' and 'high' here are indices, and so you would only be printing the array from arr[low] to array[high]. I may be wrong here. Do you have clear instructions from teacher with you, as in, a document with explicit instructions regarding the assignment?
these are the instructions i received:
Create an array named "fun[]", of 20 integers. Initialize it according to the expression:
"fun[i] = 7 * i2 – 4 * i - 500".
where "i" is the index to the array. Process the array backward and output only those elements whose value are positive and within a range as specified by the user.
So you'll need another for-loop, within which you will check if the value in each element is between 'low' and 'high' and positive. If it is, you will print it out, if not, move on to the next element. This for-loop will traverse the array backwards.
In other words, you are NOT printing the entire array backwards, only those elements which hold values between 'low' and 'high' and are positive. The range and reverse functions are unnecessary.
Lastly, why did you make your array of size 10, when the instructions clearly say it should be 20?
oh good catch i changed the 10 to 20. also i am doing something wrong. i thought this would work but it doesn't seem to recognize my high and low values
1 2 3 4 5
for (int i = 0; i < SIZE; i++) {
if (fun[i] > low && fun[i] < high) {
printArray(fun, SIZE);
}
}
No need to call the printArray function for this one. Simply cout << fun[i].
Also, you need to traversing the array in reverse order. Meaning, start from i=SIZE-1, and keep decrementing as long as i>=0.
Aside from that, I think you should also check that 'low' entered is less than 'high'. What if I entered 7 for 'low' and 3 for 'high'? The if-statement would then never be true.
thanks I have edited it. I feel really dumb but now it is not printing out the values at all. i will also work on checking that low is less than high. that's a good suggestion
1 2 3 4 5
for (int i = 0; i < SIZE-1; i--) {
if (fun[i] < low && fun[i] > high) {
cout << fun[i];
}
}
These are the instructions i received:
Create an array named "fun[]", of 20 integers. Initialize it according to the expression:
"fun[i] = 7 * i2 – 4 * i - 500".
Thank you both so much! :) TheIdeasMan I was doing i ^ 2 when i should have been doing pow(i, 2) and Arslan7041 thank you again I changed it now and its perfect
pow is a little inefficient when the power is an integer, it probably implements some kind of series expansion to do the calculation. So if one is just squaring, it's better to just multiply. Of course one would only notice the difference if there are lots of numbers to do powers with.