Why does it work with if(A[i]>4) but not with if (A[i]<0)

So there's this problem which says to add all the numbers together BUT that are bigger than 4, so for example this vector A(4) = {5,-4,-1,6} the sum would be 5+6=11
But the next problem that I have to solve from my book is kind of the same, but it just asks for me to do kind of the opposite, to add the squares of the negative numbers
so the only change I should make in this code is if (A[i]<0) and S= A[i]*A[i]
I gotta say that the problem is with the if, it doesnt matter if it is <0 or <4 or whatever, it just doesnt work with "<" but only with ">"
somebody help please, I would really appreciate it
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
#include <iostream>
using namespace std;
int main()
{
	const int n = 4;
	int i, S, A[n] = { 5, -4, -1, 6 };
	S = 0;
	cout << "Vector A= {";
	for (i = 0; i <n; i++)
	{
		cout << A[i] << " , ";
	}
	i = 0;
a:
	if (A[i]< 4)
	{
		S = S + A[i];
		i = i + 1;
		if (i<n)
		{
			goto a;
		}
		else
		{
			goto b;
		}
	}
	else
	{
		i++;
		goto a;
	}
b:
	cout << "}\n";
	cout << "\nSum S= "
		<< S
		<< endl;
	system("pause");
	return 0;
}
Last edited on
You should really stop using goto, learn how to use loops. While loops, do-while loops specifically.
You forgot to add the squares.
Last edited on
TarikNeaj, I don't think it really matters, I'm new at learning C++ and I haven't learned a lot of commands, and I don't think that's the problem but if it is, help me, modify the code I've wrote. The question I'm making is why is it working with A[i] > x but not with A[i] < x ? I type the x whatever number I want but it is just working with ">" and not with "<"
Peter I've tried, that's the last problem I'll solve, what if for example the problem is to add the numbers of a vector together BUT that are only smaller than 4? The code should be quite the same.
Last edited on
It's because you're going out of bounds on your array A. You should use a for loop here, not goto statements. I'm not going to attempt to sort out your goto stuff. I'm assuming you're not being forced to use them, so lines 14 through 33 can be replaced with this:

1
2
3
4
5
for (i = 0; i < n; i++) {
	if (A[i] > 4) {
		S += A[i];
	}
}


In fact, just add that if statement to your for loop with the cout in it and you can print and add your values without having to iterate through the array again.
Last edited on
TarikNeaj, I don't think it really matters, I'm new at learning C++ and I haven't learned a lot of commands


It actually does matter a lot. goto is one of the worst bad practices you can use in programming. The earlier you stop using them and learn to use proper loops, the better it is for you :)
im going to try to use other commands, but what kept me awake last night was why is it working with > and not with < ?
i dont get it "out of bounds on our array A" ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int n = 4;
	int A[n] = { 5, -4, -5, 6 };
	int sum = 0;
	

	for (int i = 0; i < n; i++)
	{
		if (A[i] < 0)
		{
			sum += A[i]; // It's like writing sum = sum + A[i]
		}

	}
	cout << "Sum: " << sum << endl;


This simple program will give you the sum of the negative numbers. Use this to finish what you want, squaring the negative numbers.
After your 4th element, when n = 4, because the last element (6) is greater than 4 or 0, it increments i and goes back to a. At this point i is equal to 4, so it is looking at the 5th value in the array. Out of bounds. The > operator works because the last value of the array is greater than 4, if you are looking for numbers less than the last value in your array, < will fail.

If you absolutely needed to use goto, to make this work you would need to add something to your else statement at line 28 to check whether you are at the end of your array, like by replacing line 31 with lines 19-26.

But don't do this. Do it with a while or for loop.
hey pnoid thanks a lot man, it actually worked, copying the lines 19-26 and pasting them at 31
Anyway I would also really like to understand that method with the loops and while, can you give me an example? It's not that I really must use goto
You've experienced the reason why goto and labels are a bad idea: it's easy to write buggy code and hard to find the bugs.

See the tutorial for a description of while and for loops: http://www.cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.