can someone help me fix my code?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <ctime>
using namespace std;

void reverse(const int fun[], int newfun[], int size) {
	for (int i = 0, j = size - 1; i < size; i++, j--)
	{
		newfun[j] = fun[i];
	}
}
void printArray(const int fun[], int size) {
	for (int i = 0; i < size; i++)
		cout << fun[i] << " ";
}

int range(int low, int high)
{
	static bool first = true;
	if (first)
	{
		srand(time(NULL));
		first = false;
	}

	return rand() % high + low;
}

int main() {
	const int SIZE = 10;
	int high, low;
	int fun[SIZE];

	cout << "please enter high value: ";
	cin >> high;

	cout << "Please enter low value: ";
	cin >> low;

	for (int i = 0; i < SIZE; i++) {
		fun[i] = 7 * i ^ 2 - 4 * i - 500;
	}

	for (int i = 0; i < SIZE; i++) {
		fun[i] = range(low, high);
	}

	int newfun[SIZE];
	reverse(fun, newfun, SIZE);
	cout << "The original array: ";
	printArray(fun, SIZE);
	cout << endl;

	cout << "The reversed array: ";
	printArray(newfun, SIZE);
	cout << endl;


	system("pause");
	return 0;


}
Last edited on
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 think you may be right. maybe the first one should be random and the rest should be calculated by fun[i] = 7 * i ^ 2 - 4 * i - 500?
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?

Last edited on
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.
Last edited on
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];
		} 
	}
Take a look at your if-condition. Will it ever be true?

If you still don't understand, assume low = 3 and high = 7. Now take a look at your if-condition. Whats wrong here?
i must have been tired i didn't even notice i switched the signs by accident. here is what i have now
1
2
3
4
5
6
7
8
9
for (int i = 0; i < SIZE-1; i--) {
		fun[i] = 7 * i ^ 2 - 4 * i - 500;
		if (fun[i] > low && fun[i] < high) {
			cout << fun[i] << " ";
			if (i <= 0) {
				break;
			}
		}
	}

it only prints one value now, the number 491. i appreciate the time you are taking to help me
Almost good. Your loop starts at 0, then you decrement to -1, thats why only fun[0] is printing.

But remember that the loop needs to start at SIZE-1 and end at 0. Make this small change, remove line 5 and 6, and you should be good.
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".


I think your teacher meant 7 * i2 – 4 * i - 500

So, this is not doing what you think it is:

fun[i] = 7 * i ^ 2 - 4 * i - 500;

^ is bitwise exclusive or (XOR)

Hope this helps :+)
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
One more thing:

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.

Good Luck !!
Topic archived. No new replies allowed.