can someone find the error in this code?

It's solved:)
Last edited on
It should be -
1
2
3
4
         if (x != 0)
	{
		cout << AllocateSpace[i] - x << endl;
	}


Basically it says. if x is not equal to 0, then print out the numbered stored in AllocateSpace[i] and then subract x, which is the smallest number. So if for example, AllocateSpace[3] = 10.
And the smallest number = 3. Then 10 - 3 will give you 7.

Hope it helped :)
If you are using x, you really don't need the "smallest" variable. It is declared, defined, but never used.

If the user actually enters 0 as the first integer and then only positive integers the differences will never print out.

On line 28 I'm almost certain you don't want to use AllocateSpace[x] as x is an actual integer entered by the user and not an index value.

Also if you want to display the differences between the numbers entered and the smallest number you need to do that after you have obtained the smallest number.
Last edited on
tarikneaj i corrected it as you said but the first two numbers are not correct for example when i input 4 numbers: 6, 9, 2, 4 the output is 3, 0, 2
I literally just copy paste your program into mine, and then replace that specific if statement with mine and everything works fine.

Here is the entire program -

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

int main()
{
	int num, smallest = 0, x;

	cout << "How many integers do you want to enter?: ";
	cin >> num;

	int* AllocateSpace = new int[num];

	cout << "Enter " << num << " numbers: " << endl;

	for (int i = 0; i < num; ++i)
	{
		cout << "Enter number: ";
		cin >> AllocateSpace[i];
	}

	x = AllocateSpace[0];
	cout << "The differences between the numbers entered and the smallest number are: " << endl;
	for (int i = 1; i < num; ++i) {
		if (AllocateSpace[i] < x) { x = AllocateSpace[i]; }

		if (x != 0)
		{
			cout << AllocateSpace[i] - x << endl;
		}

	}
	cout << "Smallest number is: " << x << endl;

	delete[] AllocateSpace;
	system("pause");
	return 0;
}


Here is a screenshot of the run - http://gyazo.com/5a7dafaf9a195a00ced41e2389a1167f

Could you repost your program?
oh wow looks like that was a mistake on my part. i accidentally copied it into the wrong spot. thank you so much youre a lifesaver!
 
oh wow looks like that was a mistake on my part. i accidentally copied it into the wrong spot. thank you so much youre a lifesaver!


My pleasure! :)
Topic archived. No new replies allowed.