logical error

Hi, I'm new to c++. We just started on logical errors. I'm struggling to restructure this code so as to remove all the logical errors.

int x = 2;
for (int y = 0; y <= 5; y++ )
{
cout << x << " " <<endl;
x = 2 * x + 3;
if (y % 2 == 0)
x = 3 * x + 2;
}
Homework's fun isn't it?

hint: y can't be 0. because you can't divide by 0.
Hi ulti, its fun....... Sometimes. The question related to the code
Q: Suppose the following fragment of code is correctly inserted into a program, reproduce the output that would be displayed.
When I typed it as it is it gave me,
2
23
49
305
613
3689
I'm not sure what you're asking. But I would guess you're asking for what the program will output, if you compile it. If I were you, I'd do just that. Type it up and compile it.
Line 6 is 'y' mod '2' so it would compile, the remainder would be '0' if 'y' was '0'. This is logically incorrect though since as ultifinitus pointed out, you cannot actually divide by zero.

This is one of those funny things in C++ that may not effect 100 programs you write but you should still be aware of if you use division or modulus in this case in your application.
There is nothing wrong with y%2 when y is zero. This is nothing but the remainder when 0 is divided by 2 which is zero. There is no division by zero even when y is zero. The code runs with the output you obtained by adding using std::cout; and using std::endl; statements.

It only makes sense to talk about a logical error if the code has some purpose or something specific that is suppose to do and the code does not correctly do that. Examples of logic errors would be using < when <= was needed, mistakenly swapping the code for the true and false parts of an if else construct, failing to increment a loop counter creating an infinite loop etc. In these cases the codes would not be doing what you intended it to do because the logic of the algorithm to solve the problem has not been correctly implemented. The code above does what it does and without knowing what its purpose is it doesn't make sense to talk about logic errors.
It would have been logically incorrect to say 2%y, but as alrededor pointed out not to say y%2. Funny that 2 people of whom I know know better got this wrong lol.

In case I judged you incorrectly and you do not understand math after all, you can divide 0 by anything, you just can't divide anything by 0.
Last edited on
for (int y = 0; y <= 5; y++ )

for(int y=0;y<5;y++)

Last edited on
bree, thats not an error. Unless you want the loop to be executed only 5 times.
1
2
3
4
5
6
7
8
int x = 2;
for (int y = 0; y <= 5; y++ )
{
    cout << x << " " <<endl;
    x = 2 * x + 3;
    if (y % 2 == 0)
        x = 3 * x + 2;
}


2 (line 4:the first cout, with unnecessary blank space)
(line 5: now x will be assigned the value of 7)
(line 6: after if(), x will get the value of 23 -because 0%2 = 0, and x were 7)
...(etc)

(I'm not sure whether OP's still around or if he'll check this post ever again...)
To put it simply, logical errors are errors that doesn't affect your compilation but will mess up your calculations/output, so this snippet of code is correct, i.e it should compile.

So to determine whether there's any logical errors or not, he could/should
1) read the problem statement/question/equation again,
2) just simply calculate the results by hand and compare it to the compiled output
Last edited on
Topic archived. No new replies allowed.