proper usage of the for-loop:
int num = 1, answer;
Code:
for ( int z = 1; z <= 1000; z++ )
{
answer = perfect(z);
if ( answer != 1 )
cout << answer << endl;
// num++; YOU DONT NEED num, you have a for loop with that z - so use z!!!
}
same mistake in your other for loop.
also i guess some ppl (like me) didnt even try to understand your code, because your variable names are not descriptive.
calling a variables just test_0, test_1, test_2, ... is almost as bad as:
Code:
int salt, water, tomatoes, vinegar;
// ... code ...
int ketchup = salt + water + tomatoes + vinegar; // could you imaging what is actually calculated here?
just not as amusing.
instead of:
test_x = a % b;
write
remainder = a % b;
instead of
test_y = a /b;
write something like
quotient = a / b;
so whenever remainder is used anyone who reads that knows that were talking about a remainder of some operation.
also: in for loops usually the variable i is used - this is not a neccessity, but it makes code more readable.
(whenever you see an i, you can assume that there is a for-loop that increases i every time)
note that these are NO GENERAL rules! just examples.
so don't call every result of a modulo operation remainder. give your variables reasonable names.
(e.g. when iterating over elements of a matrix, you may have 2 for loops with the variables row and col)
Code:
for(unsigned row = 0; row < n; ++row) {
for(unsigned col = 0; col < m; ++ col) {
matrix[row][col] = blah....
}
}
note that n and m are reasonable names, because now we can assume were talking about NxM matrices